134

I am trying to add a path to the PYTHONPATH environment variable, that would be only visible from a particular virtualenv environment.

I tried SET PYTHONPATH=... under a virtualenv command prompt, but that sets the variable for the whole environment.

How do I achieve that?

8 Answers 8

207

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/python3.13/site-packages or (on Windows) lib\site-packages. The file should contain just a path to the directory containing your package; it can be either an absolute path or one relative to the .pth file.

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

6 Comments

Unfortunately this does not work as an override. It appends the path, so if you're developing it doesn't work.
Also, if you know the absolute path, what's the point of a variable?
you can also navigate to your virtual environment's site-packages folder and create a symbolic link to the .pth file like this: ln -s path/to/somfile.pth. I found this useful when I needed the same directory to be accessible in multiple virtual environments
How to track this file if venv files not should be tracked ?
To correct OP, @JamieMarshall, docs say paths in PTH files can be relative to the PTH path.
|
97

If you're using virtualenv, you should probably also be using virtualenvwrapper, in which case you can use the add2virtualenv command to add paths to the Python path for the current virtualenv:

add2virtualenv directory1 directory2 …

12 Comments

How about removing from virtualenv?
I want to add a friendly comment that on shared hosts and similar situations venv wrapper is not desired. In such cases one venv is in effect and all that is needed making the additional install is not desired. Locally things are different, but on server/image KISS is really important.
I'm not sure how the command worked when this was written, but add2virtualenv doesn't modify $PYTHONPATH, rather it modifies sys.path.
@ajostergaard: Sorry, I should have provided a source. If you look at the source code for add2virtualenv, you can see it's modifying sys.path. I agree that the docs make it sound like it modifies the PYTHONPATH environment variable, but that appears to be incorrect. bitbucket.org/virtualenvwrapper/virtualenvwrapper/src/…
@ForeverWintr I stand corrected - .pth files are used to setup sys.path. docs.python.org/2/library/site.html Confused.com!
|
6

If you are using virtualenvwrapper,

$ cd to the parent folder
$ add2virtualenv  folder_to_add

console will display

Warning: Converting "folder_to_add" to "/absoutle/path/to/folder_to_add"

That's it, and you should be good to go

Comments

5

You can also try to put symlink to one of your virtualenv.

eg. 1) activate your virtualenv 2) run python 3) import sys and check sys.path 4) you will find python search path there. Choose one of those (eg. site-packages) 5) go there and create symlink to your package like: ln -s path-to-your-package name-with-which-you'll-be-importing

That way you should be able to import it even without activating your virtualenv. Simply try: path-to-your-virtualenv-folder/bin/python and import your package.

1 Comment

I guess this was downvoted for using symlinks rather than .pth files. It worked for me though, so, whavever.
2
import sys
import os

print(str(sys.path))

dir_path = os.path.dirname(os.path.realpath(__file__))
print("current working dir: %s" % dir_path)

sys.path.insert(0, dir_path)

I strongly suggest you use virtualenv and virtualenvwrapper to avoid cluttering the path.

3 Comments

if you want this to work with any python version, just use a normal old-style string-format rather than the fancy f-string f"... {dir_path}"
Thanks for the comment. I revised for use. the old ways are cumbersome and idiosyncratic and there is nothing fancy about a simple templating system
Everything is relative :)
1

I agree with most of the answers here that changing PYTHONPATH through whatever means is less elegant than adding the package you want through some form of link. However, I think the best way to add such links is through pip install -e /path/to/your/lib (after activating the virtualenv, of course). This also creates a .egg-link file in the appropriate site-packages directory, so there is no need for elaborate ways to find the right site-packages dir. And you do not need any virtualenv-specific tools installed.

As people pointed out above, that is not quite the same as changing PYTHONPATH because that appends to sys.path instead of prepending, but in many scenarios that is irrelevant.

Comments

0

As suggested by @crimeminister above, you can use virtualenvwrapper then add2virtualenv like suggested by @Aneesh Panoli. If add2virtualenv is not working after pip install virtualenvwrapper, then follow instructions in the top voted answer by @chirinosky here. Works for me.

Comments

0

See also https://stackoverflow.com/a/4758351/29165416 :

Add the following to bin/activate:

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

Add the following to bin/postdeactivate:

export PYTHONPATH="$OLD_PYTHONPATH"

But to echo what everybody is saying, a .pth file can reference an entire site-packages folder inside another. The environment variable mutation isn't needed.

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.