django 导入错误 - 没有名为 core.management 的模块
好吧,我看到很多这样的错误。我已经尝试了我所知道的一切,但尚未弄清楚这一点。
我正在开发一个运行 python 2.5 和 Django 1.3 的开发服务器。解压 tar.gz 下载后,使用 python setup.py install 安装 Django 1.3。
一切正常,我很少需要运行 manage.py
但我尝试使用新的 staticfiles 应用程序并遇到了问题。
python manage.py collectstatic
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named core.management
好的,所以我有 PATH
问题。
从 Django install 我仔细检查了我的 site-packages
目录。
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
/usr/lib/python2.5/site-packages
好吧,让我们看看我有什么,echo $PYTHON_PATH 是空的,所以我设置了它
export PYTHON_PATH=/usr/lib/python2.5/site-packages/django
仍然没有运气。让我们检查 sys.path 所说的
>>> import sys
>>> print sys.path
['', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/PIL', '/usr/lib/python2.5/site-packages/django', '/var/lib/python-support/python2.5']
路径是否在那里,我什至创建了 /usr/lib/python2.5/site-packages/django.pth 的内容
cat /usr/lib/python2.5/site-packages/django.pth
/usr/lib/python2.5/site-packages/django/
有人知道这里发生了什么吗?
我在路径上发现了一个符号链接,但没有出现新的错误。
python manage.py collectstatic
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_manager(settings)
File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 349, in execute
version=get_version(),
File "/usr/lib/python2.5/site-packages/django/__init__.py", line 12, in get_version
from django.utils.version import get_svn_revision
ImportError: No module named utils.version
我还尝试创建一个新项目以查看是否存在任何问题并得到相同的 utils.version 错误。
侧面节点:#django 的 Unode 帮了我一点忙,在同一台机器上设置了 virtualenv 并克服了错误,所以仍然不确定这里的实际安装是怎么回事,但它似乎不在 django 项目中,而是在 django 中/python 安装。
Ok, I see plenty of these errors around. I have tried everything I know to do and have yet to figure this out.
I am working on a development server running python 2.5 and Django 1.3. Django 1.3 was installed using python setup.py install after unpacking the tar.gz download.
All works well, I seldom have the need to run manage.py
but am trying to use the new staticfiles app and am running into problems.
python manage.py collectstatic
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named core.management
Ok, so I have PATH
issue.
From Django install I double check my site-packages
directory.
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
/usr/lib/python2.5/site-packages
Ok, let's check out what I have, echo $PYTHON_PATH was empty, so I set it
export PYTHON_PATH=/usr/lib/python2.5/site-packages/django
Still no luck. Lets check what sys.path has to say
>>> import sys
>>> print sys.path
['', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/PIL', '/usr/lib/python2.5/site-packages/django', '/var/lib/python-support/python2.5']
path is there, I even created /usr/lib/python2.5/site-packages/django.pth with contents
cat /usr/lib/python2.5/site-packages/django.pth
/usr/lib/python2.5/site-packages/django/
Anyone got an clues to what is going on here?
I found a symlink further up the path that was getting in the way, but no on to a new error.
python manage.py collectstatic
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_manager(settings)
File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 349, in execute
version=get_version(),
File "/usr/lib/python2.5/site-packages/django/__init__.py", line 12, in get_version
from django.utils.version import get_svn_revision
ImportError: No module named utils.version
I also tried creating a new project to see if there were any issues there and get the same utils.version error.
Side node: Unode from #django helped me a bit, set up virtualenv on same machine and got past the errors so still not sure what is up with this actual install here, but it seems to not be in the django projects but in the django/python install.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
您可能正在使用virtualenvwrapper。不要忘记通过运行选择您的环境:
You are probably using virtualenvwrapper. Don't forget to select your enviroment by running:
嵌入式系统(当然使用 django)时遇到此错误
我在尝试在 Raspberry Pi 2(并且不是 VM)上运行 这:
成功了!
I had this error while trying to run an embedded system (using django of course) on a Raspberry Pi 2 (and not a VM)
Running this:
Made the trick!
检查Python版本
Check version of Python
对我来说,我的服务器使用的是 Python 2.4。我只是查找了安装在我的服务器上的 Python 2.7,并创建了一个别名。
如果您需要了解更多信息,我在此处找到了解决方案
For me, my server was using Python 2.4. I simply looked up Python 2.7, which was installed on my server, and created an alias.
If you need to know more, I found the solution here
当我尝试创建新应用程序时,我遇到了同样的问题。如果您编写 python manage.py startapp myapp,那么它会查找 usr/bin/python。但是您需要这个“python”,它位于虚拟环境路径的/bin目录中。我通过提及 virtualenv 的 python 路径解决了这个问题,如下所示:
I was getting the same problem while I trying to create a new app. If you write
python manage.py startapp myapp
, then it looks for usr/bin/python. But you need this "python" which is located in /bin directory of your virtual env path. I solved this by mentioning the virtualenv's python path just like this:解决了!!!
在搜索年龄并尝试了所有这些不起作用的其他建议后,我终于找到了适合我的设置的解决方案。
我的设置/场景:
解决方案
删除 同一目录中名为 django 的任何文件夹没错
...一旦我删除了仅包含 __init__.py 文件的文件夹“django”...我就可以再次运行服务器了!
不知道为什么
Solved it!!!
After searching for ages and trying all these other suggestions which didn't work, I finally found the solution for my setup.
My setup/scenario:
Solution
Remove any the folder named django in the same directory of manage.py
Thats right...as soon as I removed the folder "django" which only contained a __init__.py file...I could run the server again!
Have no idea why though
尝试更改manage.py 的第一行。
更改
由
Try change your first line of manage.py.
Change
by
===================================解决方案=============== ============================
首先
通过运行以下命令转到:virtualenv:
source bin/activate
并安装 django,因为您收到与“import django”相关的错误:
然后运行:(
注意:请将“runserver”更改为您要运行的程序名称)
对于同样的问题,它在我的情况下有效。
===================================剧情简介=============== ==========================
在处理上述异常期间,发生了另一个异常:
在处理上述异常期间,又发生了异常:
在处理期间除了上述异常之外,还发生了另一个异常:
安装 django 后:
解决后:
祝你好运!!
==================================SOLUTION=========================================
First goto: virtualenv
by running the command:
source bin/activate
and install django because you are getting the error related to 'import django':
Then run:
(Note: please change 'runserver' to the program name you want to run)
For the same issue, it worked in my case.
==================================Synopsis=========================================
During handling of the above exception, another exception occurred:
During handling of the above exception, another exception occurred:
During handling of the above exception, another exception occurred:
AFTER INSTALLATION of django:
AFTER RESOLVING:
Good luck!!
对于使用 Django 1.6 或更高版本的用户,请注意 execute_manager 已删除。第二个SO答案中发布了一个解决方案此处。
For those of you using Django 1.6 or newer, note that execute_manager was removed. There is a solution posted in the second SO answer here.
将 python python 路径存储在变量中并执行。这将包括否则丢失的包。
Store the python python path in a variable and execute.This would include the otherwise missing packages.
环境:
Python 3.9.6
、Django 3.2.6
、VS Code
只需禁用
Pylance
code> 扩展并重新加载 VS Code。environment:
Python 3.9.6
,Django 3.2.6
,VS Code
Just disable
Pylance
extension and reload your VS Code.我有类似的问题。 PyCharm 无法运行服务器,但我可以从命令行运行它。我尝试了哪个 python,然后确保 PyCharm 是相同的解释器,然后一切正常。
I had a similar problem. PyCharm couldn't run the server but I could run it from the command line. I tried which python and then made sure that PyCharm was same interpreter and then everything worked OK.
此错误通常是在未安装 django 时出现。
如果您已经安装了 django 但仍然遇到相同的错误,那么您必须在单独的虚拟环境中工作。您还需要在虚拟环境中安装 django。当你在虚拟机的 shell 中时,只需执行以下操作:
pip install django
这是因为虚拟机有单独的文件系统,即使它安装在你的系统上,它也无法识别 django。
This error usually occurs when django is not installed.
If you have already installed django but still getting the same error, then you must be working in separate virtual environment. You need to install django in your virtual environmnent as well. When you are in shell of virtual machine simply do this:
pip install django
It is because virtual machine has separate file system, it doesn't recognize django even if it is installed on your system.
我通过将 #PATH="$VIRTUAL_ENV/bin:$PATH" 更改为 PATH="$PATH:$VIRTUAL_ENV/bin" 解决了这个问题
由于对我来说不明显的原因,virtualenv 目录中的 python 可执行文件看不到 django,但正常安装的 python 可以看到。
I fixed this problem by changing #PATH="$VIRTUAL_ENV/bin:$PATH" to PATH="$PATH:$VIRTUAL_ENV/bin"
For reasons not obvious to me the python executable in the virtualenv dir does not see django but the normally installed python does.
只是一个错误
这也发生在我身上。我的错误是我在创建虚拟环境之前创建了 Django 项目,后来激活了环境并尝试启动服务器。只需在激活 env 后安装 Django 即可工作
Just a single mistake
This happened with me as well. My mistake was that I've created Django project before creating virtual env, later activated the env and was trying to start the server. Just install Django after activating env it will work
这是一个翻译错误。
如果您使用的是 vscode,则只需按照以下步骤操作:
python
此问题是由于路径更改而发生的。
That's an interpreter error.
If you are using vscode then just follow these steps:
python
This problem occurs due to path changed.
您的项目是使用旧版本的 django-admin.py 创建的,早于 django1.3
来修复此问题创建另一个 django 项目并复制其 manage.py 并将其粘贴到旧项目中
your project is created using an old version of django-admin.py, older than django1.3
to fix this create another django project and copy its manage.py and paste it in the old one
完全同意这是一个路径问题,但是fwiw,我也遇到了同样的错误。这是由于在设置虚拟环境时错误地使用了 Python 可执行文件的相对路径。我已经这样做了:
相反,我必须提供 Python 可执行文件的完整路径。
哈特哈,
哈林
Agreed completely that this is a path issue but fwiw, I had this same error. It was due to the mistake of using a relative path for my Python executable when setting up my virtual environment. I had done this:
Instead I had to give the full path to the Python executable.
HTH,
Harlin
source ~/blog-venv/bin/activate
选择您的 virtualenv 来替换此处的“blog-venv”。
source ~/blog-venv/bin/activate
pick your virtualenv to replace "blog-venv" here.
确保您正在运行正确的 Python 实例,并在路径上使用正确的目录。就我而言,此错误是由于意外运行
python
可执行文件造成的 - 我实际上是在python2.7
框架下安装了 Django图书馆。 virtualenv 也可能会发生同样的情况。Be sure you're running the right instance of Python with the right directories on the path. In my case, this error resulted from running the
python
executable by accident - I had actually installed Django under thepython2.7
framework & libraries. The same could happen as a result of virtualenv as well.好的,事情是这样的:
您已经创建了一个虚拟环境,并且 django 模块仅属于该环境。由于 virtualenv 将自身与其他一切隔离,因此您会看到这一点。
请通过此获取进一步帮助:
http://www.swegler.com/becky/blog/2011/08/27/python-django-mysql-on-windows-7-part-i-getting-started/< /a>
1.您可以切换到虚拟环境所在目录,然后运行django模块。
2.或者,您可以通过使用 pip 运行 pip 或 easy_install 命令,将 django 全局安装到您的 python->site-packages
: pip install django
然后执行以下操作:
import django
print (django.get_version()) (取决于您使用的 python 版本。这适用于 python 3+ 系列)
,然后您可以运行以下命令:
python 管理.py runserver
并通过输入 :localhost:8000 检查您的网络浏览器
你应该看到 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.
在发出 startapp 命令之前,我将新应用程序的名称添加到了 settings.py 中的 INSTALLED_APPS 列表中。删除列表条目后,我就可以创建应用程序。
I included the name of the new App to the INSTALLED_APPS list in the settings.py "before" I issued the startapp command. Once I removed the list entry, I could create the app.
我通过使用“django-admin”命令解决了这个问题,如下所示:
django-admin startproject _project_name
只需删除附加到“django-admin”的“.py”
I solved this problem by using 'django-admin' command as following instead:
django-admin startproject _project_name
just remove the ".py" attached to "django-admin"
使用名为
site
的应用程序也可以重现此问题。Having an application called
site
can reproduce this issue either.如果像我一样,您在 virtualenv 中运行 django,并收到此错误,请查看您的
manage.py
。第一行应该定义用于运行脚本的 python 可执行文件。
这应该是你的virtualenv的python的路径,但是它是错误的,比如/usr/bin/python,它不是相同的路径,并且将使用全局python环境(并且包将丢失)。
只需将路径更改为 virtualenv 中 python 可执行文件的路径即可。
您还可以用
#!/usr/bin/env python
替换 shebang 行。这应该使用正确的 python 环境和解释器,前提是您首先激活 virtualenv (我假设您知道如何执行此操作)。If, like me, you are running your django in a virtualenv, and getting this error, look at your
manage.py
.The first line should define the python executable used to run the script.
This should be the path to your virtualenv's python, but it is something wrong like /usr/bin/python, which is not the same path and will use the global python environment (and packages will be missing).
Just change the path into the path to the python executable in your virtualenv.
You can also replace your shebang line with
#!/usr/bin/env python
. This should use the proper python environment and interpreter provided that you activate your virtualenv first (I assume you know how to do this).如果您位于 virtualenv 中,则需要先激活它,然后才能运行 ./manage.py 'command'
如果您在 .bash_profile 或 .bashrc 中配置 workon
*请不要编辑您的 manage.py 文件,可能不是正确的方式,并且可能会给你带来未来的错误
If you are in a virtualenv you need to activate it before you can run ./manage.py 'command'
if you config workon in .bash_profile or .bashrc
*please dont edit your manage.py file maybe works by isnt the correct way and could give you future errors
我遇到了同样的问题,因为我是以超级用户身份安装 Django,因此不在我的 virtualenv 中。您不应该执行
sudo pip install Django
相反,请按以下方式安装:
I had the same problem because I was installing Django as a super user, thus not in my virtualenv. You should not do
sudo pip install Django
Instead, install it this way:
请使用 pip 重新安装 django:(
将 1.3 替换为您的 django 版本)
Please, reinstall django with pip:
(Replace 1.3 to your django version)
众所周知,这是一个路径问题。
我的自定义包的基础与 /etc/profile 中设置的目录共享一个名称。然而对于网络服务器来说,这些包位于不同的位置。因此,我从 $PYTHONPATH 中删除了有问题的条目,一切顺利!
感谢您的帮助。
As known this was a path issue.
the base of my custom packages shared a name with a directory set in a /etc/profile. The packages were in a different location however for the webserver. So I removed the offending entries from my $PYTHONPATH and was good to go!
Thanks for the help.
此问题的另一个可能原因是您的操作系统默认运行 python3。
您必须显式执行:
python2 manage.py
,或者您需要编辑
manage.py
的shebang,如下所示:或者如果您使用的是python3:
Another possible reason for this problem is that your OS runs python3 by default.
Either you have to explicitly do:
python2 manage.py
or you need to edit the shebang of
manage.py
, like so:or if you are using python3: