Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I automatically change my PYTHONPATH when activating/deactivating a virtualenv?

I would like to have a different PYTHONPATH from my usual in a particular virtualenv. How do I set this up automatically? I realize that it's possible to hack the bin/activate file, is there a better/more standard way?

like image 700
saffsd Avatar asked Jun 09 '09 11:06

saffsd


People also ask

How to deactivate virtual environment?

You can deactivate a virtual environment by typing deactivate in your shell. The exact mechanism is platform-specific and is an internal implementation detail (typically, a script or shell function will be used).

How do I add Pythonpath to virtualenv?

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.

What is Pythonpath in virtual environment?

PYTHONPATH — This environment variable tells the current python interpreter where it can look for additional libraries that you want to be able to import that may not be in the standard site-packages, or if you're going to add a separate site-packages directory.


1 Answers

This django-users post is probably going to help you a lot. It suggests using virtualenvwrapper to wrap virtualenv, to use the add2virtualenv command. Using this, when the environment is active, you can just call:

add2virtualenv directory1 directory2 ...

to add the directories to your pythonpath for the current environment.

It handles autonomously the PATH changes on environment switches. No black magic required. Et voila!

like image 144
Nicolas Dumazet Avatar answered Sep 23 '22 08:09

Nicolas Dumazet