How can I use Django 1.9.6 within my virtual environment?

2

I'm trying to use django version 1.9.6 in the virtual environment 'myvenv' I created through the ubuntu terminal, but even after I installed it using the pip3 install django==1.9.6 command, when I use python manage.py runserver I get the following error :

Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named 'django'

Funny that when I run pip3 freeze within the virtual environment I see "Django == 1.9.6" from the installed packages and I still get that error.

Q: If, within the virtual environment, I use sudo python manage.py runserver I do not get any errors.

    
asked by anonymous 16.05.2016 / 20:16

1 answer

1

At least you are running some command from outside the virtual environment or you are creating a virtual environment with version 2 of Python (there pip3 will correspond to the pip of your system and not to that of your environment).

I'll give you the full recipe of how to do what you want, so it eliminates the possibility of doubts.

First of all, it's good to make it clear that you should not install django with the packages on your system. Both @jsbueno and @mgibsonbr and all the existing literature on django will tell you to create your application using a virtual environment.

First you will need virtualenv . This I let you use the one from the system repository. Install the version according to the version of Python you want to use if you want to make your life easier, but in fact, it does.

With it installed, create a folder for your project and enter it:

mkdir super_project
cd super_project

Create a virtual environment:

virtualenv venv

Again, use the version of virtualenv according to the version of Python that you want. However, nothing prevents you from telling virtualenv which version of Python you want to take as a basis:

virtualenv venv -p /usr/bin/python3

Regardless of the way, the two commands will create a folder called venv within the folder of your project:

super_project
└── venv

Now comes the most important step, activate the virtual environment:

source venv/bin/activate

Or simply:

. venv/bin/activate

Now you can install whatever you want:

pip install django==1.9.6

Since 1.9.6 is the latest stable version of django today, you do not need to make the version explicit by just doing:

pip install django

With django installed, you can start developing your application:

django-admin startproject project
cd project
python manage.py runserver

And that's it.

    
17.05.2016 / 19:41