2π
You can do this in the python manage.py shell
, since that would have settings.py
in .
directory.
Otherwise:
Consider settings.py
just another python library and you need to ensure its added to sys.path
and then you can do as follows :
import settings
from django.core.management import setup_environ
setup_environ(settings) # Setting up the env settings
This sets up the settings to your environment. [ This is the right way ]
There is no secret recipe in settings.py
and since django in itself is a library to python, so it could not be default set any paths to your sys.path
unless imported and executed β as I suggested.
1π
You should really use setup_environ
as suggested in another answer.
A hint regarding this specific exception: DJANGO_SETTINGS_MODULE
is a Python module name, so it may not end with β.pyβ. Usually, this would be your_django_site.settings
and the module/directory your_django_site
has to be in sys.path
.
- [Django]-Django rest framework viewset permission based on method
- [Django]-Dynamically connecting Django Q objects with AND and OR
- [Django]-Django download file not working
0π
All Django code not in a project should either be run from the Django REPL, available via ./manage.py shell
, or run as a standalone script.
- [Django]-Django: how to map the results of a raw sql query to model instances in admin list view?
- [Django]-Checkboxes and Radio buttons in Django ModelForm
- [Django]-How do I move project folders in PyCharm Project View?
- [Django]-Is there a Django template language equivalent to Pythonβs range()?
- [Django]-Looping through to create a tuple
0π
In order to test anything in the shell for Django, you should use the Django shell:
python manage.py shell
This will start up a Python interpreter which will magically have your settings file (and the rest of your project) on the Python path.
(This assumes you have a project already. If not, use django-admin.py startproject my_project_name
.)
- [Django]-Reverse Not Found: Sending Request Context in from templates
- [Django]-Can I have 2 Django sites using 2 different version of Python on 1 domain?
- [Django]-How to customize `FileField` buttons in Django template?
- [Django]-Django psycopg2.errors.InvalidSchemaName: no schema has been selected to create in