Python Django - “没有名为 django.core.management 的模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14013728/
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-18 10:10:44  来源:igfitidea点击:

Django - "no module named django.core.management"

pythondjango

提问by Krasimir

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

尝试从命令行运行 Django 时出现以下错误。

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?

关于如何解决这个问题的任何想法?

回答by RaviU

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

听起来您没有安装 django。您应该检查此命令生成的目录:

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

To see if you have the django packages in there.

看看你那里是否有 django 包。

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

如果站点包中没有 django 文件夹,那么您没有安装 django(至少对于那个版本的 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 pythonand then press TAB. Here are all the different python's I have.

可能您安装了多个版本的 python,而 django 在另一个版本中。如果您键入python然后按TAB,您可以找到所有版本的python 。这是我拥有的所有不同的蟒蛇。

$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:

您可以对每个版本的 python 执行上述命令,并查看每个版本的 site-packages 目录,看看其中是否有安装了 django。例如:

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 中找到了 django,请尝试使用原始命令

python2.6 manage.py ...

回答by keesar

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:

文件和目录所有权冲突会在此处引起问题。确保项目下的目录和文件的所有权属于当前用户。(您可以使用带有 -R 选项的 chown 命令更改它们。)尝试重新运行该命令:这在运行“第一个 Django 应用程序”示例时为我解决了问题:

python manage.py startapp polls

回答by Anagha

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

回答by adamadam

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

如果您更改 python 项目的目录结构,也会发生这种情况(我这样做了,然后对行为的变化感到困惑)。如果这样做,则需要更改 /bin/activate 文件中的一行。所以,假设你的项目在

/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

在重新激活之前

回答by mgPePe

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

很可能在您manage.py的第一行开始时,!/usr/bin/python这意味着您使用的是系统全局 python 而不是虚拟环境中的 python。

so replace

所以更换

/usr/bin/python

with

~/projectpath/venv/bin/python

and you should be good.

你应该很好。

回答by Pete Varley

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

您是否使用带有 Virtual Wrapper 的虚拟环境?你在 Mac 上吗?

If so try this:

如果是这样试试这个:

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

在命令行中输入以下内容以启动虚拟环境,然后对其进行处理

1.)

1.)

source virtualenvwrapper.sh

or

或者

source /usr/local/bin/virtualenvwrapper.sh

2.)

2.)

workon [environment name]

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

注意(来自新手) - 不要在您的环境名称周围加上括号

回答by Ashish

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.

您已经创建了一个虚拟环境并且 django 模块只属于该环境。由于 virtualenv 将自己与其他一切隔离,因此您会看到这一点。

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/

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.

1.你可以切换到你的虚拟环境所在的目录,然后运行django模块。

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

2. 或者,您可以通过运行 pip 或 easy_install 将 django 全局安装到您的 python->site-packages

Command using pip: pip install django

使用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)

import django print (django.get_version()) (取决于您使用的python版本。这适用于python 3+系列)

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.

然后你可以运行这个:python manage.py runserver 并通过键入 :localhost:8000 检查你的网络浏览器,你应该看到 django 驱动的页面。

Hope this helps.

希望这可以帮助。

回答by user1578297

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 runserverwithout sudo. I just added sudo and it worked. :)

好吧,我今天在安装 virtualenv 和 django 后遇到了同样的错误。对我来说,我使用 sudo ( sudo pip install django) 来安装 django,我试图在没有sudo 的情况下运行manage.py runserver。我刚刚添加了 sudo 并且它起作用了。:)

回答by sk8asd123

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.

如果这对其他人有帮助......我遇到了这个问题,因为我的 virtualenv 默认为 python2.7 并且我在使用 Ubuntu 时使用 Python3 调用 Django。

to check which python my virtualenv was using:

检查我的 virtualenv 正在使用哪个 python:

$ which python3
>> /usr/bin/python3

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

创建了指定 python3 的新 virtualenv(使用 virtualenv 包装器https://virtualenvwrapper.readthedocs.org/en/latest/):

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

the python path should now point to the virtualenv python:

python 路径现在应该指向 virtualenv python:

$ which python3
>> /home/user/.virtualenvs/ENV_NAME/bin/python3

回答by sk8asd123

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 sudobefore your command and it should work perfectly. In my case sudo python manage.py syncdb.

就我而言,我使用的是 Ubuntu。问题可能是我没有以普通用户身份写入该文件夹的权限。您可以简单地sudo在您的命令之前添加它,它应该可以完美运行。就我而言sudo python manage.py syncdb