Visual Studio 2019 cannot read environment variable for API key?

Forgive me if this is a silly question:

I’ve set my API key as an environment variable of my computer, as per OpenAI’s instruction.

I have no problem fetch it in a Jupiter notebook using simple os.getenv(“OPENAI_API_KEY”);

but when I copy paste my code into Visual Studio 2019, it’s not able to get this environment variable, os.getenv returns NONE.

My question:

  1. Why os.getenv() cannot get result in VS, but get result in a JupyterNotebook?

Does Visual Studio 2019 create some sandbox environment so os.getenv() does not have access to the computer system?

  1. Say if I packaged my code into a exe, send it to other people, can the code get environment variable from their computer, assuming they correctly set up and named their environment variable for API key?

Thank you.

Not sure if this will make any difference, but is your VS Code being ran as an administrator? I’ve not actually played with env variables in 2019 so I’m not sure, it works in VSCode, but that could be setup or something unrelated.

1 Like

Ask that of ChatGPT, you might like the answer.

1 Like

I didn’t run Visual Studio as admin. It might be some computer setup issues, since it worked on yours …