问题
I have a python script within my Django project designed to run seperate from the Django app. I want to use the settings.py on my Django App how can I do that.
When I try to import
from django.conf import settings
i get
ImportError: No module named DjangoTastypie.settings
My project Structure
I am running using eclipse-> Run as python
回答1:
Read https://docs.djangoproject.com/en/1.9/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage
So you basically will need to put this at the beginning of your script
import os
import django
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
回答2:
Based on @Sardorbek Imomaliev, you should also make your DjangoTastypie in your PYTHONPATH, you can do this in your script.
import os
import sys
import django
from django.conf import settings
sys.path.append("path/to/DjangoTastypie") # path to the parent dir of DjangoTastypie
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
来源:https://stackoverflow.com/questions/38196334/import-django-settings-from-external-script