2

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

enter image description here

I am running using eclipse-> Run as python

Dhanushka Amarakoon
  • 3,502
  • 5
  • 31
  • 43

2 Answers2

5

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()
ziXiong
  • 76
  • 3
2

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()
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
  • Hi I get the same error. `import os import django from django.conf import settings os.environ['DJANGO_SETTINGS_MODULE'] = 'DjangoTastypie.settings' django.setup()` and I get `ImportError: No module named DjangoTastypie.settings` – Dhanushka Amarakoon Jul 05 '16 at 08:35