No module named django but it is installed

126,012

Solution 1

Probably, pip installs packages into dist-packages directory, which is not included into PYTHONPATH environment variable. You have a couple of solutions:

  1. Create and configure virtualenv for your project, before using pip. This is the most Pythonic way
  2. Try to install Django using built-in pip module:

    python -m pip install django
    

    This command should install packages into site-packages directory.

  3. You may also add dist-packages to your PYTHONPATH. This question should help you: How to globally modify the default PYTHONPATH (sys.path)?

Solution 2

This error shows that Django is not installed. Installing Django should solve the problem.

In my case, Django was there in my virtualenv but while using gunicorn I was getting this error then later I realized gunicorn was dealing with my globally install python environment not my virtual environment installing Django on my global python env simply solved my issue.

pip install django

Solution 3

I got this error when using

python manage.py runserver #python version 3 was being used

Solved the problem by using:

python2 manage.py runserver #python version 2

Solution 4

You need to close the other active virtual environment in your machine if you're using virtualenv, and make sure django installed. Go to python3 interpreter and run this:

>>>from django import get_version
>>>get_version()

make sure it show you this '2.1.4'

Solution 5

I had given the dependency in requirements.txt as Django==2.0.7. So after activating the virtual environment, if you are receiving the message, try installing the requirements:

pip install -r requirements.txt
Share:
126,012
srk
Author by

srk

Updated on July 09, 2022

Comments

  • srk
    srk almost 2 years

    I have two versions of python 2.7 and 3.4 and installed django through pip. it shows in ubuntu terminal:

    $ pip freeze
    Django==1.6.11
    $ pip --version
    pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)
    $ python
    Python 2.7.9 (default, Feb  3 2016, 02:50:32) 
    [GCC 4.8.4] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>import django
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named django
    >>> import sys
    >>> sys.path
    ['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']
    >>> 
    

    Any idea??

  • gaspar
    gaspar over 7 years
    This solved my problem with "No Module named django.core". Thanks!
  • awesoon
    awesoon over 6 years
    Note, that starting with django 2.0 you cannot use Python2: docs.djangoproject.com/en/1.11/faq/install/…
  • Bennett Garner
    Bennett Garner over 5 years
    Thanks! #1 didn't work (hence why I came to this question) and #2 fixed it.
  • alamin
    alamin almost 5 years
    it is something to do with version... but still you dont have to downgrade it. i am running python3.7 with django 2.2.2 and inside a virtualenv.
  • DanGoodrick
    DanGoodrick over 2 years
    I too was using python2 when I thought I was using 3. python3 -m pip install django fixed it.
  • AOJ
    AOJ over 2 years
    Thank you both for asking and answering the question!! I was going crazy with this same issue.
  • JayB
    JayB about 2 years
    This solved it for me.Thanks!