简体   繁体   English

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

[英]Django - “no module named django.core.management”

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?关于如何解决这个问题的任何想法?

It sounds like you do not have django installed.听起来您没有安装 django。 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.看看你那里是否有 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.可能您安装了多个版本的 python,而 django 在另一个版本中。 You can find out all the versions of python if you type python and then press TAB.如果你输入python然后按TAB,你可以找到所有版本的python。 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.您可以对每个版本的 python 执行上述命令,并查看每个版本的 site-packages 目录,看看其中是否有安装了 django。 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 中找到了 django,请尝试使用原始命令

python2.6 manage.py ...
sudo pip install django --upgrade 

帮我解决了这个问题。

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

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

so replace所以更换

/usr/bin/python

with

~/projectpath/venv/bin/python

and you should be good.你应该很好。

well, I faced the same error today after installing virtualenv and django.好吧,我今天在安装 virtualenv 和 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 .对我来说,我使用 sudo ( sudo pip install django ) 来安装 django,我试图在没有sudo 的情况下运行manage.py runserver I just added sudo and it worked.我刚刚添加了 sudo 并且它起作用了。 :) :)

Are you using a Virtual Environment with Virtual Wrapper?您是否使用带有 Virtual Wrapper 的虚拟环境? Are you on a Mac?你在 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注意(来自新手) - 不要在您的环境名称周围加上括号

I am having the same problem while running the command-我在运行命令时遇到了同样的问题 -

python manage.py startapp < app_name > 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 -但我的问题是我在虚拟环境中运行该命令。所以只需先激活您的虚拟环境,然后再次运行该命令 -

This problem occurs when django is not installed on your computer.当您的计算机上未安装 django 时会出现此问题。 When django is not installed which means django.core.management module is also is not installed.当未安装 django 时,这意味着 django.core.management 模块也未安装。 So it didn't find this module and it gives error.所以它没有找到这个模块,它给出了错误。
For solving this problem we should install django using pip.为了解决这个问题,我们应该使用 pip 安装 django。 Open comand line cmd(on windows) and type as打开命令行 cmd(在 Windows 上)并键入

pip install django

This command will install django in your computer.此命令将在您的计算机中安装 django。 If you don't have install pip.如果你没有安装pip。 you should install pip.你应该安装pip。 Here how to install pip on windows这里如何在 Windows 上安装 pip

I experience the same thing and this is what I do.我经历了同样的事情,这就是我所做的。

First my installation of首先我的安装

pip install -r requirements.txt 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 pip install -r requirements.txt

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.希望这可以帮助。

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

This also happens if you change the directory structure of your python project (I did this, and then puzzled over the change in behavior).如果您更改 python 项目的目录结构,也会发生这种情况(我这样做了,然后对行为的变化感到困惑)。 If you do so, you'll need to change a line in your /bin/activate file.如果这样做,则需要更改 /bin/activate 文件中的一行。 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在重新激活之前

In my case, I am using Ubuntu.就我而言,我使用的是 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.您只需在sudo前添加sudo即可,它应该可以完美运行。 In my case sudo python manage.py syncdb .在我的情况下sudo python manage.py syncdb

我有同样的问题,我收到这条消息的原因是因为我在做“manage.py runserver”而做“python manage.py runserver”修复了它。

My case I used pyCharm 5 on mac.我的情况我在 mac 上使用了 pyCharm 5。 I also had this problem and after running this command my problem was solved我也有这个问题,运行这个命令后我的问题就解决了

sudo pip install django --upgrade 

我遇到了同样的问题并且以下效果很好,您应该在项目中导航主文件夹而不是键入:

source bin/activate 

had the same problem.run command 'python manage.py migrate' as root.有同样的问题。以 root 身份运行命令“python manage.py migrate”。 works fine with root access (sudo python manage.py migrate )使用 root 访问可以正常工作(sudo python manage.py migrate )

You can try it like so : python3 manage.py migrate (make sur to be in the src/ directory)你可以这样尝试: python3 manage.py migrate (使 sur 位于src/目录中)

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您也可以尝试使用pip install -r requirements.txt (确保在迁移后键入ls时看到requirements.txt文件

If after all it still won't work try pip install django如果毕竟它仍然不起作用,请尝试pip install django

Hope it helps希望能帮助到你

I got the same problem trying to use the python manage.py runserver .我在尝试使用python manage.py runserver 时遇到了同样的问题。 In my case I just use sudo su .就我而言,我只使用sudo su Use the terminal as a root and try it again an it works partially.使用终端作为 root 并再次尝试它部分工作。 So I use python manage.py migrate comand and it fix it.所以我使用python manage.py migrate命令并修复它。

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

It is because of virtual enviornment configuration.这是因为虚拟环境配置。 You need to work on your virtual enviornmnet of Python.您需要在 Python 的虚拟环境中工作。 You should try on your command promt with,你应该试试你的命令提示符,

workon virtual_enviornment_name

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

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

相关问题 Django:没有名为django.core.management的模块 - Django:No module named django.core.management 没有名为django.core.management的模块 - No module named django.core.management django模块是全局的-ImportError:没有名为django.core.management的模块吗? - Are django modules global - ImportError: No module named django.core.management? 重新启动计算机并得到:ImportError:没有名为django.core.management的模块 - restarted computer and got: ImportError: No module named django.core.management Python错误:没有名为django.core.management的模块 - Python Error: No module named django.core.management Django的manage.py提供“没有名为django.core.management的模块” - Django's manage.py gives “no module named django.core.management” Django项目运行服务器失败:ImportError:没有名为django.core.management的模块 - Django project runserver failed : ImportError: No module named django.core.management 在virtualenv中运行django-ImportError:没有名为django.core.management的模块-Windows 7 - Running django in virtualenv - ImportError: No module named django.core.management - Windows 7 python manage.py runserver没有名为django.core.management的模块 - python manage.py runserver No module named django.core.management docker-compose up --build产生ImportError:没有名为django.core.management的模块 - `docker-compose up --build` yields ImportError: No module named django.core.management
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM