I have a script that imports a models.py from an app, but it will not import! I don\'t believe I am supposed to manually create an \"export DJANGO...\" environment variable...I\
You need to properly import your settings file. If you don't import the django environment variables first, your import will fail as you've noticed. You need to code something like:
import os # Set the DJANGO_SETTINGS_MODULE environment variable. os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings" #do your stuff
From command line, please execute this command. This may help you:
export DJANGO_SETTINGS_MODULE=projectname.settings
I had this similar error, and I realized that I forgot to create the file "init.py" in to the app folder:
myapp:
manage.py
myapp:
__init__.py
settings.py
....
One alternative to messing about with the environment is to just write your script as a Django custom management command, which takes care of setting up the environment for you (along with other things like option parsing, etc).
from command line ran "python manage.py shell" should include the right settings for you and give you the python prompt.
I don't believe I am supposed to manually create an "export DJANGO..." environment variable...
Manually or otherwise, you are supposed to ensure that variable is in the environment before you import a Django models file -- not sure what the causes are for your disbelief, but, whatever they may be, that disbelief is ill-founded.