5

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?

2
  • It's not having difficulties finding my environment, it's just not loading the vars. I used to work in Pycharm, which I really like, but changed to VS Code since it has a lot of free plugins/supports more languages compared to the Community Version of Pycharm. Maybe, it's time to get out the wallet... :) Commented Dec 16, 2019 at 9:04
  • If you liked PyCharm (I do as well), but you need broad support for many languages, you could consider IntelliJ - I am curious what languages you think PyCharm does not support as an editor, that VSCode would though. Often the answer is 'there is a plugin for that'. But IntelliJ will have you covered for full-featured support of most languages - as will other similar platforms like Visual Studio for example. Commented Dec 16, 2019 at 10:54

5 Answers 5

7

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

Sign up to request clarification or add additional context in comments.

6 Comments

I gave this a shot, but also this does not seem to work. I placed a file named dev.env in my project root folder with the following var assignment CLIENT_SECRET=MYSECRET
How are you running your script? If you want to load environment automatically you need to use VSCode debugger with its configuration. By default just hit F5 or Debug -> Start Debugging, on your python file and VSCode will load environment from .dev file.
I tried in debug mode, but also direct in my terminal. Maybe I'm calling the vars the wrong way, how should I call them in my code?
Python: 'os.environ.get("CLIENT_SECRET")'
If you name your environment definition file something other than .env you will need to set the appropriate setting to point to the file such as the "python.envFile" setting.
|
3

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.

2 Comments

Ok, it indeed works. However, it only works when i'm running it in Python Interactive and not when I'm running it in the terminal. Also, your suggested code to echo the var is not working for me.
Yes, works for me. One slight oddity: when you DEactivate in W10, that env var will remain set for the duration of the command prompt session. In other words, deactivating does not get rid of the VE-specific env var. If needed you can achieve this in W10 by set my_env_var = (i.e. set to nothing) in deactivate.bat in the VE files. In Linux you don't (and can't) do this: deactivate should restore the underlying "real" environment, including env vars as they were previously.
2

The default Terminal that popped up for me in VS Code was the PowerShell, which I didn't realize at first. I had to use:

To write the env variable

$Env:MYAPP_DB_USER = "myapp_user"

To read the env variable

$Env:MYAPP_DB_USER

NB: '$' in front is part of the command. 'MYAPP_DB_USER' is the Key and 'myapp_user' is the Value I don't want to keep the passwords and keys as part of the code (including .env file) and have it pushed to the git repo and the server.

Ref: Microsoft PowerShell - Env variables

Comments

0

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

Comments

-1

use set in CMD teminal

use env: in powershell

1 Comment

This is how you set a env variable in a VE. But the question is about how you PRESERVE that env variable from one activation to the next. Normally they DISAPPEAR when the VE is deactivated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.