ImportError: No module named django.core.managemen

2019-02-02 04:45发布

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.

12条回答
叛逆
2楼-- · 2019-02-02 05:04

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".

查看更多
时光不老,我们不散
3楼-- · 2019-02-02 05:05

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
查看更多
仙女界的扛把子
4楼-- · 2019-02-02 05:06

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
查看更多
beautiful°
5楼-- · 2019-02-02 05:08

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!

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-02 05:09

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.

查看更多
在下西门庆
7楼-- · 2019-02-02 05:15

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:

查看更多
登录 后发表回答