I
I
Igor Che2017-03-31 07:51:16
Django
Igor Che, 2017-03-31 07:51:16

How to import Django settings from a standalone script?

I am writing a script separately from Dzhangi, I don’t want to import too much into it. Only the Django settings from settings_local are needed.
The structure is:
apps
- __init__.py
- settings_local.py
standalone
- __init__.py
- syncraw.py
In syncraw.py I tried different options from the following:

from .settings_local import DATABASES
from ..settings_local import DATABASES

I get the error ValueError: Attempted relative import in non-package

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
marazmiki, 2017-03-31
@chewarer

A working (but incorrect) answer is to add the directory that contains both apps and standalone to PYTHONPATH.
This is done in two ways: either through the PYTHONPATH environment variable when launched from the console:
or by adding the directory directly in the script:

# syncraw.py
import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from apps.settings_local import *
print(DEBUG)

The correct answer is not to do it at all. And refuse collective farm with settings_local

I
Igor Lyutoev, 2017-03-31
@loader777

If the script is associated with a Django project, it is correct to do the managment command - https://docs.djangoproject.com/en/1.10/howto/custo...

D
devalone, 2017-03-31
@devalone

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectName.settings')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question