Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python os.getenv() returning None when set Windows environment variables

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!

like image 293
Hannah Bernal Avatar asked Mar 27 '26 04:03

Hannah Bernal


1 Answers

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.

like image 182
LhasaDad Avatar answered Mar 28 '26 18:03

LhasaDad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!