Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.environ.get() does not return the Environment Value in windows?

I already set SLACK_TOKEN environment Variable. But "SLACK_TOKEN=os.environ.get('SLACK_TOKEN')" is returning "None". The type of SLACK_TOKEN is NoneType. I think os.environ.get not fetching value of environment variable. so rest of the code is not executing.

import os
from slackclient import SlackClient


SLACK_TOKEN= os.environ.get('SLACK_TOKEN') #returning None
print(SLACK_TOKEN) # None
print(type(SLACK_TOKEN)) # NoneType class

slack_client = SlackClient(SLACK_TOKEN)
print(slack_client.api_call("api.test")) #{'ok': True}
print(slack_client.api_call("auth.test")) #{'ok': False, 'error': 'not_authed'}


def list_channels():
    channels_call = slack_client.api_call("channels.list")
    if channels_call['ok']:
        return channels_call['channels']
    return None

def channel_info(channel_id):
    channel_info = slack_client.api_call("channels.info", channel=channel_id)
    if channel_info:
        return channel_info['channel']
    return None

if __name__ == '__main__':
    channels = list_channels()
    if channels:
        print("Channels: ")
        for c in channels:
            print(c['name'] + " (" + c['id'] + ")")
            detailed_info = channel_info(c['id'])
            if detailed_info:
                print(detailed_info['latest']['text'])
    else:
        print("Unable to authenticate.") #Unable to authenticate
like image 826
Ravi Bhushan Avatar asked Mar 23 '17 14:03

Ravi Bhushan


2 Answers

I faced similar issue.I fixed it by removing quotes from the values. Example: I created a local.env file wherein I stored my secret key values :

*local.env:*
export SLACK_TOKEN=xxxxxyyyyyyyzzzzzzz

*settings.py:*
SLACK_TOKEN = os.environ.get('SLACK_TOKEN')

In your python terminal or console,run the command : *source local.env*

****Involve local.env in gitignore.Make sure you dont push it to git as you have to safeguard your information.

This is applicable only to the local server.Hope this helps.Happy coding :)

like image 115
CodeHelp Avatar answered Oct 09 '22 05:10

CodeHelp


In my case, I write wrong content in env file:

SLACK_TOKEN=xxxxxyyyyyyyzzzzzzz

I forgot export befor it, the correct should be:

export SLACK_TOKEN=xxxxxyyyyyyyzzzzzzz
like image 41
Galley Avatar answered Oct 09 '22 03:10

Galley