TIMEX
TIMEX

Reputation: 272214

How do you set your pythonpath in an already-created virtualenv?

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

Upvotes: 151

Views: 250373

Answers (8)

samdesigncodes
samdesigncodes

Reputation: 1

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

Upvotes: 0

anon
anon

Reputation: 11

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.

Upvotes: 1

Arjen P. De Vries
Arjen P. De Vries

Reputation: 1164

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.

Upvotes: 93

mdeous
mdeous

Reputation: 18049

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"

Upvotes: 166

Siyaram Malav
Siyaram Malav

Reputation: 4718

  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

Upvotes: 4

tjb
tjb

Reputation: 11728

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

Upvotes: 79

André Laszlo
André Laszlo

Reputation: 15537

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"

Upvotes: 1

Ravikiran
Ravikiran

Reputation: 1458

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") 

Upvotes: -22

Related Questions