Is there a way to install django with pip to point to a specific version of python in virtualenv

163 Views Asked by At

I have a system with CentOS installed. It currently runs python2.6, but python2.7 is also installed.

I want to run django 1.7, which is also currently installed. If I run django outside of a virtualenv, it is using python2.6 by default. I didn't install it myself.

What I assume is a way to get around this is to create a virtualenv. Which I've done, and used --python=python2.7. But when I create the virtualenv, and install a new django 1.7 in it (with pip), it still uses python2.6 instead of 2.7.

Since I'm doing this all through ssh, I'd like an easy way around it (rather than compiling from source, etc). Is there a way to specify that django uses python2.7 when I install it with pip in the virtualenv? Or what is the right way to correct this issue?

Here is what I have done:

ssh into account.

$ mkdir project; cd project
$ virtualenv env --python=python2.7
$ cd env
$ source bin/activate
$ sudo easy_install-2.7 pip
$ pip install django==1.7

Then I go into my python interpreter. The interpreter is running 2.7 and if I import django, it all works okay. But as soon as I run

django-admin.py startproject project_name

it is back to using 2.6.

2

There are 2 best solutions below

1
On BEST ANSWER

Invoke django-admin.py like this python django-admin.py, in your activate virtualenv. Alternatively you can do /path/to/virtualenv/bin/python django-admin.py. The best solution is probably adding a shebang to django-admin.py that looks like #!/usr/bin/env python which should use the python interpreter of your active virtualenv. See https://stackoverflow.com/a/2255961/639054

0
On

You could use pyenv, which will allow you to set your Python interpreter based on what directory you're in. I also use it with pyenv-virtualenv so that I get all the benefits of isolating my packages per application.

There's a bit of a learning curve, but it's worth it if you need to switch Python interpreters to match a production server for instance.