Run custom Django management command over SSH
Question: I have a Django application with a custom management command in one of the
apps. If I log in over SSH I can run it without any problems with

> python manage.py sitedir/mycommand

However, if I try to run the command as a oneliner from my local box like
this:

> ssh myserver python manage.py sitedir/mycommand

I get an ImportError like this:

    
    
    Traceback (most recent call last):
      File "mysite/manage.py", line 11, in <module>
        execute_manager(settings)
      File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 438, in execute_manager
        utility.execute()
      File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 379, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 261, in fetch_command
        klass = load_command_class(app_name, subcommand)
      File "/usr/lib/python2.5/site-packages/django/core/management/__init__.py", line 67, in load_command_class
        module = import_module('%s.management.commands.%s' % (app_name, name))
      File "/usr/lib/python2.5/site-packages/django/utils/importlib.py", line 35, in import_module
        __import__(name)
    ImportError: No module named mysite.myapp.management.commands.mycommand
    

The real reason is that I want to run this admin command from a Fabric script
but until I can get it to run via the ssh one-liner I guess it will be
impossible. Is there something in the environment that differs when you run it
via the ssh one-liner? The python path seems correct in both cases.


Answer: I think I have a clue what is going on. I don't know how to fix it yet.

To reproduce your scenario I wrote a small script.

    
    
    #!/usr/bin/python
    import sys, django
    print django.VERSION
    

After which I executed it after logging in through SSH as well as remotely
(`ssh yourserver.com "python /home/me/script.py"`) and everything went fine.

Then I changed the script.

    
    
    #!/usr/bin/python
    import os
    print os.environ['DJANGO_SETTINGS_MODULE']
    

This version worked when I logged in to the server but **failed** when I tried
to execute it remotely.

    
    
    Traceback (most recent call last):
      File "/home/me/script.py", line 3, in <module>
        print os.environ['DJANGO_SETTINGS_MODULE']
      File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
        raise KeyError(key)
    KeyError: 'DJANGO_SETTINGS_MODULE'
    

Apparently the `DJANGO_SETTINGS_MODULE` environment variable is not set when
you execute the command remotely over SSH. I suspect this _**may**_ be what is
going wrong in your case. You will need to figure out how to make sure that
this variable is properly set before executing the script.

~~Perhaps you can explicitly set it:`os.environ['DJANGO_SETTINGS_MODULE'] =
'foo'`.~~.

Try this:

    
    
    ssh yourserver.com "python /home/me/script.py" -t 
        DJANGO_SETTINGS_MODULE=app.settings.custom
    

