简体   繁体   English

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

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

I'm trying to run python manage.py runserver on a Django application I have and I get this error:我正在尝试在我拥有的 Django 应用程序上运行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
ImportError: No module named django.core.management

Here is the output of pip freeze | grep -i django这是pip freeze | grep -i django的输出pip freeze | grep -i django pip freeze | grep -i django to 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 runserver yields the same error.此外,尝试运行/usr/local/bin/python2.7 manage.py runserver产生相同的错误。

Possible issues that may cause your problem:可能导致您的问题的可能问题:

  1. PYTHONPATH is not well configured, to configure it you should do: PYTHONPATH 没有很好地配置,要配置它你应该这样做:

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

  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)如果您正在使用 virtualenv,您忘记激活虚拟环境来执行 manage.py 命令(您可能已经在系统上安装了 Django,但没有在您的 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你有 Python 2.7 和 Python 3.4 弄乱了包

  5. You're using a very old Python 2.4 and you should tell the system to use your Python 2.7 with:您使用的是非常旧的 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:您可以在其他类似问题中找到其他一些解决方案:

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,但找不到,执行上述操作解决了问题。

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.因此,您可以尝试的是,如果您使用的是 python 2.7,那么只需将 3.4 替换为 2.7。 Hope this helps.希望这可以帮助。

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

If you use virtualenv and run manage.py runserver in windows system, cmd will use system's python, not the python in virtualenv.如果你在windows系统中使用virtualenv并运行manage.py runserver ,cmd将使用系统的python,而不是virtualenv中的python。 Because If you install python in system, cmd will automatically use python that installed in system, not the python in virtualenv .因为如果您安装python系统,CMD会自动使用python是安装在系统中,而不是在蟒蛇virtualenv So if you use virtualenv in Windows.因此,如果您在 Windows 中使用virtualenv you need run你需要跑

>python .\manage.py runserver

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

The direct problem is that django package is missing.直接的问题是缺少 django 包。 For me, as I was running django in virtualenv, this problem occurs after I rename my working directory.对我来说,当我在 virtualenv 中运行 django 时,在我重命名我的工作目录后会出现这个问题。

Reinstalling the env worked for me.重新安装环境对我有用。

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+, python here may refers python2 here.如果你使用python 3+,这里的python可能指的是这里的python2。 So check which python you install django with.因此,请检查您使用哪个 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

Also, make sure you did not mess up your .bashrc with alias es.另外,请确保您没有使用alias es 弄乱您的.bashrc You would get this kind of errors if .bashrc contains something like alias python="/usr/bin/python3" .如果.bashrc包含类似alias python="/usr/bin/python3"您将收到此类错误。

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.我使用的是 64 位版本的 python。 Uninstalling, and installing the 32-bit version, then re-creating my venv solved this for me.卸载并安装 32 位版本,然后重新创建我的 venv 为我解决了这个问题。

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!当我运行./manage.py runserver一切正常!

Using Pycharm go to Configurations add runserver 8080 to parameters, or I fixed my issue by restarting my computer....strange but worked.使用 Pycharm 转到配置将runserver 8080添加到参数,或者我通过重新启动计算机解决了我的问题......奇怪但有效。 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.我不必更改 pip 或安装其他任何东西,首先尝试我列出的第一种方法,如果您可以通过 PyCharm 箭头图标运行 manage.py,它将起作用,如果您想返回终端并键入 python manage.py runserver:port 尝试重新启动您的机器。

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重新安装 Django 并升级 pip 消除了错误,现在当我运行python migrate.py runserver时一切都按预期工作

Here are the CLI commands I used:以下是我使用的 CLI 命令:

python -m pip install django python -m pip 安装 django

python -m pip install --upgrade pip python -m pip install --upgrade pip

我通过在我正在处理的虚拟环境中安装 django 解决了这个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django的manage.py提供“没有名为django.core.management的模块” - Django's manage.py gives “no module named django.core.management” python manage.py runserver没有名为django.core.management的模块 - python manage.py runserver No module named django.core.management 找不到django.core.management,但是运行manage.py时django存在 - django.core.management not found but django exists when running manage.py 文件“manage.py”,第 9 行,在<module> from django.core.management import execute_from_command_line ModuleNotFoundError: No module named &#39;django&#39; - ile "manage.py", line 9, in <module> from django.core.management import execute_from_command_line ModuleNotFoundError: No module named 'django' django模块是全局的-ImportError:没有名为django.core.management的模块吗? - Are django modules global - ImportError: No module named django.core.management? manage.py - 导入错误:没有名为 django 的模块 - manage.py - ImportError: No module named django 重新启动计算机并得到:ImportError:没有名为django.core.management的模块 - restarted computer and got: ImportError: No module named django.core.management Django:没有名为django.core.management的模块 - Django:No module named django.core.management Django - “没有名为 django.core.management 的模块” - Django - “no module named django.core.management” 没有名为django.core.management的模块 - No module named django.core.management
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM