Search code examples
pythonwindowsvisual-studio-codevirtualenvpython-venv

Set environment variables when activating python virtual environment in windows


I want to be able to set up environment variables in my virtual environment so that they are available in my code when I activate the virtual environment. I make my virtual enviornments with venv. I'm working on a Windows machine with VS-code.

What I already tried, but didn't work.

  1. Adding the vars to end of the activate.bat file like this:
set CLIENT_SECRET="MYSECRET"
  1. Adding the vars to the end of the Activate.ps1 file like this:
$CLIENT_SECRET="MYSECRET"
  1. Adding the vars to the end of the activate file like this:
export CLIENT_SECRET="MYSECRET"

I found a lot related to my topic, but none working for me. What to do?


Solution

  • If you want to setup your development environment in VSCode you can simply add .env file with all secrets defined in project root directory. More details in docs