Why I can't access my environment variable from .env file in vscode python?

Issue

I am really new to the space so sorry if my questioning is bad or I ask novice questions. also thanks in advance for the repliers!

what I am trying to do?
I am trying to access my environment variable from the .env file and print its value in the terminal.

What is the issue?
When I run the script in the terminal I keep getting the error none

I checked a few forums and the dotenv paper about the issue but none helped me solve this

some more info:

I am using windows 10

The version of dotenv is 0.20.0

I downloaded it using python -m pip install python-dotenv

the code

.env file –
export PRIVATE_KEY = 0xc3c4e4fe27d8e6b06710e713878e4488c034ce346a578fdfa78bb3d335130eec

the python file –

from dotenv import load_dotenv

 import os

 load_dotenv()

 print(os.getenv("PRIVATE_KEY"))










Solution

first thanks for the replies! Didn’t expect an answer in less than a 24h, it really feels nice.

So I solved my issue but it wasn’t after removing the "export" in the .env file (I tried with and without it both gave the same results in the terminal), I had to specify the full path of the .env file in the load_dotenv(), apparently I had to specify it but many code samples I saw in forums didn’t need to do it I wonder why…

anyway here is the new code…

the python code –

enter image description here

the .env file –

enter image description here

by the way it’s not a real private key

I am not gonna close this thread yet, I still have a question regarding this issue as it was more luck that helped me solve this…I don’t want to see this issue pop up again for me in the future so if anyone can answer my question it would be great!

At some point before I found the solution I ran the python script in the terminal and got a random private key (I don’t have it pictured sadly), it might had been a key I set in the past but then I checked if I had other .env files in the folder but I had none I also didn’t have any environment variables in System Properties > Advanced > Environment Variables on windows so where does dotenv gets the key values by dafault? also After that I reopened vscode and tried again but I got a none error…

Answered By – boten500

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published