Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customise the virtualenv `activate` script

What is the conventional way to customise the changes made by a Python virtualenv bin/activate script?

The Python virtualenv is activated by a $VENV/bin/activate script. This script works primarily by setting environment variables specific to the virtualenv.

(This is different from the “bootstrap script” discussed in the Virtualenv documentation. Those are used once, when creating the virtualenv; they are not used when activating the virtualenv. This question is about the activate script.)

Many of the code bases for which I use a Python virtualenv, need additional (custom) environment variables set, at the same time the virtualenv is activated.

  • Edit the $VENV/bin/activate script directly, to add statements that set more environment variables?

  • Write a custom wrapper script, that incidentally calls $VENV/bin/activate?

  • Write a separate script with a specific name, that will be automatically called by the $VENV/bin/activate script? Does such a thing exist?

Of course I could write a script with a different name, and instruct developers to run that instead. Or I could instruct them to install virtualenvwrapper. But that is not the if-you-got-the-virtualenv-working-then-this-works-too automation that I'm seeking.

Instead, this question is asking how to hook into the existing convention, of activating the virtualenv with a known name $VENV/bin/activate.

like image 815
bignose Avatar asked Nov 08 '22 21:11

bignose


1 Answers

How about using virtualenvwrapper and editing $VIRTUAL_ENV/bin/postactivate? http://virtualenvwrapper.readthedocs.io/en/latest/scripts.html#scripts-postactivate

like image 167
tudor Avatar answered Nov 14 '22 20:11

tudor