【问题标题】:absolute_import not working correctly (in celery project)absolute_import 无法正常工作(在 celery 项目中)
【发布时间】: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],但我不明白为什么会出现这种情况。我是怎么知道的:

  1. mkvirtualenv test
  2. 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 也存在吗?
  • 刚试过:还是一样的。是的。
  • 查看此链接可能会解决您的问题stackoverflow.com/questions/19673662/import-error-in-celery
  • 关于第一行如何进入 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
  • 无论如何,你不应该将你的文件命名为你使用的库,即使是关闭的。文档这样做。

标签: python import celery


【解决方案1】:

关于第一行如何进入sys.path

指定模块搜索路径的字符串列表。 从环境变量 PYTHONPATH 初始化,加上一个 安装依赖的默认值。在程序启动时初始化, 该列表的第一项,path[0],是包含 用于调用 Python 解释器的脚本。

来自docs

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

【讨论】:

  • 谢谢。我会将它保留为“celery.py”,因为 celery 工作人员会自动找到它,但我会将我的脚本(“updater.py”)移出模块,因为无论如何我都不应该从模块中执行脚本。
猜你喜欢
  • 2020-08-21
  • 2017-05-06
  • 2020-04-24
  • 2016-01-14
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多