16

I've installed Django multiple ways, via apt-get and pip install. They all say I have the most recent version of Django. Now whenever I run python in Terminal and type in import django, I receive

ImportError: No module named django

However, when I run django-admin --version I receive

1.4.3

I know it's installed, but why is python not finding the django module?

2

6 Answers 6

8

python is not finding django because it's not on its path. You can see the list of paths python looks for modules like this:

$ python
>>> import sys
>>> sys.path

You can import django if you find the location it is installed, and add that location to python's path, for example like this:

$ PYTHONPATH=/path/to/django/parent/dir python
>>> import django  # should work now

But your real problem is that something's wrong with your python installation. If you have installed both python AND django using apt-get, then django should certainly be on python's path without dirty hacks like above.

That said, when working with Django, your best bet is to NOT use apt-get but create a virtual environment using virtualenv (you can install virtualenv itself using apt-get), and install Django and other modules your Django site might need using pip within the virtual environment. That way you can have multiple Django projects side by side, with precisely the Python modules and versions it requires. It's just a few extra steps to do, but definitely worth it and will save you from much frustration in the future.

3
  • You can also add to the path directly in your code by using sys.path.append('path/to/django'), then import as normal
    – NGRhodes
    Jun 6, 2014 at 7:40
  • @NGRhodes Hardcoding paths in code is not a good idea.
    – janos
    Jun 6, 2014 at 11:38
  • can always read the path from a configuration item so its a runtime setting :)
    – NGRhodes
    Jul 11, 2019 at 14:53
7

I had the same problem when I was using PyCharm Community Edition for making Django projects.

For me, the following steps worked:

It turns out that python wants you to create a virtual environment, install django in that and then run the server. To do this,

Create a Virtual Environment

1) Install virtual environment using pip install virtualenv.

2) Navigate to the project folder and type virtualenv env (Here env is the name of the virtual environment.) This will create a new folder named env inside the project folder.

3) Navigate to env/Scripts inside your Project Folder using cd env/Scripts.

4) Type activate and press Enter. This should start the virtual environment. You can verify this as (env) would be prefixed to your current path.

Install Django

1) Once inside the virtual environment, head back to your project folder using cd ../.. and type pip install django.

2) You can verify its installation by typing django-admin --version. It should display the django version number installed inside the virtual environment.

Now type python manage.py runserver to start the python server.

1

I faced the same problem. I activated the virtual environment

$ source bin/activate 

after that it showed me the version of Django

There was no need of installing anything.


mamata@mamta-inspire-all:~/dodjango$ python -m django --version
/usr/bin/python: No module named django

mamata@mamta-inspire-all:~/dodjango$ django-admin --version
Command 'django-admin' not found, but can be installed with:

sudo apt install python-django-common

mamata@mamta-inspire-all:~/dodjango$ source bin/activate

(dodjango) mamata@mamta-inspire-all:~/dodjango$ ls
bin  include  lib  lib64  mamusite  pyvenv.cfg  share

(dodjango) mamata@mamta-inspire-all:~/dodjango$ django-admin --version    
2.2.5

(dodjango) mamata@mamta-inspire-all:~/dodjango$ python -m django --version
2.2.5
1
  • Try to edit the answer with some help of AskUbuntu.
    – NIMISHAN
    Sep 25, 2019 at 5:04
0

If you have multiple python version installed into your machine. The different packages you are trying to install gets conflicted.So, the best approach is to use virtualenv..

  1. Simple install virtual environment and python version
  2. use virtualenv env to create virtualenvironment.
  3. activate your virtualenv. using . /env_name/bin/activate
  4. use pip install django==1.70 (specify version to install django)
  5. pip list to list all installed packages.
0

i have this problem and still had it till recently in my project vscode did not recognize my virtual environment , so when ever i try to run the project it gives me an error that vscode can not import django

the fix for this for me that i exported the libraries in my virtual environment to a fill

pip freeze > req.txt

then i created a new virtual environment with the same requirements , and it worked

note: try looking this up python virtual environment

-1

I've got this problem when I renamed the project folder in PyCharm.

  1. Terminal tab stopped getting into the Virtual Environment (venv folder). To fix that, I went to the Settings -> Project -> Project Interpreter and changed the folder there.
  2. Django "disappeared". django-admin --version did not respond. To fix, I did pip install django within the venv.

It helped.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .