导入模型应用时出现错误,路径层级降低一个级别
我在尝试从一个 Django 项目中的文件里添加新的路径到 sys.path
时遇到了错误。我这样做是因为我需要写一个 Python 脚本,把一个 CSV 文件导入到数据库里。我有一个独立于主项目的应用,结构如下:
/project/Scripts/file.py
而主项目的结构是这样的:
/project/project/
也就是说,我需要把主项目中的一个应用添加到路径里,以便使用这个应用的模型。
我在文件里写的代码如下:
import sys
sys.path.append('absolut path')
from app import models
from app.models import model
当我从终端执行这个文件时,出现的错误是:
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.
我的项目结构是这样的:
- Page
- Scripts
- File.py
- Page
- app
- init.py
- models.py
- tests.py
- views.py
- app
- Scripts
我在 "File.py" 里写代码,想在 "Scripts" 文件夹中使用 "app" 的模型。
当我尝试在 "File.py" 中这样写:
'Import app'
'from app import models'
出现的错误是:“模块 'app' 不存在”。
我尝试在 "File.py" 中这样写路径:
import sys
sys.path.append(path)
import app
from app import models
我该如何在 "File.py" 中使用 "app" 的模型呢?
2 个回答
0
你已经正确设置了 sys.path,但因为你在使用 Django 模型,所以还需要导入你的设置。
错误信息应该能给你一些提示,告诉你哪里出错了。特别是这一行:
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
可以看看这个 链接,里面有几种正确导入 Django 模型的方法。
0
这段内容是关于编程的讨论,主要涉及一些技术问题和解决方案。这里有一些代码示例和建议,帮助大家更好地理解和解决问题。
如果你在编写代码时遇到困难,或者对某个技术概念不太明白,可以参考这些讨论和代码块。它们提供了一些实用的技巧和思路,适合初学者学习和掌握编程的基本知识。
记得多动手实践,尝试运行这些代码,看看它们是如何工作的,这样你会学得更快。
foo.bar.__path__.append('sample/foo/bar')
from foo.bar import app