[Django]-ImportError when using virtualenv in Django

4👍

This happens because you have installed django in the system-wide Python interpreter; possibly by doing something like sudo pip install django.

Once you create a virtual environment and activate it – it contains no packages. The concept of a virtual environment is that it allows you to install Python packages without affecting the global Python installation.

So once you activate a virtual environment, you have to install packages in that virtual environment; so you should pip install django (note: without sudo) once you activate the virtual environment; like this:

$ virtualenv sample_env
...
$ source sample_env/bin/activate
(sample_env) $ pip install django

A virtual environment is a virtual environment for Python; it does not control what the user is that is logged into the system.

Finally, as a general rule – you should not be using root for development purposes as doing so can easily compromise your system.

2👍

Yup i have same error while running Django. I also have enabled virtual environment. But I was still getting error.

Solution to this problem is using this command to install any python package
python -m pip install django

This will definitely solve your problem. As it solved mine.

👤rkdevs

Leave a comment