I created a sub-directory of my Django project called bin
where I want to put all command-line run Python scripts. Some of these scripts need to import my Django project settings.py
file that is in a parent directory of bin
.
How can I import the settings.py
file from a sub-directory of the project?
The code that I use in my command-line script to set into the "Django context" of the project is:
from django.core.management import setup_environ
import settings
setup_environ(settings)
This works fine if the script is in the root directory of my project.
I tried the following two hacks to import the settings.py
file and then setup the project:
import os
os.chdir("..")
import sys
sys.path = [str(sys.path[0]) + "/../"] + sys.path
The cruel hack can import settings.py
, but then I get the error:
project_module = __import__(project_name, {}, {}, [''])
ValueError: Empty module name