问题:Django-“没有名为django.core.management的模块”

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

File manage.py, line 8, in <module>
     from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

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

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

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?


回答 0

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

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

看那里是否有django软件包。

如果站点软件包中没有django文件夹,则说明您没有安装django(至少对于该版本的python)。

可能您安装了多个版本的python,而django位于另一版本中。如果键入python然后按TAB键,则可以找出python的所有版本。这是我拥有的所有不同的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

您可以对每个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()"

如果您碰巧在python2.6中找到django,请尝试使用以下原始命令

python2.6 manage.py ...

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

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

To see if you have the django packages in there.

If there’s no django folder inside of site-packages, then you do not have django installed (at least for that version of 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 python and then press TAB. Here are all the different python’s I have.

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

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 manage.py ...

回答 1

sudo pip install django --upgrade 

为我做了把戏。

sudo pip install django --upgrade 

did the trick for me.


回答 2

我遇到了相同的错误,并以这种方式修复了该错误:

我必须使用以下命令激活我的虚拟环境

source python2.7/bin/activate

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

回答 3

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

所以更换

/usr/bin/python

~/projectpath/venv/bin/python

你应该很好

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

so replace

/usr/bin/python

with

~/projectpath/venv/bin/python

and you should be good.


回答 4

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

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


回答 5

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

如果是这样,请尝试以下操作:

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

1.)

source virtualenvwrapper.sh

要么

source /usr/local/bin/virtualenvwrapper.sh

2.)

workon [environment name]

注意(来自新手)-请勿在您的环境名称前使用方括号

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

If so try this:

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

1.)

source virtualenvwrapper.sh

or

source /usr/local/bin/virtualenvwrapper.sh

2.)

workon [environment name]

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


回答 6

我在执行命令时遇到同样的问题-

python manage.py startapp <应用程序名称>

但是我的问题是我在虚拟环境之外运行了该命令。因此,请先激活您的虚拟环境,然后再次运行该命令-

I am having the same problem while running the command-

python manage.py startapp < app_name >

but problem with me is that i was running that command out of virtual environment.So just activate your virtual environment first and run the command again –


回答 7

当您的计算机上未安装django时,会发生此问题。如果未安装django,则意味着也未安装django.core.management模块。因此,它找不到此模块,并给出了错误。
为了解决这个问题,我们应该使用pip安装django。打开命令行cmd(在Windows上)并输入

pip install django

此命令将在您的计算机上安装django。如果您没有安装pip。你应该安装点子。这里如何在Windows上安装pip

This problem occurs when django is not installed on your computer. When django is not installed which means django.core.management module is also is not installed. So it didn’t find this module and it gives error.
For solving this problem we should install django using pip. Open comand line cmd(on windows) and type as

pip install django

This command will install django in your computer. If you don’t have install pip. you should install pip. Here how to install pip on windows


回答 8

我遇到同样的事情,这就是我所做的。

首先我的安装

点安装-r requirements.txt

不在我的活动环境中。所以我要做的是激活我的环境,然后再次运行

点安装-r requirements.txt

I experience the same thing and this is what I do.

First my installation of

pip install -r requirements.txt

is not on my active environment. So I did is activate my environment then run again the

pip install -r requirements.txt


回答 9

好的,它像这样:

您已经创建了一个虚拟环境,而django模块仅属于该环境。由于virtualenv会将其自身与其他所有事物隔离开,因此您会看到这一点。

进行以下操作以获得进一步的帮助:

http://www.swegler.com/becky/blog/2011/08/27/python-django-mysql-on-windows-7-part-i-getting-started/

1.您可以切换到虚拟环境的存储目录,然后运行django模块。

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

使用pip的命令:pip install django

然后这样做:

导入django打印(django.get_version())(取决于您使用的python版本。这适用于python 3+系列)

然后您可以运行以下命令:python manage.py runserver并通过输入:localhost:8000在Web浏览器上进行检查,您应该看到django驱动的页面。

希望这可以帮助。

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.

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/

1.You can switch to the directory where your virtual environment is stored and then run the django module.

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

Command using 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)

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.

Hope this helps.


回答 10

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

检查我的virtualenv使用的是哪个python:

$ which python3
>> /usr/bin/python3

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

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

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

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

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.

to check which python my virtualenv was using:

$ which python3
>> /usr/bin/python3

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

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

the python path should now point to the virtualenv python:

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

回答 11

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

/User/me/CodeProjects/coolApp/

并且您的激活文件位于

/User/me/CodeProjects/coolApp/venv/bin/activate

设置项目时,将项目更改为

/User/me/CodeProjects/v1-coolApp/

或者其他的东西。然后,您需要打开

/User/me/CodeProjects/v1-coolApp/venv/bin/activate

找到它说的那一行

VIRTUAL_ENV="/User/me/CodeProjects/coolApp"
export VIRTUAL_ENV

并将其更改为

VIRTUAL_ENV="/User/me/CodeProjects/v1-coolApp"

重新激活之前

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

/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


回答 12

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

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


回答 13

我有同样的问题,收到此消息的原因是因为我正在执行“ manage.py runserver”,而在执行“ python manage.py runserver”时已将其修复。

I had the same issue and the reason I was getting this message was because I was doing “manage.py runserver” whereas doing “python manage.py runserver” fixed it.


回答 14

我遇到了同样的问题,下面的工作很好,您应该在项目中浏览主文件夹,而不要键入:

source bin/activate 

I had the same problem and following worked good, you should navigate main folder in your project than type:

source bin/activate 

回答 15

具有相同的问题。以root身份运行命令’python manage.py migration’。可以与root访问一起正常工作(sudo python manage.py migrate)

had the same problem.run command ‘python manage.py migrate’ as root. works fine with root access (sudo python manage.py migrate )


回答 16

我的情况是在Mac上使用pyCharm 5。我也遇到这个问题,运行此命令后,我的问题解决了

sudo pip install django --upgrade 

My case I used pyCharm 5 on mac. I also had this problem and after running this command my problem was solved

sudo pip install django --upgrade 

回答 17

您可以这样尝试:(python3 manage.py migrate使sur位于src/目录中)

您也可以尝试使用pip install -r requirements.txt(使您在迁移后输入requirements.txt时看到文件ls

如果毕竟还是不行,请尝试 pip install django

希望能帮助到你

You can try it like so : python3 manage.py migrate (make sur to be in the src/ directory)

You can also try with pip install -r requirements.txt (make sur you see the requirements.txt file when you type ls after the migrate

If after all it still won’t work try pip install django

Hope it helps


回答 18

您必须先选择项目,workon your_project_name 然后再运行服务器,然后 键入 python manage.py runserver

You must choose your Project first before running the server , type this workon your_project_name then python manage.py runserver


回答 19

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

python manage.py startapp polls

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:

python manage.py startapp polls

回答 20

我在尝试使用python manage.py runserver时遇到了同样的问题。就我而言,我只使用sudo su。将终端机用作根用户,然后重试一次,可以部分使用。因此,我使用 python manage.py migration comand进行了修复。

I got the same problem trying to use the python manage.py runserver. In my case I just use sudo su. Use the terminal as a root and try it again an it works partially. So I use python manage.py migrate comand and it fix it.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。