How do I add a path to PYTHONPATH in virtualenv
Question:
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?
Answers:
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. libpython2.7site-packages
, with the absolute path to the directory containing your package as its only contents.
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 …
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.
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.
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
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.
Was having trouble installing Box2d in a virtual environment using Python 3.9.
Simply ran a pip install swig
and then was able to successfully run pip install box2d
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.
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?
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. libpython2.7site-packages
, with the absolute path to the directory containing your package as its only contents.
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 …
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.
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.
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
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.
Was having trouble installing Box2d in a virtual environment using Python 3.9.
Simply ran a pip install swig
and then was able to successfully run pip install box2d
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.