1

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

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?

  • Maybe this link can help your http://stackoverflow.com/questions/1017909/python-sys-path-modification-not-working – catherine Mar 05 '13 at 16:43

2 Answers2

0
foo.bar.__path__.append('sample/foo/bar')
from foo.bar import app
catherine
  • 22,492
  • 12
  • 61
  • 85
0

You are setting the sys.path correctly, but because you are dealing with Django models you need to also import your settings.

The tracebacks should give you some indication of what went wrong. Specifically this line:

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

Check out this link for a couple of different ways to correctly import Django models.

Necrolyte2
  • 738
  • 5
  • 13