Using Django other-than as web framework.

11 views
Skip to first unread message

Harsha Reddy

unread,
Jul 2, 2009, 3:25:00 AM7/2/09
to Django users
I using Django from past one week.

On the Django web site, I read that it is a high-level python web
framework. Inspite of knowing this I am trying to use a Django
application both as a web application as well as a CLI client.

To know if this works out, I created a Django project named "pyMyApp"
and added an application into it.
I updated the:

settings.py to detect my application,
also models.py to create two tables.

I have an other python file in the application which parses XML. This
python file makes use of objects defined in the models.py to store the
parsed values in those 2 tables.

In the directory "../" where my "pyMyApp" resides, I created a file
index.py added the code to test the parser code. Runnning this file
yeilds:

python index.py
Traceback (most recent call last):
File "index.py", line 3, in ?
from pyAPP.translator.translate import *
File "/usr/src/pySCAF/translator/translate.py", line 3, in ?
from pySCAF.translator.models import duo, scenario
File "/usr/src/pySCAF/../pySCAF/translator/models.py", line 1, in ?
from django.db import models
File "/usr/lib/python2.4/site-packages/django/db/__init__.py", line
10, in ?
if not settings.DATABASE_ENGINE:
File "/usr/lib/python2.4/site-packages/django/utils/functional.py",
line 269, in __getattr__
self._setup()
File "/usr/lib/python2.4/site-packages/django/conf/__init__.py",
line 38, in _setup
raise ImportError("Settings cannot be imported, because
environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable
DJANGO_SETTINGS_MODULE is undefined.


By looking at the trace, I got a thought that "Django is designed to
be used as web framework"
if at all I want to use it both as cli and web apps, how to achieve
this in the case above?

jon michaels

unread,
Jul 2, 2009, 4:23:34 AM7/2/09
to django...@googlegroups.com
What do you get when you type 'python manage.py dbshell'?

Daniel Roseman

unread,
Jul 2, 2009, 4:25:53 AM7/2/09
to Django users
Did you try doing what the error says - setting the
DJANGO_SETTINGS_MODULE environment variable?

The error has nothing to do with whether you are running as a web app
or not.
--
DR.

Emily Rodgers

unread,
Jul 2, 2009, 6:55:26 AM7/2/09
to Django users
On Jul 2, 8:25 am, Harsha Reddy <nhar...@gmail.com> wrote:
Often with my apps I need to have a python script that is not part of
the web app (poking data in from logs, housekeeping scripts, etc). I
don't really know how your cli works, but you probably need to put
this in the script that takes the user input:

from django.core.management import setup_environ
import settings
setup_environ(settings)

This will load your settings file.

HTH,
Em

geert

unread,
Jul 2, 2009, 7:13:26 AM7/2/09
to Django users
Without quite knowing the details of your particular project, I can
assert that a django app will happily double as a cli app. In fact, I
do this almost all the time. The only thing you are now missing is the
proper way to import the settings.

Try something like this

from django.core.management import setup_environ
import settings
setup_environ(settings)


Geert

Harsha Reddy

unread,
Jul 2, 2009, 5:28:37 AM7/2/09
to Django users
Give Mysql prompt:

python manage.py dbshell
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8255 to server version: 5.0.22

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>


On Jul 2, 1:23 pm, jon michaels <joniama...@gmail.com> wrote:
> What do you get when you type 'python manage.py dbshell'?
>

Harsha Reddy

unread,
Jul 2, 2009, 5:30:25 AM7/2/09
to Django users
after setting the var:
export DJANGO_SETTINGS_MODULE=pyMyApp.settings

it works!!

Thanks a lot!

maplye

unread,
Jul 3, 2009, 8:33:51 PM7/3/09
to django...@googlegroups.com
def execute_from_command_line():
from django.core.management import setup_environ
sys.path.append(projectpath)

try:
import settings
except ImportError:
print "You don't appear to have a settings file in this directory!"
print "Please run this from inside a project directory"
sys.exit()
setup_environ(settings)

2009/7/2 Harsha Reddy <nha...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages