'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 1:[1]

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

Solution 2:[2]

Your first solution

set CLIENT_SECRET=MYSECRET

in activate.batshould work, when using Command Prompt in the terminal as Default Shell.

You can omit the quotes unless they are part of your envirionment variable.

You can verify, if the environment variable is set with:

echo %CLIENT_SECRET% in the terminal in VS-Code.

Solution 3:[3]

go to endowment variable folder enter Script folder now activate with cmd

Solution 4:[4]

use set in CMD teminal use env: in powershell

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 jorop
Solution 3 ashish bindra
Solution 4 Raoul Zachary