14

I'm using venv (used pyvenv to create the environment) and would like to set up environment variables here, but postactivate looks like a virtualenv thing. Can this be done with venv?

StringsOnFire
  • 2,726
  • 5
  • 28
  • 50

3 Answers3

14

venv has the activate script which you can modify to add your environment variables.

I would add the variables at the bottom, making a nice comment block to clearly separate the core functionality and my custom variables.

masnun
  • 11,635
  • 4
  • 39
  • 50
2

Open [your_virtualenv_dir]/bin/activate (not activate.csh or activate.fish)

To set environment variable on activate, put your exports at the bottom of the file e.g.

export SOME_VARIABLE=some_value

To unset the environment variable upon deactivation, unset the variables within the deactivate function at the top of the file e.g.

deactivate(
  ...
  # default code by venv
  ...
  
  unset SOME_VARIABLE

)

Seems to work fine for me.

0

Put your setup config in [your_virtualenv_dir]/bin/postactivate and your teardown config in [your_virtualenv_dir]/bin/predeactivate.

e.g. in postactivate:

YOUR_ENV_VAR="hello world!"

e.g. in predeactivate:

unset YOUR_ENV_VAR
arcanemachine
  • 571
  • 4
  • 9