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!

8 comments:

  1. I also had this problem, and this fix worked for me. Really appreciate the tip.

    ReplyDelete
  2. Well, I spoke too soon. This is strange. Followed the steps above. That allows me to run Python from my virtualenv and import celery/djcelery, but ONLY if I run Python from my MySite Folder (see below). If I try running Python from a sub-folder within my MySite (e.g., "Project" below), I get the same import error. Also, if I try running syncdb from "Project" i get the same error.

    MySite
    myVirtualEnv
    Project
    |
    App1
    App2
    manage.py
    etc.

    Any thoughts?

    ReplyDelete
  3. 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
  4. Oooo -- you're good!

    re: 1.) yes, it's active.

    re: 2) One I left out before. I think I'm handling my virtualenv(s) differently than you. Looking over your code above, it looks like you have a common dir that houses all of your virtualenvs, no? I house individual virtualenvs within individual "mysite" folders, so, yes, there indeed is a python binary living in that folder. Not sure how that might affect things.

    re: 3.) tried it, I get "No module named current_app" (not sure if i should have substituted a specific app name here).

    re: 4.) Sort of? My original virtualenv for this project was Python 2.7.2. Since starting the project, I've switched over to HomeBrew and used it to install Python, which defaults to 2.7.3. Sounds like this may be a/the culprit?

    ReplyDelete
  5. 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
    Replies
    1. Well, this points me in the right direction. Much appreciated.

      Delete