Why do I need the DJANGO_SETTINGS_MODULE set? - python

Every time I log on to my server through SSH I need to type the following:
export DJANGO_SETTINGS_MODULE=settings
if I do not any usage of the manage.py module fails
My manage.py has the following added code:
if "notification" in settings.INSTALLED_APPS:
from notification import models as notification
def create_notice_types(app, created_models, verbosity, **kwargs):
notification.create_notice_type("friends_invite", _("Invitation Received"), _("you have received an invitation"))
notification.create_notice_type("friends_accept", _("Acceptance Received"), _("an invitation you sent has been accepted"))
signals.post_syncdb.connect(create_notice_types, sender=notification)
else:
print "Skipping creation of NoticeTypes as notification app not found"
Any ideas?

Yourmanage.py is referencing an application (notifications). This forces Django to complain about DJANGO_SETTINGS_MODULE being set because the Django environment hasn't been set up yet.
Incidentally, you can force the enviroment setup manually, but honestly I wouldn't do this in manage.py. That's not really a good practice in my opinion.
Here is how you can manually setup the Django environment from within any app (or program for that matter):
# set up the environment using the settings module
from django.core.management import setup_environ
from myapp import settings
setup_environ(settings)

You need to set the DJANGO_SETTINGS_MODULE environment variable because it's how Django knows what your settings module is called (so you can have different ones per project or for testing and development.) You can set it in the scripts themselves before you import django (directly or indirectly) but that won't do much good when you run the Django-provided scripts.
The easiest solution is probably to just set DJANGO_SETTINGS_MODULE in your shell's startup scripts, so you won't have to set it manually anymore. The usual files to add it to are .bash_profile and .bashrc (if you do indeed use bash.)

By default, manage.py looks for a settings module in the same directory as itself. If it doesn't find one, it bombs out with a message to use django-admin.py instead. It doesn't actually set up the environemnt until it runs execute_manager. If you need to run your hooks before calling your management functions, the practice I've seen suggested is to put them in the relevant app's models.py.

Related

How to run django-pytest on a project which imports settings from environment variables defined in manage.py

I'm working on a Django project in which settings such as SECRET_KEY are defined in a .env file, and manage.py sets the environment variable using python-dotenv as follows:
from dotenv import load_dotenv, find_dotenv
if __name__ == "__main__":
load_dotenv(find_dotenv())
# usual manage.py code
Then settings.py simply defines module-level settings from environment variables, for example,
SECRET_KEY = os.environ['SECRET_KEY']
I'm now in the process of switching to pytest-django for unit testing. The problem, however, is that without running python manage.py first, the environment variables don't get set, so I end up with
E KeyError: 'SECRET_KEY'
The way I'm now thinking of working around this is to define a custom action to register with manage.py to run pytest (following https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/). This seems a bit like using a sledgehammer to crack a nut, though. Any suggestions of more elegant ways to go about this problem?
You could use os.getenv('SECRET_KEY'), which will return None if there is no such environment variable. That's equivalent to os.environ.get('SECRET_KEY') and similarly allows an optional second argument for a default value (e.g. os.getenv('SECRET_KEY', 'my-default-key').
This is the approach that most of the projects I've worked on have taken, both for testing and for local development reasons.
Another approach could be to create a test runner for Django as described at https://pytest-django.readthedocs.io/en/latest/faq.html#how-can-i-use-manage-py-test-with-pytest-django

How to execute external script in the Django environment

I am trying to execute an external snippet for debugging and terminal-like purposes in the environment the Django console uses so it can connect to the db, etc.
Basically, I am just using it for the same reason one would fiddle with the console but I am using longer snippets to output some formatted information so it is handy to have that code in an actual file manipulated with an IDE.
An answer said you could do that by executing python manage.py shell < snippet.py but I did not see a successfull result. And although no errors are reported, I am not getting the excepted output, but only a series of >>> prompts.
So how can I do this?
By the way, I am using PyCharm, in case this IDE has a shorthand way of doing this or any special tool.
I would say creating a new Custom management command is the best way to achieve this goal.
But you can run your script in a django environment. I use this sometimes to run a oneoff script or some simple tests.
You have to set the environment variable DJANGO_SETTINGS_MODULE to your settings module and then you have to call django.setup()
I copied these lines from the manage.py script, you have to set the correct settings module!
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.local")
django.setup()
Here is a simple template script which I use sometimes:
# -*- coding: utf-8 -*-
import os
import django
# you have to set the correct path to you settings module
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.local")
django.setup()
from project.apps.bla.models import MyModel
def run():
# do the work
m = MyModel.objects.get(pk=1)
if __name__ == '__main__':
run()
It is important to note that all project imports must be placed after calling django.setup().
Many times you need scripts to play with db or more. But you need to the the django way i.e. the ORM and play with every thing that your project has.
You can checkout https://github.com/django-extensions/django-extensions
You create a scripts folder in your project home and write down a method run . the app provide you a way to run those scripts easily.
How to do it.
Step 1 - Install the package.
Step 2 - Create a scripts folder with init.py in project root, and add 'django_extensions' in your applications
Step 3- Create a file send_email_to_users.py in scripts folder. Simple Example.
from project.models import MyModel
from django.contrib.auth.models import User
from project.utils import send_email
def run():
results = MyModel.objects.all()
for res in results:
send_mail(res.email)
Now from command line you can run
python manage.py runscript send_mail_to_users
If its just a one off script,
import django
django.setup()
from myapp.models.import MyModel
You need to have your environmental variable set up, so its easiest to run it from the IDE (make it part of the same project, right click on teh file and there should be a run option).
If you are looking for something to run on a production environment I would create a management command as suggested by DanEEStar.

Set DJANGO_SETTINGS_MODULE as an Environment Variable in Windows permanently

How can I permanently set the environmental variable DJANGO_SETTINGS_MODULE on WINDOWS on a permanent basis and be done with it?
I mean
Win Button + Pause/Break Button
This leads to Control Panel\System and Security\System
Click Advanced System Settings
Click Environment Variables
There are two boxes the first is titled User variables and the second System variables
On the System variables click the New Button
For variable name put in DJANGO_IMPORT_SETTINGS
XXX--> WHAT DO I PUT IN VARIABLE VALUE TO SET IT ONCE AND FOR ALL?
In the Django Site on this issue it states:
DJANGO_SETTINGS_MODULE
When you use Django, you have to tell it which settings you’re using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.
The value of DJANGO_SETTINGS_MODULE should be in Python path syntax,e.g. mysite.settings. Note that the settings module should be on the Python import search path.
What does it mean ...should be in Python path syntax e.g. mysite.settings... ?
I have a certain directory where my Python is located:
C:\Python27
I have a certain directory where my Django is located: C:\Python27\Lib\site-packages\django
What does this mysite means. What directory is it meanning C:\Something......
Can you put this variable once and for all or you have to constantly change it for every project (I hope not!)
And what does this suspiciously line means Note that the settings module should be on the Python import search path.
All I want it to set the DJANGO_SETTINGS_MODULE environmental variable and be done once and for all from this hassle
EDIT
In order to work, Django just has to be pointed at a valid settings file, and by default it
looks for an environment variable named DJANGO_SETTINGS_MODULE to tell it where to find the
settings. The value of this variable should be the Python import path of the settings file, such
as cms.settings.
--> What king of directory is this: cms.settings? In windows every directory starts with a hard drive as C:\Something...... How can you start a directory like this in Windows?
EDIT_2
Excerpt from a book
PROBLEM
Environment variable DJANGO_SETTINGS_MODULE is undefined.
SOLUTION
Run the command python manage.py shell rather than python.
MY QUESTION --> ON WHAT DIRECTORY?///CAN YOU SET IT FOR ONCE OR IS IT DIFFERENT PER PROJECT?
MY PROJECT IS STRUCTURED LIKE THIS
C:\Python27\pysec-master(file)
|__local_settings.py
|__manage.py
|__settings.py
|__C:\Python27\pysec(file)
|__ __init__.py
|__example.py
|__models.py
|__xbrl.py
|__xbrl_fundamentals.py
I am trying to run models.py and I have a settings.py in the C:\Python27\pysec-master
You can find an exact copy here.
MAYBE_IMPORTANT_EDIT
I have a file called manage.py in my project which has these contents
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Does this has to do anything on setting the variable? Do I need to set here here inside the loop?
EDIT
For the command in the IDLE from django.db import settings do i need to set a directory for the PYTHON_MODULE_SETTINGS like C:\Python27\Lib\site-packages\django\db ?
Okay, don't be so frustrated. Let's look at this step by step:
Python path syntax:
In Python, when you split your code base across modules, you qualify the name of the import with the name of the module. Let's say your project is structured like this:
my_project
|__utils
| |____init__.py
| |__file_utils.py
|__my_module
|____init__.py
|__main.py
In your main.py if you want to access methods you have defined in file_utils.py you add an import statement in your main.py like this:
import utils.file_utils.read_file
assuming read_file is the method you want to import into main.py. This way of importing modules where you have a . separating every module is referred as python path syntax.
PYTHONPATH:
In the above example, the import statement would work only if the Python interpreter knows where to look for the first module namely the utils. Only when it finds utils can it find file_utils and read_file. You specify the list of all the paths you want the interpreter to look into in the environment variable PYTHONPATH. So in order to have an import statement like above in your code, you have to make sure that the full path to your project my_project is in PYTHONPATH. Assuming my_project is in C:\AMAZEBALLS_CODE\my_project you should have C:\AMAZEBALLS_CODE in your PYTHONPATH
DJANGO_SETTINGS_MODULE:
Now let's suppose your my_project is actually a Django application. A Django application needs a settings file where you specify a whole bunch of things. In order to instruct Django which settings file to look into you specify it in DJANGO_SETTINGS_MODULE. Assuming this is your project structure:
my_project
|__utils/
| |____init__.py
| |__file_utils.py
|__my_module/
| |____init__.py
| |__main.py
|__site_settings/
|__dev_settings.py
|__production_settings.py
myroject.site_settings is the module Django has to look into for the settings file. And myroject.site_settings.dev_settings is the value you need to be setting to the DJANGO_SETTINGS_MODULE in the above case. When documentation says cms.settings or mysite.settings they mean cms or mysite is the name of your project and settings.py is the name of your settings file.
Now let's look at your question:
Can you permanently set it in the environment variables of Windows? Sure.
Is it the right way? No.
Because if you want to create another application tomorrow in another location, you will have to edit this in the environment variables section of Windows. Also, it is a practice to use a different settings file for development environment and another one for production. So setting it at one place with one value makes it inflexible. But if you are aware of all of the above and sure you are gonna be using just the one settings file set DJANGO_SETTINGS_MODULE to myproject.site_settings.dev_settings in the env variables section.
Hope this helps!
EDIT:
Looks like you are putting your pysec-master project in C:\Python27. Do not put your projects in the python installation. Create a settings.py file in your project and set DJANGO_SETTINGS_MODULE to pysec-master.settings
You said:
All i want it to set the DJANGO_SETTINGS_MODULE environmental variable
and be done once and for all from this hassle
If you don't want to go through a tedious procedure every time, you don't have to make it permanent, you only need to make the procedure automatic.
Create a bat and put set DJANGO_SETTINGS_MODULE='xxx.settings' inside.
If you are using virtualenv, you can set the environment variable in bin/activate.bat.

django manage.py settings default

I have a settings.py file and a dev_settings.py file that I use to override some values for dev purposes. Everytime I run the ./manage.py command, I have to specify --settings=whatever.local_settings. This becomes very tedious to do every time and I am trying to find a way to force manage.py to load my dev_settings.py file every by default so that I don't have to type that long argument every time I want to run a command.
I have tried setting DJANGO_SETTINGS_MODULE, however, it appears that manage.py overrides this option.
Is it possible to make this happen or am I doomed to always specify that argument?
manage.py sets path to settings for you, that's why it's ignoring DJANGO_SETTINGS_MODULE (it's basically just script that wraps around django-admin.py).
There are 2 easy ways to fix your problem:
set DJANGO_SETTINGS_MODULE and use django-admin.py to run all commands instead of manage.py. This is even better if you use vitualenv.
copy manage.py and name it local.py (that's the name in my case) and rename all settings mentions to dev_settings.
For example:
#!/usr/bin/env python
from django.core.management import execute_manager
import imp
try:
import settings_local
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings_local.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings_local)
You can run all commands by ./local.py now.
The way this is typically done is you have settings.py with all settings that are common between environments (things like INSTALLED_APPS, etc.). Then, you have something like settings_local.py, that defines settings particular to the environment in context. You then import settings_local.py in settings.py.
# settings.py
from settings_local import *
settings.py gets added to your source code repository, but settings_local.py does not. (However, you would normally add something like settings_local.py.example to the repo.)
When you first move your app over to production, for example, you pull down the code base from your repo. You then copy settings_local.py.example to settings_local.py and make any necessary environment specific changes.
You then have separate settings_local.py files in each environment, and it all just works.
You can make a bash alias by adding these lines to your .bash_profile file:
mymanage()
{
python manage.py $1 --settings=settings_debug
}
alias mng=mymanage
Then when you run this command:
mng runserver
settings_debug.py file will be used for settings.
You can use django-admin.py with that environment variable. Commands are interchangeable, only django-admin.py doesn't override the variable you're trying to use.
If a settings file is common to all installation, you can just import it e.g.
from settings_local import *
but usually settings_local are changed and tweaked per installation and as my installation script directly copy files to target sites (without worrying what is local what is not), which mean settings_local may get overwritten, to avoid that I just keep settings_local in parent folder of the installation target and manually import it in settings.py e.g.
local_settings_file = os.path.join(prevFolder, "settings_local.py")
if os.path.exists(local_settings_file):
execfile(local_settings_file)

Sys.path modification or more complex issue?

I have problems with importing correctly a module on appengine. My app generally uses django with app-engine-patch, but this part is task queues using only the webapp framework.
I need to import django settings for the app to work properly.
My script starts with:
import os
import sys
sys.path.append('common/')
# Force Django to reload its settings.
from django.conf import settings
settings._target = None
# Must set this env var before importing any part of Django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
I always get this error, or something related:
<type 'exceptions.ImportError'>: No module named ragendja.settings_pre
because the settings.py file starts with
from ragendja.settings_pre import *
I think I need to add ragendja to sys.path again but I had several tries that didn't work.
Here is my directory:
project/
app.yaml
setting.py
common/
appenginepatch/
ragendja/
setting_pre.py
myapp/
script.py
Is it only a sys.path problem and how do I need to modify it with the correct syntax?
Thanks
App engine patch manipulates sys.path internally. Background tasks bypass that code, so your path will not be ready for Django calls. You have two choices:
Fix the paths manually. The app engine documentation (see the sub-section called "Handling import path manipulation") suggests factoring the path manipulation code into a module that can be imported by your task script.
Eliminate dependencies on django code, if possible. If you can write your task to be pure python and/or google api calls, you're good to go. In your case, this might mean refactoring your settings code.
Why not:
sys.path.append('common/appenginepatch')
since the ragendja is in this directory?

Categories