1

I would like to create a .env file in my project root that I could execute to initialize env vars inside a virtualenv when running.

Bonus points if I can do this automatically when I cd into the project root, like autoenv on linux.

This seems like such a simple thing to do but I have not found an answer anywhere on here as of yet. Please point me in the direction if there is one. Thanks for your time.

2
  • You could probably create a .bat file containing all of your set VARIABLE=X commands. This can also be done with a PowerShell script. I also think that you can do this with os.environ['VARIABLE'] = 'X' in python. Commented May 27, 2020 at 9:50
  • 2
    Thanks, but creating a python environment and initializing vars inside it, is a fairly basic requirement of a python env and quite easy to do on Linux, I'm looking for the Windows equivalent. Setting the vars inside of the python script would defeat the purpose as they contain sensitive info like api keys, db strings, etc. Also a .bat file would create the vars on my system and not in the env. Commented May 30, 2020 at 7:29

1 Answer 1

2

For now, I have added the vars to the env/scripts/activate.ps1 script, meaning they will be created with environment. If you want to do this, create your virtualenv:

virtualenv env

Then add your env vars next to the default vars in the env/scripts/activate.ps1 script in the form:

$env:some_key="someValue"

Now anytime you create the env, it will run those lines and create your vars.

This is not a clean solution and I don't like it, but for lack of anything better am going to use it for now.

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

Comments

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.