I have a error when I try to add a new path to sys.path
of python from a file in a django project. I'm doing this because I need create a scripts of python to include a csv file in the database. I have a app out of the general project like this:
/project/Scripts/file.py
and the general project is like this:
/project/project/
Namely, I need include in the path an app that it's in the general project, to use the models of the this app.
The lines that I have in the file are the follow:
import sys
sys.path.append('absolut path')
from app import models
from app.models import model
The error that appear when I ejecute this file from the terminal is:
user:~/repositorios/project/Scripts$ python file.py
Traceback (most recent call last):
File "file.py", line 8, in <module>
from app import models
File "path", line 1, in <module>
from django.db import models
File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 52, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 45, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.user:~/repositorios/project/Scripts$ python file.py
Traceback (most recent call last):
File "file.py", line 8, in <module>
from app import models
File "path", line 1, in <module>
from django.db import models
File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 52, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 45, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I have the project structure like this:
- Page
- Scripts
- File.py
- Page
- app
- init.py
- models.py
- tests.py
- views.py
- app
- Scripts
I'm writting the code in "File.py" at Scripts, and I want to use the models of "app" into the folder "Scripts".
When I try with (in File.py)
'Import app'
'from app import models'
appear a error that says: "Module 'app' not exists".
I try write the path with (in File.py):
import sys
sys.path.append(path)
import app
from app import models
How I can to use the models of app in File.py?