Monday, October 8, 2012

celery + djcelery problem with virtualenv and virtualenvwrapper



this is a tricky one - out of nowhere, production env boxes started failing at celery startup with:



ImportError: cannot import name current_app
when importing djcelery. Versions of celery were fine.
It turns out if you import celery and run
import celery.current_app you'll see the real problem, which is that the virtualenv binary is out of sync with the new python binary from a recent security update - specifically, os.urandom has been changed/removed.

if you have virtualenvwrapper, and you let $ENV=YOUR_ENVIRONEMNT_NAME
So the answer is:


deactivate (in case an env is running)
cd ~/$ENV_HOME (.virtualenvs, for me)
rm $ENV/bin/python
virtualenv $ENV


this will rebuild your python binary with the correct python post-security fix, without losing any other packages. happy hacking!

4 comments:

  1. sure, some thoughts:
    1) make sure the virtualenv is active again (a no brainer, but still...)
    2) is there a python binary living in the mysite folder?
    3) try the above trick that I found (start the python interpreter, run: import celery.current_app - is it the same problem as before?
    4) has your version of python changed?

    ReplyDelete
  2. from what it sounds like, the reason it's working when you are in that folder is that it's running your virtual environment's python as it's supposed to, but that for some reason that python is not getting set to $PATH/python in general. thus, when your path (including current directory) includes the correct python everything works, but otherwise it does not.

    I actually use virtualenvwrapper to manage my virtualenvs, altho I'm pretty sure that shouldn't make a huge difference.

    from the mysite folder, if you run python, what version is it? can you run "import celery.current_app (or maybe "from celery import current_app")? Is this version + behavior different from the other python interpreter that opens from other folders?

    I may be good, but I don't know OS X / mac stuff very well at all, so altho I can tell that your problem seems to involve your virtualenv not setting you python binary correctly (esp. based on your answers to the above questions), I'm not sure what to do beyond that.

    ReplyDelete