148

What file do I edit, and how? I created a virtual environment.

erjiang
  • 44,417
  • 10
  • 64
  • 100
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 14
    Use the [add2virtualenv](http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html#add2virtualenv) command. – s29 Jan 16 '13 at 03:47
  • check that it points to the right directories. If you don't have one already, click the New button and create it. –  Jan 21 '11 at 11:54
  • 9
    add2virtualenv is a `virtualenvwrapper` command. It is not part of `virtualenv`. – kojiro Aug 11 '17 at 20:49

8 Answers8

162

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"
Stabledog
  • 3,110
  • 2
  • 32
  • 43
mdeous
  • 17,513
  • 7
  • 56
  • 60
  • 2
    Will it also be removed on `deactivate`? – Ram Rachum Jan 25 '12 at 23:03
  • 1
    So this means that a newly created virtual enviromnent have the default `PYTHONPATH` (the global one) instead of the virtualenv local path? – ducin Nov 23 '13 at 19:37
  • 1
    @tkoomzaaskz `PYTHONPATH` is empty by default, it's just a way of augmenting the search paths available within `sys.path`. This method is only meant to have it modified for the time you're in a virtualenv. – mdeous Nov 25 '13 at 01:24
  • This approach requires `VirtualEnvWrapper` – Fydo Jan 23 '14 at 21:15
  • If you add this to your ~/.virtualenv/name_of_env/bin/activate file, it will load the pythonpath inside a virtualenv everytime you load it. – cbron Feb 10 '14 at 18:14
  • 7
    This worked very well for me, I am not using virtualenvwrapper. Only note is that I did not have such bin/postdeactivate script, rathere activate had a deactivate function. The line of code thus should be put there, being careful to wrap it in a if statement as the rest of the operations in the function. – Niccolò Oct 11 '14 at 03:01
  • I found that to get this working with `pyenv`, I needed to modify `~/.pyenv/plugins/pyenv-virtualenv/bin/pyenv-sh-activate` and `~/.pyenv/plugins/pyenv-virtualenv/bin/pyenv-sh-deactivate` rather than the virtualenv's activate and deactivate scripts. (which are called for all virtualenvs not just a specific one) – nedned Jan 24 '17 at 00:22
  • 2
    It seems odd that virtualenv doesn't do this by default. – Seth Oct 08 '17 at 00:11
  • One line fix: export PYTHONPATH="/the/path/you/want" – Kai Wang Jul 17 '18 at 15:03
  • To expand @Niccolò comment (for newbies like me), the `if` statement inside `deactivate` can be `if true; then... fi` – Rodrigo E. Principe Oct 16 '18 at 19:56
  • 3
    Edit #2 is a lie! :) The right answer is [@arjen-p-de-vries's one](https://stackoverflow.com/a/47184788/4228275) – Berriel Jan 23 '19 at 00:15
  • 1
    How to add this change for windows. I have an `activate.bat` file – Murtaza Haji Feb 24 '21 at 17:15
  • After looking a lot, that's what solved! Thanks – Leonardo Mar 22 '23 at 16:41
78

The comment by @s29 should be an answer:

One way to add a directory to the virtual environment is to install virtualenvwrapper (which is useful for many things) and then do

mkvirtualenv myenv
workon myenv
add2virtualenv . #for current directory
add2virtualenv ~/my/path

If you want to remove these path edit the file myenvhomedir/lib/python2.7/site-packages/_virtualenv_path_extensions.pth

Documentation on virtualenvwrapper can be found at http://virtualenvwrapper.readthedocs.org/en/latest/

Specific documentation on this feature can be found at http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html?highlight=add2virtualenv

arogachev
  • 33,150
  • 7
  • 114
  • 117
tjb
  • 11,480
  • 9
  • 70
  • 91
76

You can create a .pth file that contains the directory to search for, and place it in the {venv-root}/lib/{python-version}/site-packages directory. E.g.:

cd $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
echo /some/library/path > some-library.pth

The effect is the same as adding /some/library/path to sys.path, and remain local to the virtualenv setup.

Stabledog
  • 3,110
  • 2
  • 32
  • 43
  • 12
    I really like this approach; not nearly as fickle as adding lines to activate/deactivate and doesn't require using virtualenvwrapper. If you'd rather not invoke python to find the site-packages dir, it's at the fairly-predictable path `$VIRTUALENV_ROOT/lib/pythonX.Y/site-packages`. Docs on .pth files: https://docs.python.org/3.6/library/site.html – alecbz Jan 24 '18 at 16:13
  • 3
    I didn't want to type in an absolute path, because my workspaces move around my filesystem. And, my project path has spaces. So, I typed the slightly modified command: ```pushd "$(venv/bin/python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")" ...path/to/project/venv/lib/python3.7/site-packages ...path/to/project echo ../../../../src > my_project.pth popd``` Then I deactivated my virtualenv, and reactivated. I could now run project code that required knowing where my project directory was. – Jim DeLaHunt Feb 11 '19 at 04:01
  • 8
    This is the correct answer. Modifying vendor-provided script (`/bin/activate`) is a malpractice and installing 3rd party tools is inferior to using built-in way to achieve the same. – Red Mar 06 '19 at 08:30
  • 1
    Note for people trying to get this to work: you have to add an _existing_ filesystem path (it doesn't have to be a python module) or the path won't show up in `sys.path`. Quoting the docs: "For each of the distinct head-tail combinations, it sees if it refers to an existing directory, and if so, adds it to sys.path and also inspects the newly added path for configuration files." – Tom Jan 09 '21 at 00:40
  • But there is important difference between .pth and EXPORT approaches. EXPORT adds path to the beggining of the sys.path, but .pth adds to the end. – EzR1d3r Apr 23 '21 at 15:29
  • 2
    Great! As a minor comment, my 3.10 virtualenv says: `DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives` and `DeprecationWarning: The distutils.sysconfig module is deprecated, use sysconfig instead`. – nilo Jan 08 '22 at 18:06
  • @JimDeLaHunt, the path where you create the venv is hard coded in the activate script. So not sure how useful your dynamic approach really is. – The Fool Mar 31 '22 at 20:42
  • @nilo Yes, the proper way of doing it nowadays is using `sysconfig` as outlined in [this answer](https://stackoverflow.com/a/52638888/5984810). In a nutshell: `python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))"`. – otterrisk Nov 14 '22 at 09:20
4
  1. Initialize your virtualenv
cd venv

source bin/activate
  1. Just set or change your python path by entering command following:
export PYTHONPATH='/home/django/srmvenv/lib/python3.4'
  1. for checking python path enter in python:
   python

      \>\> import sys

      \>\> sys.path

Jeff Sloyer
  • 4,899
  • 1
  • 24
  • 48
Siyaram Malav
  • 4,414
  • 2
  • 31
  • 31
1

I modified my activate script to source the file .virtualenvrc, if it exists in the current directory, and to save/restore PYTHONPATH on activate/deactivate.

You can find the patched activate script here.. It's a drop-in replacement for the activate script created by virtualenv 1.11.6.

Then I added something like this to my .virtualenvrc:

export PYTHONPATH="${PYTHONPATH:+$PYTHONPATH:}/some/library/path"
André Laszlo
  • 15,169
  • 3
  • 63
  • 81
1

The answers here really confused me. I'm not sure if I'm missing something but I found pyvenv.cfg at venv/pyvenv.cfg the ENTIRE contents of that file was

home = C:\Users\User\.pyenv\pyenv-win\versions\3.10.7
include-system-site-packages = false
version = 3.10.7

all I did was change \User to \NewUser and that stopped the error No Python at 'C:\Users\User\.pyenv\pyenv-win\versions\3.9.6\python.exe'

But yeah, I wasted an hour on this when ctrl+shift+f solved it easily.

anon
  • 11
  • 1
  • Jumping between computers and storing my project in OneDrive caused an issue (differing usernames). Not a great solution but correcting the path here got me going. – Tyler Montney Mar 30 '23 at 16:41
  • In this case, I believe the intended solution is something like "make a new venv for every computer" ridiculous, I know, but that is modern computing for now. It's called a URL, not a LRL. – anon May 09 '23 at 02:02
0

The Simplest way I've seen to solving this is by doing the following,

The python virtual environment PYTHON path is set in the "pyvenv.cfg" which is in the virtual environment folder.

home = C:\Users\{USERNAME}\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0

include-system-site-packages = false version = 3.11

you can edit this to your desired python version. After I made the changes I had the same error I fixed this by creating a new "pyvenv.cfg" file with the new python path and replacing it with the existing one. after deactivate your virtual environment and activate

this should work. at least it did for me

-20

It's already answered here -> Is my virtual environment (python) causing my PYTHONPATH to break?

UNIX/LINUX

Add "export PYTHONPATH=/usr/local/lib/python2.0" this to ~/.bashrc file and source it by typing "source ~/.bashrc" OR ". ~/.bashrc".

WINDOWS XP

1) Go to the Control panel 2) Double click System 3) Go to the Advanced tab 4) Click on Environment Variables

In the System Variables window, check if you have a variable named PYTHONPATH. If you have one already, check that it points to the right directories. If you don't have one already, click the New button and create it.

PYTHON CODE

Alternatively, you can also do below your code:-

import sys
sys.path.append("/home/me/mypy") 
Community
  • 1
  • 1
Ravikiran
  • 1,440
  • 11
  • 15