【发布时间】:2015-08-09 16:17:46
【问题描述】:
我正在尝试设置一个简单的芹菜项目。根据official documentation,布局如下:
clemux@melody ~/dev/debian/debsources/debsources
% find new_updater -name "*.py"
new_updater/tasks.py
new_updater/updater.py
new_updater/__init__.py
new_updater/celery.py
在 celery.py 中,我以这种方式导入 celery.Celery:
from __future__ import absolute_import
from celery import Celery
在 IPython 中,我可以毫无问题地导入 new_updater.celery:
In [2]: from debsources.new_updater import celery
In [3]: celery?
Type: module
String form: <module 'debsources.new_updater.celery' from '/home/clemux/dev/debian/debsources/debsources/new_updater/celery.pyc'>
但是,在尝试运行 new_updater.updater 时,我遇到了以下错误:
clemux@melody ~/dev/debian/debsources
% python debsources/new_updater/updater.py
Traceback (most recent call last):
File "debsources/new_updater/updater.py", line 6, in <module>
from debsources.new_updater.tasks import print_package
File "/home/clemux/dev/debian/debsources/debsources/new_updater/tasks.py", line 3, in <module>
from debsources.new_updater.celery import app
File "/home/clemux/dev/debian/debsources/debsources/new_updater/celery.py", line 3, in <module>
from celery import Celery
File "/home/clemux/dev/debian/debsources/debsources/new_updater/celery.py", line 3, in <module>
from celery import Celery
ImportError: cannot import name Celery
这里会发生什么?
我知道我可以简单地将 celery.py 重命名为,例如celery_config.py (这是关于 SO 这类问题的标准答案),但我更愿意实际解决这个问题,而不是偏离 celery 的官方文档。
编辑:我在 new_updater/updater.py 中打印出 sys.path,结果如下:
['/home/clemux/dev/debian/debsources/debsources/new_updater',
'/home/clemux/dev/debian/debsources',
'/home/clemux/.virtualenvs/debsources/lib/python2.7',
<snip>
在另一个导入“解决”问题之前删除sys.path[0],但我不明白为什么会出现这种情况。我是怎么知道的:
mkvirtualenv test-
python setup.py develop在我的项目的根目录
EDIT2:在virtualenv之外也是一样,从debian安装celery,我的PYTHONPATH这样设置:
export PYTHONPATH=/usr/lib/python2.7/dist-packages:~/dev/debian/debsources
【问题讨论】:
-
你试过
PYTHONPATH=".:$PYTHONPATH" python debsources/new_updater/updater.py吗?debsources/__init__.py也存在吗? -
刚试过:还是一样的。是的。
-
关于第一行如何进入 sys.path:
A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default. As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter.from docs.python.org/2/library/sys.html -
无论如何,你不应该将你的文件命名为你使用的库,即使是关闭的。文档这样做。