How to add path to PYTHONPATH in virtualenv

I am trying to add the path to the PYTHONPATH environment variable, which will only be visible from a specific virtual environment.

I tried SET PYTHONPATH=... on the virtualenv command line, but this sets the variable for the whole environment.

How do I achieve this?

+79
python virtualenv
May 24 '12 at
source share
4 answers

You can usually do nothing with PYTHONPATH using .pth files . Just put the file with the extension .pth (any base name) in your virtualenv directory folder, for example. lib\python2.7\site-packages , with the absolute path to the directory containing your package being its only content.

+131
May 24 '12 at 14:47
source share

If you are using virtualenv , you should probably also use virtualenvwrapper , in which case you can use add2virtualenv to add Python pathnames for the current virtualenv:

add2virtualenv directory1 directory2 …

+84
Mar 07 '13 at 22:23
source share

You can also try to establish a symbolic link to one of your virtual servers.

eg. 1) activate your virtualenv 2) run python 3) import sys and check sys.path 4) you will find the python search path there. Select one of them (for example, site packages) 5) go there and create a symbolic link to your package, for example: ln -s path-to-your-package name-with-which-you-you-be-importing

This way you can import it without even activating your virtualenv. Just try: path-to-your-virtualenv-folder / bin / python and import your package.

+4
May 09 '16 at 12:49
source share

In Python 3.6.4

 import sys import os print(str(sys.path)) dir_path = os.path.dirname(os.path.realpath(__file__)) print(f"current working dir: {dir_path}") sys.path.insert(0, dir_path) 

I highly recommend you use virtualenv and virtualenvwrapper to avoid confusion

0
Mar 01 '18 at 9:45
source share



All Articles