ImportError: No module named django.core.management when using manage.py
Asked Answered
I

14

29

I'm trying to run python manage.py runserver on a Django application I have and I get this error:

Traceback (most recent call last):
File "manage.py", line 8, in <module>
 from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

Here is the output of pip freeze | grep -i django to show I do in fact have Django installed:

Django==1.6.5
django-cached-authentication-middleware==0.2.0
django-cors-headers==1.1.0
django-htmlmin==0.7.0
django-static-precompiler==0.9
djangorestframework==2.3.14

Also, trying to run /usr/local/bin/python2.7 manage.py runserver yields the same error.

Idellaidelle answered 22/5, 2015 at 6:28 Comment(3)
did u run the command from where manage.py is at?Sarcous
And how many versions of pythons are running on ur machine?Sarcous
are you using a virtual environment for this purpose? can you share the details of your settings file?Villanueva
I
10

To fix my problem I used the following line in my .zprofile:

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

I was trying to import Django and it couldn't be found, and doing the above solved the issue.

Idellaidelle answered 23/5, 2015 at 5:6 Comment(4)
in case an error is shown that 'export is not recognized as an internal or external command, operable program or batch file, change EXPORT to SET, i.e., set PYTHONPATH=/usr/local/lib/python2.7/site-packagesReiko
is there any way to configure the python path permanently? because I have to run the above command every time I open a cmd windowReiko
This worked perfectly for me. Only solution I've found so far that has worked.Astern
Did same but still got same error /django_project$ python manage.py runserver Traceback (most recent call last): File "manage.py", line 8, in <module> from django.core.management import execute_from_command_line ModuleNotFoundError: No module named 'django' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 14, in <module> ) from excNeela
U
31

Possible issues that may cause your problem:

  1. PYTHONPATH is not well configured, to configure it you should do:

    export PYTHONPATH=/usr/local/lib/python2.7/site-packages
    
  2. You forgot the line #!/usr/bin/env python at the beginning of manage.py

  3. If you're working on virtualenv you forgot to activate the virtual env to execute manage.py commands (You may have installed Django on your system but not on your virtualenv)

    source path/to/your/virtualenv/bin/activate
    

    or

    workon env_name
    
  4. You have Python 2.7 and Python 3.4 messing with the package

  5. You're using a very old Python 2.4 and you should tell the system to use your Python 2.7 with:

    alias python=python2.7
    

Some times reinstalling/upgrading Django fix some of those issues.

You may want to execute

python -c "import django; print(django.get_version())"

to check if Django is installed on your PC or your virtualenv if you're using one

You can find some other solutions in other similar questions:

Ureide answered 22/5, 2015 at 6:50 Comment(2)
Thanks for you answer. I'm not sure about how to set up my PYTHONPATH correctly. Can you give me more insight? Regarding the rest, I do have #2 correct, for #3 I'm not using a virtual environment, so that shouldn't be the problem, for #4 and #5 I have python running a shell with Python 2.7.6 so I think I'm fine there. Also, I've since reinstalled Django but I'm still having the issue.Idellaidelle
In my case it was number 3: I forgot to activate the virtual environment. A simple: source bin/activate, and solved :)Slotnick
I
10

To fix my problem I used the following line in my .zprofile:

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

I was trying to import Django and it couldn't be found, and doing the above solved the issue.

Idellaidelle answered 23/5, 2015 at 5:6 Comment(4)
in case an error is shown that 'export is not recognized as an internal or external command, operable program or batch file, change EXPORT to SET, i.e., set PYTHONPATH=/usr/local/lib/python2.7/site-packagesReiko
is there any way to configure the python path permanently? because I have to run the above command every time I open a cmd windowReiko
This worked perfectly for me. Only solution I've found so far that has worked.Astern
Did same but still got same error /django_project$ python manage.py runserver Traceback (most recent call last): File "manage.py", line 8, in <module> from django.core.management import execute_from_command_line ModuleNotFoundError: No module named 'django' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 14, in <module> ) from excNeela
P
4

I solved this same error by running the below command:

python3.4 manage.py runserver

And the above command successfully executed for me. So what you can try is, if you are using python 2.7 then just replace 3.4 with 2.7. Hope this helps.

Plush answered 20/4, 2016 at 22:12 Comment(1)
this one saved my day... After much struggling, I just changed python2.7 manage.py changepassword instead of python manage.py changepassword and voilá!Clement
T
3

I came across the same issue when I did all the setup and run for django with Python3 but ran the following command:

$ python manage.py migrate

fixed the issue by using a consistent version of Python:

$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: sessions, auth, admin, contenttypes
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying sessions.0001_initial... OK

Other commands I ran before this were:

python3 -m pip install django
django-admin startproject learning_site
python3 manage.py  runserver 0.0.0.0:8000
Thirlage answered 20/6, 2016 at 12:56 Comment(0)
C
2

If you use virtualenv and run manage.py runserver in windows system, cmd will use system's python, not the python in virtualenv. Because If you install python in system, cmd will automatically use python that installed in system, not the python in virtualenv. So if you use virtualenv in Windows. you need run

>python .\manage.py runserver
Conglomeration answered 29/10, 2016 at 5:58 Comment(0)
M
1

That error is due to the user environment. Indeed i just add the virtuel environment bin path to PATH and the problem has been solved.

PATH=/bin/of_virtualenvironment/:$PATH
Matrass answered 1/10, 2015 at 8:55 Comment(0)
R
1

The direct problem is that django package is missing. For me, as I was running django in virtualenv, this problem occurs after I rename my working directory.

Reinstalling the env worked for me.

Radiation answered 20/9, 2016 at 1:32 Comment(0)
E
0

in where your django file resides, check the first line of django-admin.py:

#!/usr/bin/env python

If you use python 3+, python here may refers python2 here. So check which python you install django with.

ls -l $(which -a python3)

If you do have python3 installed and not linked as python, change the first shebang line into

#!/usr/bin/env python3
Evaevacuant answered 25/7, 2016 at 18:4 Comment(1)
I think I misunderstand your question since you do use python manage.py to indicate the environment. Just check which python you install django with and use that python. You may have to change the PYTHONPATH or PATH to indicate which python to use.Evaevacuant
M
0

Also, make sure you did not mess up your .bashrc with aliases. You would get this kind of errors if .bashrc contains something like alias python="/usr/bin/python3".

Majuscule answered 30/9, 2016 at 14:41 Comment(0)
A
0

I found that I could import the django module from the python interpreter, but django-admin.py could not import it when run from the command line.

I confirmed that I was using the python interpreter in my virtual environment.

I was using a 64-bit version of python. Uninstalling, and installing the 32-bit version, then re-creating my venv solved this for me.

Acrylic answered 7/7, 2017 at 20:4 Comment(0)
L
0

Fixed the same problem in my project using only;

pip install -r requirements.txt

I didn't have to do anything else. When I ran./manage.py runserver everything worked!

Lipid answered 28/1, 2019 at 7:27 Comment(0)
N
0

Using Pycharm go to Configurations add runserver 8080 to parameters, or I fixed my issue by restarting my computer....strange but worked. I did not have to change pip or install anything else, first try the first method I listed, if you are fine with running manage.py through PyCharm arrow icon, it will work, if you want to go back to the terminal and type python manage.py runserver:port try restarting your machine.

Norvun answered 3/3, 2020 at 19:24 Comment(0)
C
0

Trying the solutions here didn't work for me. Reinstalling Django and upgrading pip removed the error and everything now works as expected when I run python migrate.py runserver

Here are the CLI commands I used:

python -m pip install django

python -m pip install --upgrade pip

Catharinecatharsis answered 27/7, 2020 at 6:43 Comment(0)
E
-1

i solved this problem by installing django inside the virtual enviromment that i was working on

Estriol answered 7/5, 2018 at 10:7 Comment(1)
This is quite vague. Could you explain a little bit what exactly you did and how and why that answers the question?Imprison

© 2022 - 2024 — McMap. All rights reserved.