[Answer]-Python console error: ImportError: No module named 'django'

-1👍

Turns out the problem was permissions earlier in the setup.

It leads to Django being unable to complete it’s install. pip install django would fail with a permissions error on a mkdir during the Cleaning up... segment of the install. And while all the files may have been installed, I suppose it wasn’t completely configged.

sudo pip install django is no better because the django install now has su permissions which helps with the mkdir problem above, while potentially creating other problems.

After both installs, the python console import django would error.

The fix: the virtual env dirs need to be owned by the user that invoked the virtual envs.

sudo chown -R youruser:youruser /opt/myenv

In my case, puppet created the venv and dirs as the root user.

👤monsto

2👍

I suspect here is where you went wrong:

  1. You created a virtual environment (virtualenv --python=/usr/bin/python3.4)
  2. Next, you forgot to activate it; or you activated it and then you did sudo pip install django.

sudo pip will run the system wide pip, installing django for Python 2.7.8, rather than for the virtual environment.

If you exit the virtual environment and try to import django – it will work.

To fix the problem, make sure you activate the virtual environment and then do not use sudo to install packages.

0👍

Before installing/using django you should activate your virtualenv. And do not sudo while you call pip:

$ source ~/.virtualenvs/myenv/bin/activate
$ pip install django

Leave a comment