How do I add a path to PYTHONPATH in virtualenv
Asked Answered
F

7

130

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?

Flow answered 24/5, 2012 at 13:57 Comment(0)
I
198

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.

Insinuation answered 24/5, 2012 at 14:47 Comment(6)
Unfortunately this does not work as an override. It appends the path, so if you're developing it doesn't work.Dvina
Also, if you know the absolute path, what's the point of a variable?Philipphilipa
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 environmentsApron
How to track this file if venv files not should be tracked ?Sidwell
This is the only thing that worked for my when using anaconda on macOS with current version of vscode. Pylance would not recognize my other modules folders without this .pth file. Thank you!Edmundoedmunds
To correct OP, @JamieMarshall, docs say paths in PTH files can be relative to the PTH path.Unaffected
P
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 …

Paprika answered 7/3, 2013 at 22:23 Comment(12)
where does that utility come from? I tried pip install virtualenvwrapper and that didn't get it for me.Agosto
How about removing from virtualenv?Extravert
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.Eating
I'm not sure how the command worked when this was written, but add2virtualenv doesn't modify $PYTHONPATH, rather it modifies sys.path.Salerno
@Salerno not according to the docs: virtualenvwrapper.readthedocs.io/en/latest/…Tangible
@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/…Salerno
@Salerno what I see there is that much stuff is written into _virtualenv_path_extensions.pth inside site-packages... AFAIK .pth files in site-packages are used to set PYTHONPATH not sys.path. The sys.path stuff appears to be stuff added to the top of the file if the file doesn't already exist.Tangible
@Salerno I stand corrected - .pth files are used to setup sys.path. docs.python.org/2/library/site.html Confused.com!Tangible
I'd just noticed that myself and was trying to figure out what gets modified by .pth files. :) I can't remember now if I was aware of that when I wrote the original comment, or if I was only correct by accident.Salerno
@Salerno turns out sys.path IS PYTHONPATH - which I never knew. :) "The search path can be manipulated from within a Python program as the variable sys.path." docs.python.org/2/using/cmdline.html#envvar-PYTHONPATHTangible
@Salerno I always thought sys.path was the env PATH. Learn something new... :)Tangible
This just creates a pth file. Which is an append-only modification.Dvina
M
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

Muscid answered 27/2, 2019 at 18:20 Comment(0)
R
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.

Ruffian answered 9/5, 2016 at 12:49 Comment(1)
I guess this was downvoted for using symlinks rather than .pth files. It worked for me though, so, whavever.Chummy
F
1
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.

Fpc answered 1/3, 2018 at 9:45 Comment(3)
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}"Furunculosis
Thanks for the comment. I revised for use. the old ways are cumbersome and idiosyncratic and there is nothing fancy about a simple templating systemFpc
Everything is relative :)Furunculosis
M
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.

Metrist answered 20/3, 2023 at 9:41 Comment(0)
C
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.

Cultigen answered 16/6, 2021 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.