Python 导入错误:使用 manage.py 时没有名为 django.core.management 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30389771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:21:59  来源:igfitidea点击:

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

pythondjangopython-2.7importerrordjango-manage.py

提问by Zach

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

我正在尝试python manage.py runserver在我拥有的 Django 应用程序上运行,但出现此错误:

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 djangoto show I do in fact have Django installed:

这是pip freeze | grep -i django显示我确实安装了 Django的输出:

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 runserveryields the same error.

此外,尝试运行会/usr/local/bin/python2.7 manage.py runserver产生相同的错误。

采纳答案by Zach

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

为了解决我的问题,我在 .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.

我试图导入 Django,但找不到,执行上述操作解决了问题。

回答by AlvaroAV

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 pythonat 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
    
  1. PYTHONPATH 没有很好地配置,要配置它你应该这样做:

    export PYTHONPATH=/usr/local/lib/python2.7/site-packages
    
  2. 你忘记了#!/usr/bin/env pythonmanage.py 开头的那一行

  3. 如果您正在使用 virtualenv,您忘记激活虚拟环境来执行 manage.py 命令(您可能已经在系统上安装了 Django,但没有在您的 virtualenv 上安装)

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

    或者

    workon env_name
    
  4. 你有 Python 2.7 和 Python 3.4 弄乱了包

  5. 您使用的是非常旧的 Python 2.4,您应该告诉系统将您的 Python 2.7 用于:

    alias python=python2.7
    

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

有时重新安装/升级 Django 可以解决其中的一些问题。

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

检查 Django 是否安装在您的 PC 或您的 virtualenv(如果您正在使用)上

You can find some other solutions in other similar questions:

您可以在其他类似问题中找到其他一些解决方案:

回答by JeanDez

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.

该错误是由于用户环境造成的。事实上,我只是将美德环境 bin 路径添加到 PATH 中,问题已经解决。

PATH=/bin/of_virtualenvironment/:$PATH

回答by zenwraight

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.

并且上面的命令为我成功执行。因此,您可以尝试的是,如果您使用的是 python 2.7,那么只需将 3.4 替换为 2.7。希望这可以帮助。

回答by Mona Jalal

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

当我使用 Python3 完成所有设置并运行 django 但运行以下命令时,我遇到了同样的问题:

$ python manage.py migrate

fixed the issue by using a consistent version of Python:

通过使用一致版本的 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

回答by Izana

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

在 django 文件所在的位置,检查 django-admin.py 的第一行:

#!/usr/bin/env python

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

如果你使用python 3+,python这里可能指的是这里的python2。因此,请检查您使用哪个 python 安装 django。

ls -l $(which -a python3)

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

如果您确实安装了 python3 并且没有链接为python,请将第一行 shebang 更改为

#!/usr/bin/env python3

回答by SolessChong

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.

直接的问题是缺少 django 包。对我来说,当我在 virtualenv 中运行 django 时,在我重命名我的工作目录后会出现这个问题。

Reinstalling the env worked for me.

重新安装环境对我有用。

回答by prusya

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

肯定还有,让你没惹你.bashrcaliasES。如果.bashrc包含类似alias python="/usr/bin/python3".

回答by Ming

If you use virtualenvand run manage.py runserverin windows system, cmd will use system's python, not the python in virtualenv. Because If you install pythonin system, cmd will automatically use pythonthat installed in system, not the python in virtualenv. So if you use virtualenvin Windows. you need run

如果你在windows系统下使用virtualenv和运行manage.py runserver,cmd会使用系统的python,而不是virtualenv中的python。因为如果你python在系统中安装,cmd 会自动使用python系统中安装的,而不是virtualenv. 所以如果你virtualenv在 Windows 中使用。你需要跑

>python .\manage.py runserver

回答by noodles

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.

我发现我可以从 python 解释器中导入 django 模块,但是从命令行运行时 django-admin.py 无法导入它。

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

我确认我在我的虚拟环境中使用了 python 解释器。

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.

我使用的是 64 位版本的 python。卸载并安装 32 位版本,然后重新创建我的 venv 为我解决了这个问题。