Cerin
Cerin

Reputation: 64870

How to fix default Python path after installing Virtualenv

I installed virtualenv via sudo pip install virtualenv and created some environments. But now I'm finding my default "global" Python path is completely gone.

In a fresh terminal, I see output like:

user@localhost:~$ sudo pip install django
Requirement already satisfied (use --upgrade to upgrade): django in /usr/local/lib/python2.7/dist-packages
Cleaning up...
user@localhost:~$ ls /usr/local/lib/python2.7/dist-packages/django
bin   contrib  db        forms  __init__.py   middleware  template      test   views
conf  core     dispatch  http   __init__.pyc  shortcuts   templatetags  utils
user@localhost:~$ python
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named django
>>> 

What's going on here? How do I fix my global Python installation so that it can see its installed packages?

Upvotes: 0

Views: 3104

Answers (3)

Burhan Khalid
Burhan Khalid

Reputation: 174708

You should only install virtualenv using sudo, everything else should be done without sudo.

You should also use --no-site-packages, as this makes sure your environment doesn't have conflicting versions of packages.

The proper way to do this is:

$ sudo apt-get install python-virtualenv
$ virtualenv --no-site-packages django_env
$ source django_env/bin/activate
(django_env)$ pip install -U django

If you need anything from your global path, you should install it in your virtual environment. That way, when you freeze your environment you'll get only what is required for your application.

You can install yolk, which will list packages in your environment:

(django_env)$ pip install yolk
(django_env)$ yolk -l

Upvotes: 0

Cerin
Cerin

Reputation: 64870

This problem was my own fault. I had accidentally run virtualenv --no-site-packages . in my home directory, creating folders like ~/bin, ~/local, ~/lib, and Python was looking for its packages there, where there were none. Deleting these directories fixed the problem.

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 114068

just set the environmental variable

PYTHONPATH=path1;path2;etc;
echo $PYTHONPATH

and it should use the path you specify

Upvotes: 0

Related Questions