Import Error: No module named django
Asked Answered
T

9

34

I am using centos linux.

I had python 2.6 with django and now i upgraded to python 2.7.
Python 2.6 is located in /usr/lib/python2.6.
Python 2.7 is located in /usr/local/lib/python2.7.
They both have site-packages directory and they both contain django 1.2.

If i run python i get the 2.7 version.
My problem is that if try to import django i get

ImportError: No module named django

I am not sure where is my PYTHONPATH defined and if this is what i need to change. anyone ?

i ended up making a symbolic link to the 2.6 site-packages directory.

Translucent answered 27/2, 2012 at 8:48 Comment(0)
H
19

To check your path, you can use the following code:

import sys     
print(sys.path)

If you already know where django is installed, it should be easy to test if the desired directory is in your path with directory in sys.path.

Regarding where your PYTHONPATH is defined, note that it's an environment variable, so you can check its value (if defined) with: echo $PYTHONPATH

Hyrcania answered 27/2, 2012 at 8:53 Comment(4)
the direcroty is not in the path but the parent direcotory - '/usr/local/lib/python2.7/site-packages' is in the path, how do i add a directory to that path ?Translucent
PYTHONPATH is not defined, so where are all the directories that are in the sys.path are defined ?Translucent
In that case, python should be able to find django. Did you install it maybe as an egg?Hyrcania
Aside from PYTHONPATH, the contents of sys.path is populated using a value that depends on the installation. Related question.Hyrcania
C
57

I had the same error, and this fix my issue

python -m pip install django

:) Done!

Correll answered 30/10, 2017 at 12:58 Comment(3)
This worked for me - care to elaborate the difference on running this, as opposed to pip install django?Simplex
This is the correct answer, you helped me. Thank you.Darbee
I did this, and it says Requirement already satisfied. I got ModuleNotFoundError: No module named 'address' after I changed the location of my django folder.Louvar
H
19

To check your path, you can use the following code:

import sys     
print(sys.path)

If you already know where django is installed, it should be easy to test if the desired directory is in your path with directory in sys.path.

Regarding where your PYTHONPATH is defined, note that it's an environment variable, so you can check its value (if defined) with: echo $PYTHONPATH

Hyrcania answered 27/2, 2012 at 8:53 Comment(4)
the direcroty is not in the path but the parent direcotory - '/usr/local/lib/python2.7/site-packages' is in the path, how do i add a directory to that path ?Translucent
PYTHONPATH is not defined, so where are all the directories that are in the sys.path are defined ?Translucent
In that case, python should be able to find django. Did you install it maybe as an egg?Hyrcania
Aside from PYTHONPATH, the contents of sys.path is populated using a value that depends on the installation. Related question.Hyrcania
D
16

Under linux, you can set the PYTHONPATH environment variable in your .profile or .bashrc. You can either edit it directly from the terminal by changing to your home directory (cd ~), and then edit the file (nano .bashrc), or by opening the file with gtkedit or vim or whatever, and add:

PYTHONPATH=/usr/local/lib/python2.7/site-packages:/another/path/etc

If you want to test this before editing your profile, you can export this from the terminal as:

export PYTHONPATH=/local/lib/python2.7/site-packages

I'm assuming you're running this straight from the command line. If you're running it as a wsgi module in apache, you can add this to your syspath from your wsgi file as:

import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
Dupre answered 27/2, 2012 at 9:8 Comment(2)
I think the separator should be a ":" instead of a ";"Avast
@YannSagon Thanks - fixed above. Semi-colon would be for windows, but this was for centos, which would use a colon.Dupre
B
13

try

pip freeze

this command show which packages are installed in your system then run with root privilege

pip install django

then create a new project with command

django-admin.py startproject mysite

then start your project

cd path/to/mysite
./manage.py runserver 

in file wsgi.py add this lines

import os
import sys
DJANGO_PATH =  os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)
Bashemath answered 30/4, 2016 at 8:25 Comment(0)
L
11

Try printing sys.path to see what's in your path. Django need to be in one of the dirs listed. Example on Windows:

>>> import sys
>>> for p in sys.path: print p

C:\Python27\Lib\idlelib
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
>>> 
Labrum answered 27/2, 2012 at 8:56 Comment(1)
how do i add a directory to this list ? if i use .append it does not presistTranslucent
L
3

django went missing with an upgrade to python 3.7

pip3 install django

fixed the problem.

Lochia answered 7/8, 2020 at 15:10 Comment(0)
V
1

If you are using a environment use:

$ <environment_location>/<environment_name>/bin/python manage.py runserver
Videlicet answered 9/6, 2020 at 19:44 Comment(0)
A
1

python3 -m django --version1

for me it was that^

Ashbey answered 21/3, 2021 at 22:34 Comment(0)
B
0

I also had same error but easily solved it .those who are using version 4 and above of Django and python 3.0 can do the following(for windows)

pip install virtualenv #installs virtual environment in pc

py -m venv myvenv #creates virtual environment named myvenv inside folder

myvenv/Scripts/activate # activates the virtual environment

You are now ready to go.

pip install django # pip install django

python -m django --version # checks the version of installed django

Byrle answered 11/9, 2022 at 13:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.