简体   繁体   中英

Environment variables not being accessed in Python script on VSCode

I am creating a simple app that connects to an API and stores specific data into a list. In order to connect to the API, I must do BasicAuth with an email and password. Whenever I hardcode my email and password into the Python script, it works perfectly, however whenever I store these values in a .env file and do os.getenv('EMAIL') , I am getting a 401 error. I proceeded to print out the value of EMAIL and PASSWORD when EMAIL = os.getenv('EMAIL') and PASSWORD = os.getenv('PASSWORD') but it is printing None in the console. Here is my code in the Python script:

    load_dotenv()
    EMAIL = os.getenv('EMAIL')
    PASSWORD = os.getenv('PASSWORD')

Here is what my.env file looks like:

    EMAIL = importantemail@mydomain.com  
    PASSWORD = secret_password

The two files are in the same folder but I am thinking that I need to do something like pointing my working directory to the folder that has the script and the.env file. This is my first full-fledged project so I believe that I am missing something obvious.

os.getenv() is for system environment variables like HOME , PATH , etc. This function does not read any data from your .env file.

You can use this parser , or parse file by your own with os package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM