107

I get the following error when trying to run Django from the command line.

File manage.py, line 8, in <module>
     from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

Any ideas on how to solve this?

Jon Clements
  • 124,071
  • 31
  • 219
  • 256
Krasimir
  • 1,726
  • 2
  • 17
  • 30
  • 1
    What exactly are you trying to run, what version of Python, version of Django etc...? – Jon Clements Dec 23 '12 at 18:57
  • The version of my local host is Python 2.6.6, and 1.4.3 Dyango. Python server is 2.6.6 and for Dyango can not check if know how to check? – Krasimir Dec 23 '12 at 19:10
  • I got this error randomly while trying to make migrations and I was in my virtual environment with Django installed. I simply ran the command again and it worked. – aalaap Jan 21 '19 at 10:34
  • I have this same error. I have django installed but I it is installed in the dist-packages directory. The Dockerfile uses the ubuntu base image. So I updated the Dockerfile to add this `ENV PYTHONPATH="$PYTHONPATH:/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages"` but it still not loading the django app. – alltej Sep 04 '19 at 02:31

22 Answers22

56

It sounds like you do not have django installed. You should check the directory produced by this command:

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

To see if you have the django packages in there.

If there's no django folder inside of site-packages, then you do not have django installed (at least for that version of python).

It is possible you have more than one version of python installed and django is inside of another version. You can find out all the versions of python if you type python and then press TAB. Here are all the different python's I have.

$python
python            python2-config    python2.6         python2.7-config  pythonw2.5
python-config     python2.5         python2.6-config  pythonw           pythonw2.6
python2           python2.5-config  python2.7         pythonw2          pythonw2.7

You can do the above command for each version of python and look inside the site-packages directory of each to see if any of them have django installed. For example:

python2.5 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
python2.6 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

If you happen to find django inside of say python2.6, try your original command with

python2.6 manage.py ...
RaviU
  • 1,183
  • 13
  • 17
  • This is what comes right after the command "/usr/lib/python2.6/site-packages" – Krasimir Dec 23 '12 at 19:05
  • Go into the folder "/usr/lib/python2.6/site-packages" and see if there's a folder called django inside. – RaviU Dec 23 '12 at 19:10
  • Evrything else not Django :D – Krasimir Dec 23 '12 at 19:13
  • Hi, please check my edited answer. Maybe your django is in another version of python. – RaviU Dec 23 '12 at 19:17
  • I checked but could not find other versions of python – Krasimir Dec 23 '12 at 19:24
  • Then it's safe to say your host has not installed django. :) If my answer was helpful, please +1. Thanks. – RaviU Dec 23 '12 at 19:29
  • Let them have already reported problems. I reached on +1 reputation as otherwise I would have given you +1 :) – Krasimir Dec 23 '12 at 19:31
  • Here's what I replied to the host company Hello These frameworks only work for fastcgi, the rules you set them in. htaccess file, then documentation frameworks were you use. – Krasimir Dec 23 '12 at 19:52
  • 1
    If you installed django using virtualenv or while on a virtual environment, you have to be in the virtual environment for it to run, unless you have installed it in the system globally. – Ramon Suarez Feb 16 '13 at 22:03
43
sudo pip install django --upgrade 

did the trick for me.

Code Tree
  • 1,822
  • 3
  • 21
  • 33
  • 4
    Your answer worked for me, so I gave it an upvote. However, just your answer alone wasn't enough, because when I ran `sudo pip install django --upgrade` it gave me a new error: `No module named psycopg2`. So, I ran `sudo pip install psycopg2 --upgrade`, and it did the trick. – Dorian Dore Apr 17 '15 at 05:00
  • 2
    Turned out I didn't have django installed...Even though I'd used it before on the machine. Weird. – James111 Dec 11 '15 at 08:41
  • I had a timeout issue so I did `sudo pip install --default-timeout=100 django --upgrade` and it all went smoothly. – Zap Apr 12 '19 at 14:07
  • If you are using "pyenv" you would need the following : `pip install django --upgrade --user` – Inyoka May 23 '19 at 01:25
30

I got the same error and I fixed it in this manner:

I had to activate my virtual environment using the following command

source python2.7/bin/activate
Anagha
  • 660
  • 1
  • 8
  • 15
22

Most probably in your manage.py the first line starts with !/usr/bin/python which means you are using the system global python rather than the one in your virtual environment.

so replace

/usr/bin/python

with

~/projectpath/venv/bin/python

and you should be good.

mgPePe
  • 5,205
  • 12
  • 46
  • 78
  • Thanks. It helped. – Serg Smyk Dec 06 '19 at 16:32
  • My project had a bash script that called one portion of the app, and in that bash script was ```python startapp``` so I had to change it to ```/path/to/venv/bin/python startapp``` and it worked. Thank you for pointing this out!! – Gordster Jul 07 '20 at 02:02
13

well, I faced the same error today after installing virtualenv and django. For me it was that I had used sudo (sudo pip install django) for installing django, and I was trying to run the manage.py runserver without sudo. I just added sudo and it worked. :)

user1578297
  • 149
  • 1
  • 2
10

Are you using a Virtual Environment with Virtual Wrapper? Are you on a Mac?

If so try this:

Enter the following into your command line to start up the virtual environment and then work on it

1.)

source virtualenvwrapper.sh

or

source /usr/local/bin/virtualenvwrapper.sh

2.)

workon [environment name]

Note (from a newbie) - do not put brackets around your environment name

Pete Varley
  • 412
  • 5
  • 11
  • It took me like two seconds to figure out that virtualenv was not enabled, but this was my problem...so upvote since i hit google the google button once and didn't read anything that came up. – RobotHumans Nov 20 '13 at 03:29
7

I am having the same problem while running the command-

python manage.py startapp < app_name >

but problem with me is that i was running that command out of virtual environment.So just activate your virtual environment first and run the command again -

Rahul Satal
  • 1,403
  • 2
  • 21
  • 44
  • 1
    My project had a bash script that called one portion of the app, and in that bash script was ```python startapp``` so I had to change it to ```/path/to/venv/bin/python startapp``` and it worked. – Gordster Jul 07 '20 at 02:01
5

This problem occurs when django is not installed on your computer. When django is not installed which means django.core.management module is also is not installed. So it didn't find this module and it gives error.
For solving this problem we should install django using pip. Open comand line cmd(on windows) and type as

pip install django

This command will install django in your computer. If you don't have install pip. you should install pip. Here how to install pip on windows

Community
  • 1
  • 1
Harun ERGUL
  • 4,996
  • 5
  • 47
  • 55
5

I experience the same thing and this is what I do.

First my installation of

pip install -r requirements.txt

is not on my active environment. So I did is activate my environment then run again the

pip install -r requirements.txt

Jek Hatulan
  • 139
  • 1
  • 6
4

Okay so it goes like this:

You have created a virtual environment and django module belongs to that environment only.Since virtualenv isolates itself from everything else,hence you are seeing this.

go through this for further assistance:

http://www.swegler.com/becky/blog/2011/08/27/python-django-mysql-on-windows-7-part-i-getting-started/

1.You can switch to the directory where your virtual environment is stored and then run the django module.

2.Alternatively you can install django globally to your python->site-packages by either running pip or easy_install

Command using pip: pip install django

then do this:

import django print (django.get_version()) (depending on which version of python you use.This for python 3+ series)

and then you can run this: python manage.py runserver and check on your web browser by typing :localhost:8000 and you should see django powered page.

Hope this helps.

Ashish
  • 96
  • 3
3

In case this is helpful to others... I had this issue because my virtualenv defaulted to python2.7 and I was calling Django using Python3 while using Ubuntu.

to check which python my virtualenv was using:

$ which python3
>> /usr/bin/python3

created new virtualenv with python3 specified (using virtualenv wrapper https://virtualenvwrapper.readthedocs.org/en/latest/):

$ mkvirtualenv --python=/usr/bin/python3 ENV_NAME

the python path should now point to the virtualenv python:

$ which python3
>> /home/user/.virtualenvs/ENV_NAME/bin/python3
sk8asd123
  • 1,465
  • 15
  • 13
2

This also happens if you change the directory structure of your python project (I did this, and then puzzled over the change in behavior). If you do so, you'll need to change a line in your /bin/activate file. So, say your project was at

/User/me/CodeProjects/coolApp/

and your activate file is at

/User/me/CodeProjects/coolApp/venv/bin/activate

when you set up your project, then you changed your project to

/User/me/CodeProjects/v1-coolApp/

or something. You would then need to open

/User/me/CodeProjects/v1-coolApp/venv/bin/activate

find the line where it says

VIRTUAL_ENV="/User/me/CodeProjects/coolApp"
export VIRTUAL_ENV

and change it to

VIRTUAL_ENV="/User/me/CodeProjects/v1-coolApp"

before reactivating

chue x
  • 17,865
  • 6
  • 52
  • 67
adamadam
  • 48
  • 5
2

In my case, I am using Ubuntu. The problem can be that I don't have the permission to write to that folder as a normal user. You can simply add the sudo before your command and it should work perfectly. In my case sudo python manage.py syncdb.

Exhausted
  • 1,827
  • 2
  • 23
  • 33
2

I had the same issue and the reason I was getting this message was because I was doing "manage.py runserver" whereas doing "python manage.py runserver" fixed it.

MAhsan
  • 103
  • 1
  • 3
  • 2
    Note that if Python 3 is used it needs to be `python3 manage.py runserver`. Otherwise import error will still be there. – rbaleksandar Jan 29 '16 at 14:09
2

My case I used pyCharm 5 on mac. I also had this problem and after running this command my problem was solved

sudo pip install django --upgrade 
Amir
  • 8,255
  • 5
  • 42
  • 47
2

I had the same problem and following worked good, you should navigate main folder in your project than type:

source bin/activate 
Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80
GoldFish
  • 87
  • 8
1

had the same problem.run command 'python manage.py migrate' as root. works fine with root access (sudo python manage.py migrate )

Hemanth Gowda
  • 436
  • 3
  • 15
1

You can try it like so : python3 manage.py migrate (make sur to be in the src/ directory)

You can also try with pip install -r requirements.txt (make sur you see the requirements.txt file when you type ls after the migrate

If after all it still won't work try pip install django

Hope it helps

0

I got the same problem trying to use the python manage.py runserver. In my case I just use sudo su. Use the terminal as a root and try it again an it works partially. So I use python manage.py migrate comand and it fix it.

Tatarin
  • 1,111
  • 8
  • 28
0

You must choose your Project first before running the server , type this workon your_project_name then python manage.py runserver

0

It is because of virtual enviornment configuration. You need to work on your virtual enviornmnet of Python. You should try on your command promt with,

workon virtual_enviornment_name
Damini Suthar
  • 798
  • 6
  • 30
-1

File and Directory ownership conflict will cause issues here. Make sure the ownership of the directories and files under the project are to the current user. (You can change them using the chown command with the -R option.) Try rerunning the command: this solved the problem for me when running through the "First Django App" sample:

python manage.py startapp polls
keesar
  • 1