python manage.py runserver 报错

2013-05-13 18:02:05 +08:00
 paicha
我在搭建 Python + Django 的 WEB 服务器

Python 现在安装了两个版本:

Python2.72(编译安装的)

Python2.43(网上看到说如果卸载这个版本会出现很多问题,所以就不敢卸载了)

======================================================
python manage.py runserver
提示:
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: cannot import name execute_manager

不过执行 python2 manage.py runserver 是可以运行成功的。

但是用 Python2 命令的话,发现是 Python2.43 版本,有些功能实现不了。

所以需要在 Python2.72 下运行,但是一直报错,求搭救。
6564 次点击
所在节点    Python
13 条回复
SErHo
2013-05-13 18:16:25 +08:00
估计是django没有在2.72的PATH里面,你试一下import django 有没有问题。
chemzqm
2013-05-13 18:28:28 +08:00
好奇你在用什么系统,怎么默认python版本是2.4.3
paicha
2013-05-13 18:29:20 +08:00
@SErHo

是用这样的命令吗?初学勿怪

Python 2.7.2 (default, May 11 2013, 23:47:25)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print django
<module 'django' from '/usr/local/lib/python2.7/site-packages/django/__init__.pyc'>
>>>
paicha
2013-05-13 18:30:40 +08:00
@chemzqm CentOS 5.8 空间商安装的。
notedit
2013-05-13 18:45:21 +08:00
不是import execute_manager 的错误 是你的程序在import 其他的模块的时候已经已经报错了 但是被django 给统一扑捉了 所以报import execute_manager的错误
paicha
2013-05-13 19:25:29 +08:00
@notedit 虽然不太懂,但是要怎么解决?
notedit
2013-05-13 20:03:31 +08:00
在manager.py 中 ImportError 下面 添加:

import traceback
traceback.print_exc()

你就知道错误了
paicha
2013-05-13 20:17:22 +08:00
@notedit

一下是修改后的 manager.py

#!/usr/bin/env python
from django.core.management import execute_manager
import imp
try:
imp.find_module('settings') # Assumed to be in the same directory.
except ImportError:

import traceback #增加的代码
traceback.print_exc()

import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
sys.exit(1)

import settings

if __name__ == "__main__":
execute_manager(settings)

=======运行===================
[root@server test]# python manage.py runserver 0.0.0.0:8000
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: cannot import name execute_manager

=============================================

还是一样的错误提示。
DH
2013-05-13 20:23:48 +08:00
就是路径的问题。一般用django的话,

用virtualenv 配环境的,在家 pip 安装需要的 包。这基本上是标准流程。
你试着重新配一次环境(标准,流程很重要,这也是要学习的,而不光是代码本身)。

安装
* python
* pip
* virtualenv

然后到你的网站目录:
virtualenv --distribute venv
source venv/bin/activate
pip install -r requirements.txt

(完了要检查一下package有没有安装到 venv/lib/python2.x 下面)
如果你用的centos,可能要先 sudo -s


requirements.txt一般这么写

Django==1.5.0
PIL==1.1.7
django-redis==3.1.7
psycopg2==2.4.6
paicha
2013-05-13 20:24:45 +08:00
@notedit 不知道是不是这样做,只会一些基本的命令。

当初就想自己有服务器就别浪费,如果实在搞不定,就在 SAE 里搭建好了。

捂脸。。。(话说,V2EX不能贴图,也没一些简单的表情呢,简洁得有点不习惯)
paicha
2013-05-13 20:26:53 +08:00
@DH 好的,谢谢,我再折腾看看。
notedit
2013-05-14 08:38:35 +08:00
仔细看了一下 是路径问题 你要用python2.7 要在python2.7下安装django
paicha
2013-05-14 12:30:08 +08:00
@notedit 好的,那我重新安装一下

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/68708

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX