Reputation: 27139
If I have python script which activates the virtualenv like this:
#!/path/to/venv/bin/python
How can I set variables for this script without modifying this script?
I want this environment variable to be active for all scripts which use this virtualenv.
This means modifying this script is not a solution, since there are twenty scripts, and I don't want to modify twenty scripts.
Writing a shell wrapper-script around the python scripts would work, but I would like to avoid this.
In the past I thought a custom sitecustomize.py
can be used for start-up code. But Ubuntu (AFAIK the only distribution which does this) comes with its own sitecustomize.py file, with the effect that my sitecustomize.py does not get called. See https://bugs.launchpad.net/ubuntu/+source/python2.5/+bug/197219
Here are some ways how I want to use the virtualenv:
(I have thought about this again. I guess it setting the variables is not the job of python or virtualenv. I need a unified way to set environment variables. And in my case I would like to do this without using a shell wrapper).
Upvotes: 20
Views: 37036
Reputation: 1467
The problem with Ubuntu it seems that python package comes with PYTHONNOUSERSITE as cPython compilation flag. You can find it with site module and see that site.ENABLE_USER_SITE
is set to False.
tl;dr #!/path/to/venv/bin/python
is not enough to execute your virtual environment.
Although the rest of the answers seems to be correct in some contexts, the problem with the question is that he forces the execution with #!/path/to/venv/bin/python
but hi is not providing any contextual virtual environment in his question, so I suppose he is not activating his virtual environment previous to run his script.
That is useless because that command usually links to your system python executable[^1] and does anything more. Also, by default you will use your system environment variables if you don't activate the virtual environment with source /path/to/venv/bin/activate
or configure apache or nginx to manage your venv.
Then, is possible to add a specific line in your venv/bin/activate
script that adds a environment value in bash language:
export FOO=BAR
and then activate it with source
command.
[^1]: Probably you will prefer a generic #!/usr/bin/env python3
(python2 is deprecated). The environment will take properly your executable.
Upvotes: 0
Reputation: 3883
Bit hackish, but should work.
python
link in virtual environment bin
to something like python.lnk
bin
folder create file python
that looks like#!/bin/sh
export TEST='It works!'
"$0.lnk" "$@"
chmod +x python
Then if you run a script like
#!/work/venv/bin/python
import os
print 'Virtualenv variable TEST="{}"'.format(os.environ['TEST'])
as follows:
./myscript.py
it prints out:
Virtualenv variable TEST="It works!"
Upvotes: 1
Reputation: 2585
From what I have tried, it seems if you create a sitecustomize.py
file inside the virtual environment, it will take precedence over the global sitecustomize.py
installed in /usr/lib/python2.7
directory. Here is what I did:
Create a sitecustomize.py
in the virtual environment
$ echo "import os; os.environ['FOO'] = 'BAR'" > ~/venvs/env_test/lib/python2.7/sitecustomize.py
Verify that it is getting imported and executed when running the Python binary from the virtual environment
$ ~/venvs/env_test/bin/python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sitecustomize
>>> sitecustomize.__file__
'/home/abhinav/venvs/env_test/lib/python2.7/sitecustomize.py'
>>> import os
>>> os.environ['FOO']
'BAR'
>>>
Just to verify that FOO
is set even without explicitly importing sitecustomize
:
$ ~/venvs/env_test/bin/python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['FOO']
'BAR'
>>>
Upvotes: 4
Reputation: 75
After trying dotenv
package as well as the .pth
method, I discovered they didn't work for me. So, I just modified the venv/bin/activate
script, and exported the variables there.
Here's the idea.
$ cat venv/bin/activate
deactivate () {
unset FOO
unset BAR
...
}
...
export FOO='xxx'
export BAR='xxx'
Upvotes: 3
Reputation: 16654
while writing sitecustomize.py
file and changing bin/python
all are feasible solutions, I would suggest another method that does not involve directly change contents inside virutalenv, by simply install a .pth
file:
./venv/lib/python2.7/site-packages/_set_envs.pth
with content:
import os; os.environ['FOO'] = 'bar'
test:
$ ./venv/bin/python -c "import os; print os.getenv('FOO')"
bar
the trick is, python will load every .pth
file on startup, and if there is a line starts with import
, this line will be get executed, allowing inject arbitrary code.
the advantage is, you could simply write a python package to install this .pth
file with setuptools, install to the virtualenv you want to change.
Upvotes: 39