Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 205
LuckyLub Avatar asked Oct 30 '25 14:10

LuckyLub


2 Answers

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

like image 116
makozaki Avatar answered Nov 02 '25 05:11

makozaki


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.

like image 29
jorop Avatar answered Nov 02 '25 05:11

jorop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!