20

Is there a difference between running script using virtualenv interpreter (without virtualenv activation) and running it in activated virtualenv?

venv/bin/python some_script.py

vs

source venv/bin/activate
python some_script.py

3 Answers 3

12

If you directly run a script or the python interpreter from the virtualenv’s bin/ directory (e.g. path/to/ENV/bin/pip or /path/to/ENV/bin/python-script.py) then sys.path will automatically be set to use the Python libraries associated with the virtualenv. But, unlike the activation scripts, the environment variables PATH and VIRTUAL_ENV will not be modified. This means that if your Python script uses e.g. subprocess to run another Python script (e.g. via a #!/usr/bin/env python shebang line) the second script may not be executed with the same Python binary as the first nor have the same libraries available to it. To avoid this happening your first script will need to modify the environment variables in the same manner as the activation scripts, before the second script is executed.

source: https://virtualenv.pypa.io/en/16.7.9/userguide.html#activate-script

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

4 Comments

This is unbelievably stupid. Why did they design it this way? It's easier if I just clone the main installation rather than try to deal with this mess :\
Current (20.26.2) docs do not have this text, and instead say: "Note that you don’t have to activate a virtual environment to use it. You can instead use the full paths to its executables, rather than relying on your shell to resolve them to your virtual environment."
@Nickolay Interesting, I've tested with python 2.10 and I see the activation script changing these variables: OLDPWD, PATH, PS1, VIRTUAL_ENV, VIRTUAL_ENV_PROMPT I think PATH change is worth mentioning in the Doc, right?
@uak: yep, it says so: "The activate script prepends the virtual environment’s binary folder onto the PATH environment variable. It’s really just convenience for doing so, since you could do the same yourself."
9

Running source bin/activate will set the PATH variable to point to your environment bin directory which is useful if you have other command line scripts/binaries installed (this can happen with certain python packages that add shell commands), it will also unset/set PYTHONHOME.

So, if bin/python works for you then you're fine but if some of the packages you're using start behaving strangely (or wrong one gets imported) it's probably because Python is getting the wrong PYTHONHOME or because a certain script is not found in PATH.

4 Comments

activate script modifies PATH, but I don't see it setting PYTHONHOME (ubuntu 14.04)
Actually, it looks like it unsets PYTHONHOME. Maybe it's set somewhere else? Eitherway, there is definite PYTHONHOME handling in the activate script, it makes a copy of it and unsets it during activation and then sets it back during deactivation. Lines 13-17 and 49-55.
Does this mean that it's not possible to run a script without activating an environment?
I think you can still run a script without having to source or activate the virtual environment. e.g. You can run the version of python and installed dependencies on the .venv/bin directory by specifying the path and it will pick up any dependencies installed there with pip or with your requirements.txt You can also try adding this path to your users PATH environmental variable eg export PATH=/path/to/.venv/bin:$PATH when running on the server.
3

Yes. Virtualenv creates an interpreter in its own right. Just do this,

which python

For each interpreter, virtualenv and your normal interpreter and see what happens. They will show you two different links to python interpreter. Here's my example:

quazinafiulislam@Nafiuls-Mac: ~/Code/Python/PyTestingZone
 $ which python                                                                                               [7:49:26]
/Users/quazinafiulislam/.pyenv/shims/python

quazinafiulislam@Nafiuls-Mac: ~/Code/Python/PyTestingZone
 $ source .venv/bin/activate                                                                                  [7:49:29]
(.venv)
quazinafiulislam@Nafiuls-Mac: ~/Code/Python/PyTestingZone
 $ which python                                                                                               [7:49:35]
/Users/quazinafiulislam/Code/Python/PyTestingZone/.venv/bin/python

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.