9

I am following a Twitter bot tutorial using the Twippy library and Twitter API: https://realpython.com/twitter-bot-python-tweepy/

I set the config.py file and set my Windows environment variables as user variables with all my tokens. But when I run my file, it gives an error since os.getenv() is None when retrieving my tokens

consumer_key = os.getenv("CONSUMER_KEY")
consumer_secret = os.getenv("CONSUMER_SECRET")
access_token = os.getenv("ACCESS_TOKEN")
access_token_secret = os.getenv("ACCESS_TOKEN_SECRET")

In the Windows terminal, I printed each of these variables and they are correct. Is there something I am missing here? Any help is much appreciated!

Hannah Bernal
  • 133
  • 1
  • 1
  • 10
  • when you said set at user variables are you saying you set it through the gui to set user env vars? or did you do command line? did you start a fresh cmd line to run the python program? before running the python script can you see the values? – LhasaDad Aug 24 '20 at 00:26
  • @LhasaDad I set the user env vars in the gui (the system properties window). After I set those, I was able to see my values by running a command line and doing "echo" to each of my variables. But after that, when I ran my script, os.getenv() returns None for all variables – Hannah Bernal Aug 24 '20 at 02:08
  • 1
    How did you run you script? From an ide? Did you restart it to pick up the env var change? – LhasaDad Aug 24 '20 at 03:03
  • @LhasaDad Ah restarting it worked! I didn't restart VS Code after I set up my variables so after I did that and ran my script, it got the correct values. Thank you, I appreciated your help! – Hannah Bernal Aug 24 '20 at 03:21
  • There is a way to pass in vars on launch from vs code. Will post an answer with that in the morning – LhasaDad Aug 24 '20 at 03:26

4 Answers4

5

So this is an issue with the fact that processes that are spawned off another process inherit its set of environment variables. In this case the IDE in use that is launching the code needs to be restarted. An alternative for VS Code is to launch the item with the environment specified. This can be done by adding the env option to a launch config:

   {
        "name": "Python: Current File (Integrated Terminal)",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal"
        "env": {"VAR_NAME": "VAR_VALUE"
                "VAR_NAME2": "VAR_VALUE2"}
    },

In this case, the VAR_NAME and VAR_NAME2 are the environment variable names. and the VAR_VALUE and VAR_VALUE2 would be the strings assigned to them.

LhasaDad
  • 1,786
  • 1
  • 12
  • 19
  • 1
    "In this case the IDE in use that is launching the code needs to be restarted." This solved the issue for me (Windows 10, running Python on SublimeText) – Lfppfs May 11 '22 at 15:05
4

If os.getenv() is not working you can use decouple. Just do pip install python-decouple and then in the code do from decouple import config and then you can do this:

consumer_key = config('CONSUMER_KEY')
consumer_secret = config('CONSUMER_SECRET')
access_token = config('ACCESS_TOKEN')
access_token_secret = config('ACCESS_TOKEN_SECRET')

This worked in my case. Hope so it will work in your case too.

cooltea
  • 1,113
  • 7
  • 16
2

Newby here. I restarted VSCode, but it still didn't work. It wasn't until I restarted VSCode, and closed the project folder, THEN re-opened it.

Learning from this https://youtu.be/M576WGiDBdQ?t=14649

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30637385) – garg10may Dec 21 '21 at 04:15
0

I had exact same issue and restarting computer solved the problem

bohontw
  • 165
  • 6