How can I use Django 1.9.6 within my virtual environment?

Asked

Viewed 106 times

2

I’m trying to use version 1.9.6 of Django in the virtual environment 'myvenv' that I created through the Ubuntu terminal, but even after having done the installation through the command pip3 install django==1.9.6, when using 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 as it turns pip3 freeze within the virtual environment I see "Django==1.9.6" among the installed packages and yet I get such an error.

P.S.: if, within the virtual environment, use sudo python manage.py runserver I get no error.

  • 1

    I’m assuming you were within virtual environment when you installed with Pip, right? (i.e. first you used the command source, then you used the command pip3 to install Django)

  • Tried to install Django by apt-get? I usually use the distro’s own installer, I’ve avoided a lot of headaches with it

  • @Brumazzidb The problem is that the virtual environment uses its own site-packages independent of the global environment (and depending on how virtualenv was created, it does not even inherit the globally installed packages). So install via apt-get I don’t think that’s a good idea in that context.

  • In fact, it makes no sense to use the installation of Django via apt-get - the idea of virtualenv’s is to just avoid the problems that this brings (To begin with, you would have to have an operating system on your server that would have to have exactly the same version of Django as your development s.o. - and so on for all Python dependencies)

  • 1

    @mgibsonbr yes, was already inside the virtual environment

  • Right... I think I should check the version of python being called, many linux distros have python 2.x by default, if you try python3 manage.py runserver, can solve

  • @Brumazzidb was the first thing I tried when error appeared kkk. I tried everything but so far nothing solved =/

Show 2 more comments

1 answer

1


Dude, at least you’re running some command from outside the virtual environment or you’re creating a virtual environment with version 2 of Python (there pip3 will match your system’s Pip and not your environment’s).

I will pass the complete recipe of how to do what you want, so eliminates the possibility of doubts.

First of all it’s good to make it clear that you nay you must install Django with packages from your system. Both @jsbueno and @mgibsonbr and all existing literature on Jango will tell you to create your application using a virtual environment.

First of all you will need the virtualenv. This I let you use 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 real, whatever.

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 virtualenv version according to the Python version 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 inside your project folder:

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 explicit the version can only do:

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.