17

I installed virtualenv on my Ubuntu 10.04 server.

Now when I do pip freeze it shows me the following packages:

Django==1.2.4
distribute==0.6.10
python-apt==0.7.94.2ubuntu6.2
virtualenv==1.5.1
wsgiref==0.1.2

When I do "pip uninstall Django" it says:

pip uninstall Django
Uninstalling Django:
Proceed (y/n)? y
Successfully uninstalled Django

Ideally this should uninstall Django but it doesn't. I can still see the same packages when I do "pip freeze".

Now bear with me, the other weird thing is that when I create a virtualenv and then do "pip freeze" inside it, I see only one package and that is "wsgiref" which is weird because it should ideally be blank.

Also, in spite of creating this virtualenv with --no-site-packages I can still create a new django project with "django-admin.py startproject".

When I start python interpreter inside this virtualenv and do "import django" it gives me the error "No module named django".

Also when I try to install "pip install Django" inside the virtualenv it asks for sudo permissions which shouldn't happen ideally.

How do I sort out this mess. Any way to just reset everything pep and virtualenv?

Day
  • 9,465
  • 6
  • 57
  • 93
Sushi
  • 631
  • 1
  • 8
  • 19
  • You probably installed django with apt? (if so it should be in dist-packages) You can run django-admin.py because it's in the PATH. – ionelmc Feb 06 '11 at 13:34
  • No it's not. When I do "dpkg -l" I don't see django anywhere. Also when I try to "sudo apt-get remove django" it says "Couldn't find package django. Same for python-django. – Sushi Feb 06 '11 at 13:38
  • @Sushi Does the interpreter, started from outside a virtualenv, still gives an error when trying `import django`? – Reiner Gerecke Feb 06 '11 at 13:48
  • I don't get any error when I try "import error" from outside virtualenv. Also when I try django.VERSION it shows "(1, 2, 4, 'final', 0)". – Sushi Feb 06 '11 at 13:53
  • 2
    @Sushi if `import django` works outside a virtualenv, do `import django;django.__path__`. This will show the path where django is still installed. – Reiner Gerecke Feb 06 '11 at 16:06
  • @Reiner Gerecke: I get this now ['/usr/local/lib/python2.6/dist-packages/django'] – Sushi Feb 06 '11 at 19:16
  • @Sushi That most certainly means you still have Django installed with your distribution, like ionelmc assumed before, even though dpkg and apt-get disagree. – Reiner Gerecke Feb 06 '11 at 19:20
  • @Reiner Gerecke: Yeah django is very much there. The trouble is it has spread to virtualenv as well where it shouldn't go. I am just removing the global django now to keep it sane. Thanks for your help all along. – Sushi Feb 06 '11 at 19:57
  • 2
    For future, `toggle-site-packages` will turn off stuff that's not explicitly in your virtualenv, so if a package is installed globally, you can disable it/enable it using this command. – David Boshton Nov 26 '14 at 11:46
  • @Sushi, Accept the answer which worked for you. –  Oct 19 '21 at 15:15

3 Answers3

7

As far as I can tell, the only purpose of a venv is to manage dependencies.

You should be safe to just deactivate the venv, delete it, and create a new one using virtualenv venv; source venv/bin/activate.

This will give you a fresh start.

Brad Johnson
  • 1,776
  • 1
  • 20
  • 26
3

This question is old, but it got bumped to the front page, so it's hard to discern which versions of pip and virtualenv you are using.

There are a few things we can do to straighten this, nonetheless.

  1. Exit all virtualenvs, and check your $PYTHONPATH. Make sure it's empty; else run unset PYTHONPATH.
  2. Create a fresh virtualenv at myenv using virtualenv --no-site-packages myenv.
  3. Activate myenv and check your $PYTHONPATH again. It should only have myenv.
  4. Run pip with an absolute path, like so myenv/bin/pip freeze.
  5. If pip is not available inside your virtualenv, you may have to install it manually.

Related: virtualenv --no-site-packages and pip still finding global packages?

Finally, start a python shell using myenv/bin/python, then run:

>>> import sys
>>> sys.path

If pip can find wsgiref, then wsgiref must be in one of the paths in sys.path. Use that clue!

Rico
  • 58,485
  • 12
  • 111
  • 141
James Lim
  • 12,915
  • 4
  • 40
  • 65
2

You can just delete your .venv file to remove all dependencies and then run python3 -m venv .venv for a fresh virtual environment.

TacoEater
  • 2,115
  • 20
  • 22