Django manage.py 1.6.11 suddenly crashes when importing settings - python

I have an old DJango application that used work fine but suddenly crashes. I use to test it with:
cd /opt/formshare/src/formshare/
export PYTHONPATH=$PYTHONPATH:/opt/formshare/src/formshare
export DJANGO_SETTINGS_MODULE=formshare.settings.default_settings
python manage.py validate
But now I get this error:
(formshare) bash-4.3$ python manage.py validate
Your environment is:"formshare.settings.default_settings"
Traceback (most recent call last):
File "manage.py", line 21, in <module>
execute_from_command_line(sys.argv)
File "/opt/formshare/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/opt/formshare/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/formshare/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
commands = get_commands()
File "/opt/formshare/lib/python2.7/site-packages/django/core/management/__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "/opt/formshare/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/opt/formshare/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "/opt/formshare/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'formshare.settings.default_settings' (Is it on sys.path? Is there an import error in the settings file?): cannot import name OrderedDict
I am using:
virtualenv=15.0.2
Python=2.7.11
amqp==2.1.1
billiard==3.3.0.23
celery==3.1.15
dict2xml==1.3
Django==1.6.11
django-braces==1.10.0
django-celery==3.1.16
django-cors-headers==0.13
django-db-readonly==0.3.2
django-digest==1.13
django-filter==0.7
django-guardian==1.2.4
django-nose==1.4.2
django-oauth-toolkit==0.7.2
django-registration-redux==1.1
django-reversion==1.8.4
django-taggit==0.12.1
django-templated-email==0.4.9
djangorestframework==2.4.3
djangorestframework-csv==1.3.3
dpath==1.2.post70
elaphe==0.5.6
FormEncode==1.3.1
gdata==2.0.18
httmock==1.2.2
httplib2==0.9
jdcal==1.3
jsonfield==0.9.23
kombu==4.0.0
librabbitmq==1.5.2
linecache2==1.0.0
lxml==3.4.0
Markdown==2.5
mock==1.0.1
modilabs-python-utils==0.1.5
nose==1.3.7
numpy==1.11.2
oauthlib==2.0.1
openpyxl==2.0.5
pandas==0.12.0
Pillow==2.5.3
poster==0.8.1
psycopg2==2.5.4
pybamboo==0.5.8.1
pylibmc==1.3.0
pymongo==2.7.2
python-dateutil==2.6.0
python-digest==1.7
-e git+https://github.com/qlands/python-json2xlsclient.git#5a39387752d819cb6387f75569dbea9a5288aa6f#egg=python_json2xlsclient
pytz==2014.7
-e git+https://github.com/XLSForm/pyxform.git#cfe8589f40319fa3279b0a83e0d23d49bcbe8408#egg=pyxform
recaptcha-client==1.0.6
requests==2.4.1
savReaderWriter==3.4.2
simplejson==2.6.2
six==1.10.0
South==1.0
traceback2==1.4.0
unicodecsv==0.9.4
unittest2==1.1.0
vine==1.1.3
xlrd==0.9.3
xlwt==0.7.5
I tried to add set the path and setting in the validate but nothing works.
Any idea what could have changed and how to correct it?

The django process doesn't appear to be seeing your formshare.settings.default_settings. Without seeing the project structure, it's hard to know exactly what's going on, but a few things you might check.
If you are using python 2.x, does the formshare/ and the settings/ directory both have an __init__.py file in them?
Try adding /opt/formshare/src/ to the python path instead of (or in addition to) what you have.
One trick that I often try to make sure the python path is correct: while I'm active in my virtualenv, I'll enter a python shell (not django shell) and try something like the following:
import formshare
from formshare import settings
from fomrmshare.settings import default_settings
The error messages that might be generated from those might also help isolate the issue.

Since you activated virtualenv, you have to do something different. Try add2virtualenv in virtualenvwrapper.For more info
http://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html#add2virtualenv

Related

Running heroku run command generates the SECRET_KEY setting must not be empty error

I'm trying to deploy my Django project using Heroku, but now stuck with the empty SECRET_KEY problem when I run the heroku run python manage.py syncdb command:
Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 338, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 190, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 40, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/app/.heroku/python/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 4, in
from django.contrib.auth import get_user_model
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/init.py", line 7, in
from django.middleware.csrf import rotate_token
File "/app/.heroku/python/lib/python2.7/site-packages/django/middleware/csrf.py", line 14, in
from django.utils.cache import patch_vary_headers
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/cache.py", line 26, in
from django.core.cache import caches
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/cache/init.py", line 34, in
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 48, in getattr
self._setup(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 113, in init
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
However, running python manage.py syncdb is totally fine. In my settings.py file, I even wrote the SECRET_EKY explicitly as:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '*$_99!t5u&#w&9boeq()=obq#rjdsfasdfa#8hp_5s%h-mh(hk'`
I still got the same problem. I wondered if the environment variable DJANGO_SETTINGS_MODULE was missing, but running heroku config gave me (I replaced the information with xxx):
DATABASE_URL: xxx
DJANGO_SETTINGS_MODULE: xxx
SECRET_KEY: xxx
It seems that the relevant environment variables do exist. I really hope you could give me some suggestions. Thank you very much in advance.
I faced this issues a while ago. The following command resolved the issue for me
command: heroku config:set SECRET_KEY="your_secret_key"
Might be a stupid question, but are you running heroku run python manage.py syncdb?
If so, I am very confident, that your settings module is not loaded.
This could potentially have one of the following reasons:
Your settings module is not where Django expects it
(but you are saying manage.py can find it)
You have another config, that overshadows your config settings
(for example a local.py settings module for development settings)
Did you do any modification to manage.py or wsgi.py?
I edit this answer if I can think of more.
You can as well go to your app on your heroku dashboard, click on settings, then, scroll downwards; you'll find the button Reveal Config Vars. Add the SECRET_KEY and it's value as instructed. You can as well, add other secret properties required by your application.

Why does Django give errors with settings when started from script but not interactively?

I'm trying to start a mini-Django test server using some fixed data, for testing.
When run interactively (from the REPL) loop, it runs correctly, when executed as part of a script by python myscript.py, it fails. I'm guessing there's something different in the environment, but what?
Code:
import os
import sys
import json
import resttest
import unittest
from multiprocessing import Process
from django.core.management import execute_from_command_line
server_process = None
prefix = 'http://localhost:8000'
path = os.getcwd()
os.chdir('testapp') # Django project is separate and in subfolder 'testapp'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testapp.settings")
config_args = list()
config_args.append('')
config_args.append('testserver')
config_args.append('test_data.json')
proc = Process(target=execute_from_command_line, args=[config_args])
proc.start()
os.chdir(path)
Error:
Process Process-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 261, in fetch_command
commands = get_commands()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'testapp.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named testapp.settings
Context:
I'm working on a small Python project for testing & benchmarking REST services. It makes use of pycurl bindings to provide low-level control of the HTTP requests issued, to facilitate testing. To ensure that it is correctly using pycurl, I have created a basic Django REST API to use in functional testing.
Add the parent directory of testapp/ to the Python path.
sys.path.append("/path/to/directory/")
You can determine this location programmatically of course; the commands used to do so depend on where your script is in comparison to the testapp location. You can drop the os.getcwd() and os.chdir(). It's the PYTHONPATH that matters, not the working directory. You are specifying the settings module environment variable correctly, but the docs make note of properly setting the Python import search path.
Aside 1: take a look at django.core.management.call_command. It's a shortcut, though you still need to setup the settings module. call_command('testserver', 'test_data.json')
Aside 2: You can skip setting an environment variable as mentioned in the docs here. In your case:
from django.conf import settings
from testapp import settings as my_settings
settings.configure(my_settings)
What about adding export PYTHONPATH=$PYTHONPATH:$PWD to your .env?

Django tutorial (using the guide on Heroku), “Please supply the ENGINE value” (yes, I've read other threads)

I've read a lot of similar questions, and I've spent a lot of time trying to work this out myself, but I'm still stuck. Here's my situation:
I'm going through the Django guide on Heroku (https://devcenter.heroku.com/articles/django). I originally started in windows, but after having all sorts of problems I decided it would be a lot easier to switch to linux. I wiped my hard drive and installed Ubuntu 12.10 and Win 7 side by side to dual boot. Everything has been installed with pip (again, following the Heroku guide). I'm using PostgreSQL since it is (supposedly) well-integrated with Heroku. Before I get into what I've tried, here is the error (it's from near the end of the tutorial):
(venv)wtodom#wubuntu:~/hellodjango$ python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
If I try to specify the settings file with its absolute path to make sure it uses the right one I get this:
(venv)wtodom#wubuntu:~/hellodjango$ python manage.py syncdb --settings=/home/wtodom/hellodjango/hellodjango/settings.py
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/sql.py", line 6, in <module>
from django.db import models
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings '/home/wtodom/hellodjango/hellodjango/settings.py' (Is it on sys.path?): Import by filename is not supported.
A lot of the similar questions had people editing the wrong settings.py file. I know I edited the right one. Here's a cat of it (on gist since it's kinda long) - https://gist.github.com/wtodom/4736303. I've tried every combination I can think of for the data in there, including empty strings - no difference.
Other posts I've read have had people who had issues with the Django version they were running. Mine is a fresh install through and through, so it's not that.
Some people had issues with Django not being on the path. If I open a python shell from the projects root directory I can import things, so it's not that.
Some people didn't have values in their settings.py file, but as you can see from the gist, I do. I also tried running the command (python manage.py syncdb) both with and without pgadmin running and logged in.
On that note, here is a screencap of pgadmin showing that I have databases created and up: http://i.imgur.com/CXD2AWo.png
Here's what I get when I \l in PostgreSQL (another gist since SO killed the formatting): https://gist.github.com/wtodom/4736359
While I'm at it, here's a \du listing: https://gist.github.com/wtodom/4736442
I made sure that my manage.py has os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings") in it.
Everything in the Heroku tutorial worked without a hitch in Ubuntu up until I tried to sync the db locally (python manage.py syncdb). I can run the same command through heroku and it works fine (heroku run python manage.py syncdb).
I'm sure I'd forgotten probably something relevant that I've tried or read. I've spent about 6 hours reading and trying to get this thing going, just on this one error. I don't really know what else to try.
Also, I'm really new to databases and non-.NET web development (I've worked on projects, but never set one up), so if you know what's wrong with my setup please try to be clear. Thank you very much!
Heroku and your local machine usually need different database settings. A common way to handle that is to first list your local database settings in the settings.py file, then do a check to see if you're code is currently running on Heroku, and if so, override your database settings with what Heroku needs. That allows a single settings.py file to work in both environments.
Heroku creates an environment variable called DATABASE_URL containing the database URL string. A common way to check whether your code is running on Heroku is to check for that environment variable. Then, if it exists, use the dj_database_url module, which knows how to parse that string into the dictionary that Django needs for its configuration.
The only thing incorrect in your code is that you didn't check for the environment variable first before calling dj_database_url.config(). This means that dj_database_url, which normally should only run within Heroku, was running on your local machine too. Since your local machine almost certainly doesn't have the DATABASE_URL environment variable set, dj_database_url returned an empty dictionary. So, your code is overwriting your local database configuration with an empty dictionary when it's not run on Heroku. Django looks in that dictionary for the information it needs, can't find it, and raises an error.
To fix the problem, add a check for DATABASE_URL before using dj_database_url.
Change this part at the end of settings.py:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
To this:
if os.environ.get('DATABASE_URL', None):
import dj_database_url
DATABASES['default'] = dj_database_url.config()

Django: Import error when trying to use django-audiofield from GitHub

I'm trying to use a django application on Github called django-audiofield in my django project. I'm using Aptana Studio IDE. I installed it in my virtual environment and I'm getting the following error:
Traceback (most recent call last):
File "/Users/home/Desktop/Web Development/Aptana Studio 3.0/musicproject/src/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/commands/validate.py", line 9, in handle_noargs
self.validate(display_num_errors=True)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/validation.py", line 30, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/db/models/loading.py", line 158, in get_app_errors
self._populate()
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/db/models/loading.py", line 67, in _populate
self.load_app(app_name)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/db/models/loading.py", line 88, in load_app
models = import_module('.models', app_name)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/home/virtualenv/venv/src/audiofield/audiofield/models.py", line 19, in <module>
from audiofield.fields import AudioField
File "/Users/home/virtualenv/venv/src/audiofield/audiofield/fields.py", line 20, in <module>
from tasks import audio_convert_task
File "/Users/home/virtualenv/venv/src/audiofield/audiofield/tasks.py", line 14, in <module>
from celery.task import Task
ImportError: No module named celery.task
Finished "/Users/home/Desktop/Web Development/Aptana Studio 3.0/musicproject/src/manage.py validate" execution.
Please assist.
I'm the maintainer of the package, thanks to your post I realized that we were missing celery/django-celery in our requirements. It's now added in the latest version.
So "pip install django-audiofield" will install those dependencies for you.
If you have django-audiofield already installed you can run the following command to upgrade : "pip install django-audiofield --upgrade"
Have you installed celery? If no, install celery (pip install celery). If the github application in question (a link would have been nice) has a requirements.txt file anywhere, do pip install -r requirements.txt to install all the required dependencies of the application.
If yes, it looks like your import search path may not be set up correctly to find Celery - just a guess based on the import error.
Open up the django shell (type python manage.py shell when you're in your project's working directory) and type in import sys; print sys.path. Make sure that Celery is installed in one of those directories, since sys.path specifies the import search path for python modules.
If it isn't included there, you can temporarily append the directory celery is installed into by doing sys.path.append("<directory where celery is>") (mind the backslashes, since you're on Windows). If, after doing that, you can successfully from celery.tasks import Task, then you can set the PYTHONPATH environment variable appropriately to make it permanent.
Also, since you installed Celery into a virtualenv, it may be worth checking that both django and Celery are in the same virtualenv.
Finally, the latest commits to Celery indicate Celery now has native Django support, but since that hasn't been released as a stable version yet, you may want to look at django-celery.
Edit (in response to comment below):
Since you mentioned you're trying to get django-audiofield to work: it seems to be on pypi. This means you can use pip (or easy_install) to install it using pip install django-audiofield once you've installed all the required (non-python) dependencies, and stuff like celery should be downloaded for you.

Run custom Django management command over SSH

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.
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
You may try mysite path to sys.path in manage.py? It could help - as I can see now python does not see mysite directory.
import sys
sys.path.append('mysite_directory')
This could be because the .profile file is not loaded. Try this instead (replace .profile with your shell profile):
ssh myserver '. ~/.profile; python manage.py sitedir/mycommand'

Categories

Resources