Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting DJANGO_SETTINGS_MODULE on windows Correctly

There are my post on this query but most of them are for linux. None of them exlicitly for windows

in my application i'm setting up the database (sqlite3 , default in Django). after editing setting.py file of my application (mysite)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'D:/Django_Code/sqlite.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

after finding that i have to set DJANGO_SETTINGS_MODULE environment so that Django can know about the database setting. So i set it up like

DJANGO_SETTINGS_MODULE = "D:\Django_Code\mysite\mysite\settings.py"

To cross check the database setup when i issue

>>> from django.db import connection
>>> cursor = connection.cursor()

it says DJANGO_SETTINGS_MODULE environment variable is Undefined.

Need help to set DJANGO_SETTINGS_MODULE correctly.

like image 440
navyad Avatar asked Oct 12 '12 07:10

navyad


1 Answers

The easiest way to set DJANGO_SETTINGS_MODULE in Windows is using the set command from the command prompt. You should also be able to set it via system properties, but you'd need to close and reopen the command prompt for the changes to take effect.

You can query the current value of DJANGO_SETTINGS_MODULE using the set command as well:

C:\temp\testproject> set DJANGO_SETTINGS_MODULE

In addition, you need to set it to the python module name, not the filename (setting it to the filename will give you an error similar to "Could not import settings 'C:\temp\testproject\settings.py' (Is it on sys.path?): Import by filename is not supported.")

For example,

C:\temp\testproject> set DJANGO_SETTINGS_MODULE=testproject.settings

Then you can run python and the module can be imported.

>>> import sys
>>> sys.path += ['C:\\temp']
>>> from django.db import connection
>>> connection.cursor()
<django.db.backends.util.CursorDebugWrapper object at 0x02C7F0B0>

Note that we've also explicitly added the directory containing the django project (testproject in this case) to sys.path, which is effectively a list of directories where Python looks for modules. This is necessary because Python imports the settings file as a python module, not a file (as mentioned earlier).

If you want an interactive shell to play with Django objects, you can use the shell management command. In your Django project directory, run the following command:

manage.py shell

Since you're on windows, you may have to do

python manage.py shell

instead, since I've personally had issues with python scripts not receiving command line arguments otherwise.

C:\temp\testproject> python manage.py shell

In [1]: from django.db import connection

In [2]: connection.cursor()
Out[2]: <django.db.backends.util.CursorDebugWrapper at 0x33a0bd0>

(Note that I have IPython installed and Django is being clever and using that; if you don't have IPython installed, your shell will look slightly different.)

If you want to run a script with Django, the easiest way is to write a custom management command, which you can then run with an argument to manage.py.

like image 161
Caspar Avatar answered Sep 18 '22 10:09

Caspar