Custom commands called outside project path

15 views
Skip to first unread message

Tim Daniel

unread,
Feb 9, 2010, 1:34:46 PM2/9/10
to Django users
I'm trying to launch custom admin commands with a cron(outside the
project path). I'm running Django 1.1.1. I've tried to add the --
pythonpath='path_to_my_project' option, but it doesn't work either.

When I call path_to_my_project/manage.py my_command --
pythonpath='path_to_my_project' from inside the project path it all
goes well, but when I'm outside(cwd). It only gives a

Unknown command: 'my_command'
Type 'manage.py help' for usage.

error.
I've put a trace inside the manage.py and followed it until I got to:

/usr/lib/pymodules/python2.6/django/core/management/__init__.py
Method: def find_management_module(app_name)


if os.path.basename(os.getcwd()) != part:
raise e

The problem is that os.path.basename(os.getcwd()) returns my current
working directory(the last part of it) that will never be equal to
'part' that is the project_name. Only if you are inside your project
path.

So because of that all my custom commands are never 'discovered'.

I've been stucked here yesterday on other machine but somehow it
solved magically!!
I hope I'm doing some really stupid noob error here and so it can be
easily solved.

Regards,

Tim D.G.

Tim Daniel

unread,
Feb 9, 2010, 1:58:57 PM2/9/10
to Django users
Well I think I've made the trick: the option pythonpath accepts one
parameter: PATH_TO_MY_PROJECT but without the project name itself. So
if your projects name is my_project, my_project should not appear
inside PATH_TO_MY_PROJECT . Sorry for the unnecessary posting, hope
it'll be useful for somebody.

Bill Freeman

unread,
Feb 9, 2010, 2:37:34 PM2/9/10
to django...@googlegroups.com
If you're running on *nix (including Os/X, probably) you have a few options.
Someone else will have to help if it's Windows without cygwin.

The easy way, if it does all that you need, is to deploy, instead of a
python file
a shell script. It can cd to the appropriate directory, then run the
the appropriate
(and not on the general PATH) command you want to run. The shell is perfectly
capable of collecting command line arguments and passing them on.

You can rig your python script to start thusly:
----------
import sys, os
sys.path[0] = 'path_to_your_project'
os.chdir(sys.path[0])
-----------
certainly before importing any django stuff. The os.chdir() call may
not be necessary,
but can't hurt.

Both of these have the disadvantage of the script needing to know the
path to your
project, but something has to know it.

Bill

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

creecode

unread,
Feb 9, 2010, 5:27:40 PM2/9/10
to Django users
Hello Tim,

On Feb 9, 10:34 am, Tim Daniel <redarrow...@hotmail.com> wrote:
> I'm trying to launch custom admin commands with a cron(outside the
> project path). I'm running Django 1.1.1. I've tried to add the --
> pythonpath='path_to_my_project' option, but it doesn't work either.

Have you tried something like...

* * * * * root /path/to/your/project/manage.py yourcommand

Toodle-loooooooo........
creecode

Bill Freeman

unread,
Feb 9, 2010, 5:46:56 PM2/9/10
to django...@googlegroups.com
Or:

* * * * * (cd /path/to/you/project ; ./manage.py yourcommand)

The parentheses probably aren't necessary since the entire command
portion of the crontab entry is almost certainly run in one shell.

(I'm not sure about that "root" in creecode's version. Editing crontab
with crontab -e on a linux box has no user specification because it
edits the crontab of the invoking user. Perhaps it's different for a
directly edited root crontab.)

Bill

David De La Harpe Golden

unread,
Feb 10, 2010, 6:34:20 AM2/10/10
to django...@googlegroups.com

> (I'm not sure about that "root" in creecode's version. Editing crontab
> with crontab -e on a linux box has no user specification because it
> edits the crontab of the invoking user. Perhaps it's different for a
> directly edited root crontab.)

The system-wide /etc/crontab is different to individual user crontabs
(including root's) - the former has an extra user field, so the sysadmin
can declare things to run under different users, without
touching the per-user - and user-editable - crontabs.

crontabs also support direct environment variable declarations,
so for simple things you can just also stick in a
DJANGO_SETTINGS_MODULE=blah.settings
PYTHONPATH=/some/python/stuff:/even/more/python/stuff

See also "man 5 crontab"


Tim Daniel

unread,
Feb 10, 2010, 1:57:48 PM2/10/10
to Django users
Thanks for the quick and helpful answers.

On 9 feb, 19:37, Bill Freeman <ke1g...@gmail.com> wrote:
> If you're running on *nix (including Os/X, probably) you have a few options.
> Someone else will have to help if it's Windows without cygwin.
>
> The easy way, if it does all that you need, is to deploy, instead of a
> python file
> a shell script.  It can cd to the appropriate directory, then run the
> the appropriate
> (and not on the general PATH) command you want to run.  The shell is perfectly
> capable of collecting command line arguments and passing them on.
>
> You can rig your python script to start thusly:
> ----------
> import sys, os
> sys.path[0] = 'path_to_your_project'
> os.chdir(sys.path[0])
> -----------
> certainly before importing any django stuff.  The os.chdir() call may
> not be necessary,
> but can't hurt.
>
> Both of these have the disadvantage of the script needing to know the
> path to your
> project, but something has to know it.
>
> Bill
>

> On Tue, Feb 9, 2010 at 1:34 PM, Tim Daniel <redarrow...@hotmail.com> wrote:
> > I'm trying to launch custom admin commands with acron(outsidethe
> > project path). I'm running Django 1.1.1. I've tried to add the --
> > pythonpath='path_to_my_project' option, but it doesn't work either.
>
> > When I call path_to_my_project/manage.py my_command --
> > pythonpath='path_to_my_project' from inside the project path it all

> > goes well, but when I'moutside(cwd). It only gives a

Reply all
Reply to author
Forward
0 new messages