absolute_import 无法正常工作(在 celery 项目中)

absolute_import not working correctly (in celery project)

我正在尝试设置一个简单的芹菜项目。根据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(这是对此类问题的标准答案),但我更愿意实际解决这个问题,而不是偏离 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>

Deleting sys.path[0] before the other import 'solves' 这个问题,但我不明白为什么那是在路径中。我是怎么得到的:

  1. mkvirtualenv test
  2. python setup.py develop 在我项目的根目录下

EDIT2:在 virtualenv 之外是一样的,从 debian 安装了 celery,我的 PYTHONPATH 是这样设置的:

export PYTHONPATH=/usr/lib/python2.7/dist-packages:~/dev/debian/debsources

关于第一行如何进入 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.

来自 docs

无论如何,你不应该将你的文件命名为你使用的库,即使关闭。文档这样做。将有助于避免许多可能的错误。