django 导入错误 - 没有名为 core.management 的模块

发布于 2024-11-08 14:02:37 字数 2419 浏览 4 评论 0原文

好吧,我看到很多这样的错误。我已经尝试了我所知道的一切,但尚未弄清楚这一点。

我正在开发一个运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

∝单色的世界 2024-11-15 14:02:38

您可能正在使用virtualenvwrapper。不要忘记通过运行选择您的环境:

$ workon env_name

You are probably using virtualenvwrapper. Don't forget to select your enviroment by running:

$ workon env_name
对你的占有欲 2024-11-15 14:02:38

嵌入式系统(当然使用 django)时遇到此错误

我在尝试在 Raspberry Pi 2(并且不是 VM)上运行 这:

 sudo pip install Django

成功了!

  • 以防万一使用 Raspbian/Jessie 的人得到这个

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:

 sudo pip install Django

Made the trick!

  • just in case a fellow using Raspbian/Jessie gets this
聆听风音 2024-11-15 14:02:38
python3 manage.py runserver

检查Python版本

python3 manage.py runserver

Check version of Python

温柔戏命师 2024-11-15 14:02:38

对我来说,我的服务器使用的是 Python 2.4。我只是查找了安装在我的服务器上的 Python 2.7,并创建了一个别名。

alias python=python2.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.

alias python=python2.7

If you need to know more, I found the solution here

我做我的改变 2024-11-15 14:02:38

当我尝试创建新应用程序时,我遇到了同样的问题。如果您编写 python manage.py startapp myapp,那么它会查找 usr/bin/python。但是您需要这个“python”,它位于虚拟环境路径/bin目录中。我通过提及 virtualenv 的 python 路径解决了这个问题,如下所示:

<env path>/bin/python manage.py startapp myapp

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:

<env path>/bin/python manage.py startapp myapp
终难愈 2024-11-15 14:02:38

解决了!!!

在搜索年龄并尝试了所有这些不起作用的其他建议后,我终于找到了适合我的设置的解决方案。

我的设置/场景:

  • Windows,Python27
  • 在新文件夹中运行 python manage.py runserver 时,我的 django 项目通过 svn 签出
  • ,我收到导入错误
  • python manage.py runserver em> 曾经在原始文件夹(我将从中提交更改)中工作,直到我将其删除

解决方案

删除 同一目录中名为 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:

  • Windows, Python27
  • My django project is checked out via svn
  • when running python manage.py runserver in the new folder, I got the import error
  • python manage.py runserver used to work in the original folder (which I would commit changes from) until I deleted it

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

桃气十足 2024-11-15 14:02:38

尝试更改manage.py 的第一行。

更改

#!/usr/bin/python

#!/usr/bin/env python

Try change your first line of manage.py.

Change

#!/usr/bin/python

by

#!/usr/bin/env python
七分※倦醒 2024-11-15 14:02:38

===================================解决方案=============== ============================

首先

通过运行以下命令转到:virtualenv:
source bin/activate

并安装 django,因为您收到与“import django”相关的错误:

pip install django

然后运行:(

python manage.py runserver

注意:请将“runserver”更改为您要运行的程序名称)

对于同样的问题,它在我的情况下有效。
===================================剧情简介=============== ==========================

ERROR:
(Development) Rakeshs-MacBook-Pro:src rakesh$ 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
ModuleNotFoundError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    import django
ModuleNotFoundError: No module named 'django'

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
(Development) Rakeshs-MacBook-Pro:src rakesh$ 
(Development) Rakeshs-MacBook-Pro:src rakesh$ 
(Development) Rakeshs-MacBook-Pro:src rakesh$ python -Wall manage.py test
Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

在处理上述异常期间,又发生了异常:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    import django
ModuleNotFoundError: No module named 'django'

在处理期间除了上述异常之外,还发生了另一个异常:

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

安装 django 后:

(Development) MacBook-Pro:src rakesh$ pip install django
Collecting django
  Downloading https://files.pythonhosted.org/packages/51/1a/e0ac7886c7123a03814178d7517dc822af0fe51a72e1a6bff26153103322/Django-2.1-py3-none-any.whl (7.3MB)
    100% |████████████████████████████████| 7.3MB 1.1MB/s 
Collecting pytz (from django)
  Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
    100% |████████████████████████████████| 512kB 4.7MB/s 
Installing collected packages: pytz, django

解决后:

(Development) MacBook-Pro:src rakesh$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

August 05, 2018 - 04:39:02
Django version 2.1, using settings 'trydjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[05/Aug/2018 04:39:15] "GET / HTTP/1.1" 200 16348
[05/Aug/2018 04:39:15] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[05/Aug/2018 04:39:15] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
[05/Aug/2018 04:39:15] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
[05/Aug/2018 04:39:15] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
Not Found: /favicon.ico
[05/Aug/2018 04:39:16] "GET /favicon.ico HTTP/1.1" 404 1976

祝你好运!!

==================================SOLUTION=========================================

First goto: virtualenv

by running the command:
source bin/activate

and install django because you are getting the error related to 'import django':

pip install django

Then run:

python manage.py runserver

(Note: please change 'runserver' to the program name you want to run)

For the same issue, it worked in my case.
==================================Synopsis=========================================

ERROR:
(Development) Rakeshs-MacBook-Pro:src rakesh$ 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
ModuleNotFoundError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    import django
ModuleNotFoundError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
(Development) Rakeshs-MacBook-Pro:src rakesh$ 
(Development) Rakeshs-MacBook-Pro:src rakesh$ 
(Development) Rakeshs-MacBook-Pro:src rakesh$ python -Wall manage.py test
Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    import django
ModuleNotFoundError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

AFTER INSTALLATION of django:

(Development) MacBook-Pro:src rakesh$ pip install django
Collecting django
  Downloading https://files.pythonhosted.org/packages/51/1a/e0ac7886c7123a03814178d7517dc822af0fe51a72e1a6bff26153103322/Django-2.1-py3-none-any.whl (7.3MB)
    100% |████████████████████████████████| 7.3MB 1.1MB/s 
Collecting pytz (from django)
  Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
    100% |████████████████████████████████| 512kB 4.7MB/s 
Installing collected packages: pytz, django

AFTER RESOLVING:

(Development) MacBook-Pro:src rakesh$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

August 05, 2018 - 04:39:02
Django version 2.1, using settings 'trydjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[05/Aug/2018 04:39:15] "GET / HTTP/1.1" 200 16348
[05/Aug/2018 04:39:15] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[05/Aug/2018 04:39:15] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
[05/Aug/2018 04:39:15] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
[05/Aug/2018 04:39:15] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
Not Found: /favicon.ico
[05/Aug/2018 04:39:16] "GET /favicon.ico HTTP/1.1" 404 1976

Good luck!!

铁憨憨 2024-11-15 14:02:38

对于使用 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.

梦纸 2024-11-15 14:02:38

将 python python 路径存储在变量中并执行。这将包括否则丢失的包。

python_path= `which python` 
$python_path manage.py runserver

Store the python python path in a variable and execute.This would include the otherwise missing packages.

python_path= `which python` 
$python_path manage.py runserver
挽你眉间 2024-11-15 14:02:38

环境: Python 3.9.6Django 3.2.6VS 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.

深爱不及久伴 2024-11-15 14:02:38

我有类似的问题。 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.

飘过的浮云 2024-11-15 14:02:38

此错误通常是在未安装 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.

香橙ぽ 2024-11-15 14:02:38

我通过将 #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.

初雪 2024-11-15 14:02:38

只是一个错误
这也发生在我身上。我的错误是我在创建虚拟环境之前创建了 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

裸钻 2024-11-15 14:02:38

这是一个翻译错误。

如果您使用的是 vscode,则只需按照以下步骤操作:

  • 查看
  • 命令面板
  • 搜索 python
  • 选择解释器
  • 选择 >windows store,您的问题将得到解决,几秒钟后错误就消失了。

此问题是由于路径更改而发生的。

That's an interpreter error.

If you are using vscode then just follow these steps:

  • View
  • Command palette
  • Search for python
  • Select interpreter
  • Select windows store and your problem will get solved, after few seconds the errors have gone.

This problem occurs due to path changed.

濫情▎り 2024-11-15 14:02:38

您的项目是使用旧版本的 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

时光匆匆的小流年 2024-11-15 14:02:38

完全同意这是一个路径问题,但是fwiw,我也遇到了同样的错误。这是由于在设置虚拟环境时错误地使用了 Python 可执行文件的相对路径。我已经这样做了:

virtualenv -p ~/python_runtimes/2.7.3/bin/python venv2.7.3 --distribute

相反,我必须提供 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:

virtualenv -p ~/python_runtimes/2.7.3/bin/python venv2.7.3 --distribute

Instead I had to give the full path to the Python executable.

HTH,
Harlin

凉月流沐 2024-11-15 14:02:38

source ~/blog-venv/bin/activate

选择您的 virtualenv 来替换此处的“blog-venv”。

source ~/blog-venv/bin/activate

pick your virtualenv to replace "blog-venv" here.

梦忆晨望 2024-11-15 14:02:38

确保您正在运行正确的 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 the python2.7 framework & libraries. The same could happen as a result of virtualenv as well.

束缚m 2024-11-15 14:02:38

好的,事情是这样的:

您已经创建了一个虚拟环境,并且 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.

月竹挽风 2024-11-15 14:02:38

在发出 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.

故事↓在人 2024-11-15 14:02:38

我通过使用“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"

永不分离 2024-11-15 14:02:38

使用名为 site 的应用程序也可以重现此问题。

Having an application called site can reproduce this issue either.

独自←快乐 2024-11-15 14:02:37

如果像我一样,您在 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).

嗳卜坏 2024-11-15 14:02:37

如果您位于 virtualenv 中,则需要先激活它,然后才能运行 ./manage.py 'command'

source path/to/your/virtualenv/bin/activate

如果您在 .bash_profile 或 .bashrc 中配置 workon

workon yourvirtualenvname

*请不要编辑您的 manage.py 文件,可能不是正确的方式,并且可能会给你带来未来的错误

If you are in a virtualenv you need to activate it before you can run ./manage.py 'command'

source path/to/your/virtualenv/bin/activate

if you config workon in .bash_profile or .bashrc

workon yourvirtualenvname

*please dont edit your manage.py file maybe works by isnt the correct way and could give you future errors

静赏你的温柔 2024-11-15 14:02:37

我遇到了同样的问题,因为我是以超级用户身份安装 Django,因此不在我的 virtualenv 中。您不应该执行 sudo pip install Django

相反,请按以下方式安装:

$ source ./bin/activate
$ 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:

$ source ./bin/activate
$ pip install Django
意中人 2024-11-15 14:02:37

请使用 pip 重新安装 django:(

sudo pip install --upgrade django==1.3

将 1.3 替换为您的 django 版本)

Please, reinstall django with pip:

sudo pip install --upgrade django==1.3

(Replace 1.3 to your django version)

粉红×色少女 2024-11-15 14:02:37

众所周知,这是一个路径问题。

我的自定义包的基础与 /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.

夏了南城 2024-11-15 14:02:37

此问题的另一个可能原因是您的操作系统默认运行 python3。

您必须显式执行:python2 manage.py

,或者您需要编辑manage.py的shebang,如下所示:

#!/usr/bin/env python2

或者如果您使用的是python3:

#!/usr/bin/env 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:

#!/usr/bin/env python2

or if you are using python3:

#!/usr/bin/env python3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文