12

I'm on a new virtualenv, and trying to install django on it.

When I type, which django-admin.py I get back /usr/local/bin/django-admin.py which is not correct, it should be installing inside the .virtualenvs folder.

If I do, pip install django I get back:

Requirement already satisfied (use --upgrade to upgrade): django in /Library/Python/2.6/site-packages

If I do, pip uninstall django I get back:

Not uninstalling Django at /Library/Python/2.6/site-packages, outside environment /Users/username/.virtualenvs/envname/bin/..

How do I get pip to install and use django inside the virtualenv? No idea how to troubleshoot this.

will-hart
  • 3,742
  • 2
  • 38
  • 48
some1
  • 2,447
  • 8
  • 26
  • 23

2 Answers2

12

First, you should be setting up the virtualenv with --no-site-packages:

virtualenv --no-site-packages …/my-env/

Then you should make sure that you've activated it:

. …/my-env/bin/activate

Or, with virtualenv helper:

workon my-env

Then install Django with:

pip install django
potench
  • 3,802
  • 1
  • 28
  • 39
David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • I am already on the virtualenv. I did 'workon envname' and (envname) is on the command line. Maybe I setup the virtualenv without the "--no-site-packages" flag though? Would that cause this issue? – some1 Nov 02 '11 at 05:18
  • 3
    This could cause the issue, as you'll be seeing the global Django. If `which pip` shows `…/my-env/…/bin/pip`, though, you can just use `pip install django`, re-launch your shell (because your shell has already cached the wrong path to `django-admin.py`), re-activate and you should be good to go. – David Wolever Nov 02 '11 at 05:24
  • "which pip" is correct yes, it shows the correct virtualenvs folder. I relaunched the shell, did "pip install django" again, and got the same error as above though. Not sure what you mean by re-activate..? – some1 Nov 02 '11 at 05:28
  • 1
    D'oh, sorry, yes — it won't work because django is already installed globally. Try again with `--no-site-packages` and it should work. – David Wolever Nov 02 '11 at 05:33
  • No problem, thanks for your help. So essentially remove the virtualenv and redo it with the --no-site-packages flag then is the only way? I'll give that a shot and update in a few. – some1 Nov 02 '11 at 05:34
  • I removed the virtualenv (by deactivating it and doing "rmvirtualenv envname".) Then I redid it using the --no-site-packages flag. Then I relaunched the shell, and "pip install django" worked this time. Thanks! – some1 Nov 02 '11 at 05:39
  • 5
    Just a note. Sometime after these comments were posted, --no-site-packages became the default behavior for virtualenv. Yet I was still getting an issue. This may go without saying but I'm saying it nonetheless, for anyone who has the same issue as I did: make sure you don't put "sudo" in front of pip. Just do "pip install django", no sudo. Thanks everyone – floer32 Jul 28 '12 at 16:26
  • 1
    It was adding 'sudo' in front of the pip command that was breaking it for me. I logged in as root and ran without without 'sudo' and it worked just fine. – David Jones Nov 15 '12 at 08:39
4

Like David pointed out, you should tell virtualenv not to use packages outside your virtual environment (using the --no-site-packages flag). That's why "which django-admin.py" returns "/usr/local/bin/django-admin.py"

César
  • 9,939
  • 6
  • 53
  • 74
  • Yeah, I think he nailed that. How do I add the --no-site-packages flag once the virtualenv has already been setup though? Or do I have to remove it and recreate? – some1 Nov 02 '11 at 05:24
  • 3
    Yes, you need to create a new one: `virtualenv --no-site-packages myenv`. Try installing django again using pip: `pip install django` and let me know :) – César Nov 02 '11 at 05:28
  • 3
    Don't forget to do it inside your recently created virtual environment: `source myenv/bin/activate` – César Nov 02 '11 at 05:29
  • Thanks Cesar, got it working by removing and rebuilding it with the --no-site-packages flag. Have a good night! – some1 Nov 02 '11 at 05:40