2

I'm trying to run a cron to run a custom Django management command. I'm running on DotCloud.

When the cron runs, I get the following error:

Traceback (most recent call last):
File "./manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named django.core.management

I think this is because cron is running in a bare environment, so I tried to manually set the PYTHONPATH in the same cron. Here is that command...

PYTHONPATH=/home/dotcloud/env/lib/python2.6/site-packages/setuptools-0.6c11-    
py2.6.egg:/home/dotcloud/env/lib/python2.6/site-packages/pip-1.0.1-        

py2.6.egg:/home/dotcloud/current:/home/dotcloud/env/lib/python2.6:/home/dotcloud/env/lib/python2.6/plat-linux2:/home/dotcloud/env/lib/python2.6/lib-tk:/home/dotcloud/env/lib/python2.6/lib-old:/home/dotcloud/env/lib/python2.6/lib-dynload:/usr/lib/python2.6:/usr/lib64/python2.6:/usr/lib/python2.6/plat-linux2:/usr/lib/python2.6/lib-tk:/usr/lib64/python2.6/lib-tk:/home/dotcloud/env/lib/python2.6/site-packages:/usr/local/lib/python2.6/dist-packages/virtualenv-1.6.1-py2.6.egg:/usr/local/lib/python2.6/site-packages:/usr/local/lib/python2.6/dist-packages:/usr/lib/python2.6/dist-packages:/usr/lib/pymodules/python2.6

The error is still occurring and I'm not sure what's going on. Any advice is much appreciated.

Thanks.

user523513
  • 957
  • 1
  • 7
  • 8

1 Answers1

7

You're right: cron jobs run in a "bare" environment.

Instead of doing this:

* * * * * /home/dotcloud/current/myapp/manage.py args...

You should do this:

* * * * * /home/dotcloud/env/bin/python /home/dotcloud/current/myapp/manage.py args...

Running the python interpreter contained in ~/env/bin will automatically setup the correct environment.

jpetazzo
  • 14,874
  • 3
  • 43
  • 45
  • That gets me one step further. If I do this, I get an error that the custom command I'm trying to run doesn't exist. If I run it from my shell, it works perfectly. I've read that you have to execute the command from the manage.py directory itself, but I'm not sure. Thoughts? – user523513 May 04 '12 at 04:05
  • @user523513 Django management commands will run fine if you do what jpetazzo said and use the fully qualified path to python and manage.py The only reason why it wouldn't work is if the management command is using the current running directory for some reason, maybe looking up the current directory and delete files, or something like that. This isn't good practice, so most management commands don't do this. – Ken Cochrane May 04 '12 at 12:52
  • I am having the exact same problem as @user523513. This is not a case of the management command trying to use the current running directory. It is a problem with Django not finding the management command in the first place. I am already using the python interpreter of the virtualenv, and all I get is "Unknown command". – Kal Dec 05 '13 at 02:24