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

6

11

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!

Hypognathous answered 23/8, 2020 at 23:22 Comment(5)
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?Shuttering
@Shuttering 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 variablesHypognathous
How did you run you script? From an ide? Did you restart it to pick up the env var change?Shuttering
@Shuttering 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!Hypognathous
There is a way to pass in vars on launch from vs code. Will post an answer with that in the morningShuttering
S
6

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.

Shuttering answered 25/8, 2020 at 0:11 Comment(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)Yean
D
6

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.

Dominicdominica answered 23/1, 2021 at 18:3 Comment(0)
S
4

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

Spume answered 20/12, 2021 at 21:24 Comment(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 ReviewReisch
T
0

I had exact same issue and restarting computer solved the problem

Togo answered 4/5, 2022 at 18:43 Comment(0)
S
0

For most of the IDEs, you will need to restart it after setting the new environmental variables (in Windows OS)

Shirtwaist answered 7/2, 2024 at 20:38 Comment(0)
C
0

I encountered the same issue. Initially, I attempted to set the environment variables via the terminal and restarted the IDE, but to no avail. Subsequently, I resorted to using the Windows GUI to configure the environment, and after restarting the IDE, it functioned correctly.

An alternative for PyCharm IDE adding key=value;key1=value1 to Configurations -> Environment -> Environment variables.

Conduce answered 17/2, 2024 at 16:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.