4

I am trying to setup a simple celery project. As per the official documentation, the layout is as follow:

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 import celery.Celery this way:

from __future__ import absolute_import
from celery import Celery

In IPython, I can import new_updater.celery without problems:

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 run into 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 could be going on here?

I know I could simply rename celery.py to, e.g. celery_config.py (this is the standard answer to this kind of question on SO), but I'd prefer actually fixing this and not stray away from celery's official documentation.

EDIT: I printed out sys.path in new_updater/updater.py, here's the result:

['/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' the issue, but I don't understand why that is in the path. How I got that:

  1. mkvirtualenv test
  2. python setup.py develop at the root of my project

EDIT2: it's the same outside a virtualenv, with celery installed from debian, and my PYTHONPATH set this way:

export PYTHONPATH=/usr/lib/python2.7/dist-packages:~/dev/debian/debsources
Clément Schreiner
  • 1,087
  • 1
  • 8
  • 16
  • did you try `PYTHONPATH=".:$PYTHONPATH" python debsources/new_updater/updater.py` ? also does `debsources/__init__.py` exist? – Jörn Hees May 27 '15 at 14:11
  • Just tried: it's the same. And yes. – Clément Schreiner May 27 '15 at 14:16
  • Check this link this may solve your problem http://stackoverflow.com/questions/19673662/import-error-in-celery – Kousik May 27 '15 at 16:07
  • 1
    About how that first line got into 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 https://docs.python.org/2/library/sys.html – pavel_form May 27 '15 at 16:08
  • Anyway, you shouldn't name your files as libraries you use, even if off. docs do so. – pavel_form May 27 '15 at 16:14
  • Thanks @pavel_form. Can you put this (the sys.path explication) in an answer so I can accept it? – Clément Schreiner May 27 '15 at 16:50

1 Answers1

2

About how that first line got into 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

Anyway, you shouldn't name your files as libraries you use, even if off. docs do so. Will help avoid many possible errors.

pavel_form
  • 1,760
  • 13
  • 14
  • Thanks. I'll keep it as "celery.py" because celery workers will find it automatically, but I will move my script ("updater.py") out of the module, since I shouldn't execute a script from a module anyway. – Clément Schreiner May 27 '15 at 18:45