无法在python脚本中导入Django模型

时间:2021-01-05 01:14:05

My code is here. I have tried different approach from * and non of them worked.

我的代码在这里。我尝试过*的不同方法,而且没有工作。

import os
import sys
from django.conf import settings

sys.path.append('/var/www/iaas/horizon')
sys.path.append('/var/www/iaas/horizon/openstack_dashboard')
os.environ['DJANGO_SETTINGS_MODULE'] = 'openstack_dashboard.settings'

from bill.models import MonthlyBills
from django.contrib.auth import models

If I run python daemonize.py, here is the error message I get. I am confused because I have already included my django project path in my sys.path

如果我运行python daemonize.py,这是我收到的错误消息。我很困惑,因为我已经在我的sys.path中包含了我的django项目路径

raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'openstack_dashboard.settings' (Is it on sys.path?): cannot import name connection

What I am trying to achieve is to create a python-daemon, I need to have an access in my django models.

我想要实现的是创建一个python-daemon,我需要在我的django模型中访问。

I hope someone who could point me where I am mistaking here.

我希望有人能指出我在这里误会的地方。

1 个解决方案

#1


1  

You have to set up os.environ['DJANGO_SETTINGS_MODULE'] before you import settings.

在导入设置之前,必须设置os.environ ['DJANGO_SETTINGS_MODULE']。

The process of importing django.conf.settings will look to see if the DJANGO_SETTINGS_MODULE environment variable is set before determining white settings to load.

在确定要加载的白色设置之前,导入django.conf.settings的过程将查看是否设置了DJANGO_SETTINGS_MODULE环境变量。

import os
import sys

sys.path.append('/var/www/iaas/horizon')
sys.path.append('/var/www/iaas/horizon/openstack_dashboard')
os.environ['DJANGO_SETTINGS_MODULE'] = 'openstack_dashboard.settings'

from django.conf import settings

#1


1  

You have to set up os.environ['DJANGO_SETTINGS_MODULE'] before you import settings.

在导入设置之前,必须设置os.environ ['DJANGO_SETTINGS_MODULE']。

The process of importing django.conf.settings will look to see if the DJANGO_SETTINGS_MODULE environment variable is set before determining white settings to load.

在确定要加载的白色设置之前,导入django.conf.settings的过程将查看是否设置了DJANGO_SETTINGS_MODULE环境变量。

import os
import sys

sys.path.append('/var/www/iaas/horizon')
sys.path.append('/var/www/iaas/horizon/openstack_dashboard')
os.environ['DJANGO_SETTINGS_MODULE'] = 'openstack_dashboard.settings'

from django.conf import settings