Absolute_import is not working correctly (in a celery project)

I am trying to set up a simple celery project. According to the official documentation, the format looks like this:

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

      

In celery.py, I am importing celery. Celery in this way:

from __future__ import absolute_import
from celery import Celery

      

In IPython, I can import new_updater.celery without any problem:

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'>

      

However, when trying to run new_updater.updater, I encountered the following error:

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

      

What can be done here?

I know I can just rename celery.py to eg. celery_config.py (this is the standard answer to this question on SO), but I'd rather actually fix it rather than deviate from the official celery documentation.

EDIT: I have printed out sys.path

to new_updater / updater.py, here is the result:

['/home/clemux/dev/debian/debsources/debsources/new_updater',
'/home/clemux/dev/debian/debsources',
'/home/clemux/.virtualenvs/debsources/lib/python2.7',
<snip>

      

Removing sys.path[0]

before another import "solves" the problem, but I don't understand why this is happening along the way. How I got it:

  • mkvirtualenv test

  • python setup.py develop

    at the root of my project

EDIT2: It's the same outside virtual, with celery installed from debian, and mine is PYTHONPATH

installed this way:

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

      

+3


source to share


1 answer


About how the first line ended up in sys.path

:

A list of strings indicating the search path for modules. Initialized from the PYTHONPATH environment variable, plus depends on the default setting. After initialization at program startup, the first element of this list, path [0], is the directory containing the script that was used to invoke the Python interpreter.



from docs

In any case, you should not list your files as libraries you are using, even if they are disabled. docs do it. Help avoid many possible mistakes.

+2


source







All Articles