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.
To use the virtual environment you created to run Python scripts, simply invoke Python from the command line in the context where you activated it. For instance, to run a script, just run python myscript.py .
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"
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
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.
cd venv
source bin/activate
export PYTHONPATH='/home/django/srmvenv/lib/python3.4'
python
\>\> import sys
\>\> sys.path
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With