How to Add a Path to Pythonpath in Virtualenv

How do I add a path to PYTHONPATH in virtualenv

You can usually avoid having to do anything with PYTHONPATH by using .pth files. Just put a file with a .pth extension (any basename works) in your virtualenv's site-packages folder, e.g. lib\python2.7\site-packages, with the absolute path to the directory containing your package as its only contents.

How to add to pythonpath in virtualenvironment

"I can just add to user variables" just means adding an environment variable to the command shell. Virtualenv shouldn't (by default at least) clear environment variables. So it should just work if you already have PYTHONPATH set.

If you only want the path available to the virtualenv then modify the file called Scripts/activate.bat in the virtualenv folder and add the line:

set PYTHONPATH=path/to/dir

How do you set your pythonpath in an already-created virtualenv?

The most elegant solution to this problem is here.

Original answer remains, but this is a messy solution:


If you want to change the PYTHONPATH used in a virtualenv, you can add the following line to your virtualenv's bin/activate file:

export PYTHONPATH="/the/path/you/want"

This way, the new PYTHONPATH will be set each time you use this virtualenv.

EDIT: (to answer @RamRachum's comment)

To have it restored to its original value on deactivate, you could add

export OLD_PYTHONPATH="$PYTHONPATH"

before the previously mentioned line, and add the following line to your bin/postdeactivate script.

export PYTHONPATH="$OLD_PYTHONPATH"

How to add virtualenv to path

It seems that I myself am the exception to the rule for almost all 'simple' installation procedures. For some reason, it WAS a path related issue:

I ran brew info python, which outputted a lot of information. At the bottom I found this:

Executable python scripts will be put in:
/usr/local/share/python
so you may want to put "/usr/local/share/python" in your PATH, too.

I added that to my PATH in /etc/launchd.conf and ~/.bashrc and lo and behold:

$ which virtualenv 

tells me:

"/usr/local/share/python/virtualenv"

I still don't know why I couldn't find any pointers in the right direction, online, anywhere? Is pip install virtualenv supposed to add to the PATH itself? If so, why not on my system? Why did @bibhas tell me explicitly it was not a path issue?

Does virtualenv just modify the pythonpath?

No, it is not. virtualenv is not only about site-packages, it is about a whole isolated python environment.

Doing source /path/to/venv/bin/activate just changes your $PATH environment variable to include your virtualenv bin directory as first lookup.

If you call python directly, it is just a shortcut for:

$ /path/to/venv/bin/python myscript.py

And if you call pip in an activated virtualenv, it is the same as:

$ /path/to/venv/bin/pip install XYZ

How does activating a python virtual environment modify sys.path?

sys.path is initiated in site.py, it is set using the relative path of sys.prefix, which is the path of python executable inside the virtual environment.

if the virtual environment is created without option --system-site-packages, which is the default, the config value of key include-system-site-packages set to false in pyvenv.cfg .

virtualenv has an identical option --system-site-packages, but it will write a file named no-global-site-packages.txt into the site dir of venv as a flag.

during python startup, site.py is executed, it will check pyvenv.cfg config file to set sys.path:

If “pyvenv.cfg” (a bootstrap configuration file) contains the key “include-system-site-packages” set to anything other than “true” (case-insensitive), the system-level prefixes will not be searched for site-packages; otherwise they will.

if venv is created with virtualenv, site.py
in venv is a modified version, it check the existence of file no-global-site-packages.txt, if this flag file not exists, system-wide site package path will be added to sys.path, which is infered from sys.real_prefix.

update 2022: lastest virtualenv also use pyvenv.cfg.

hope this could answer your question.



Related Topics



Leave a reply



Submit