Atom`script`附加组件在运行脚本时无法识别Django模型/设置 [英] Atom `script` add-on doesn't recognize Django Model/settings when running a script
问题描述
当我尝试使用原子附加组件script
在基于Django的Web应用程序中运行python脚本时,似乎遇到了一些依赖项问题.
It seems I run into some dependencies issues when trying to run a python script within my Django based web application using the atom add-on script
.
我想使用Atom script
附加组件运行以下脚本:
I would like to run the following script using the Atom script
add-on:
feeder.py:
import zmq
import time
from time import sleep
import uuid
from models import AccountInformation
context = zmq.Context()
zmq_socket = context.socket(zmq.PULL)
zmq_socket.bind("tcp://*:32225")
time.sleep(1)
while True:
try:
msg = zmq_socket.recv_string()
data = msg.split("|")
print(data)
if (data[0] == "account_info"):
version = data[1]
DID = uuid.UUID(data[2])
accountNumber = int(data[3])
broker = data[4]
leverage = data[5]
account_balance = float(data[6])
account_profit = float(data[7])
account_equity = float(data[8])
account_margin = float(data[9])
account_margin_free = float(data[10])
account_margin_level = float(data[11])
account_currency = data[12]
feed = AccountInformation(
version=version,
DID=DID,
accountNumber=accountNumber,
broker=broker,
leverage=leverage,
account_balance=account_balance,
account_pofit=account_profit,
account_equity=account_equity,
account_margin=account_margin,
account_margin_free=account_margin_free,
account_margin_level=account_margin_level,
account_currency=account_currency
)
feed.save()
# Push data to account information table
else:
print("no data")
except zmq.error.Again:
print("\nResource timeout.. please try again.")
sleep(0.000001)
不幸的是,它引发了以下错误:
Unfortunately it raises the following error:
Traceback (most recent call last):
File "C:\Users\Jonas Blickle\Desktop\dashex\Dashboard_app\feeder.py", line 5, in <module>
from models import AccountInformation
File "C:\Users\Jonas Blickle\Desktop\dashex\Dashboard_app\models.py", line 7, in <module>
class AccountInformation(models.Model):
File "C:\Program Files\lib\site-packages\django\db\models\base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Program Files\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "C:\Program Files\lib\site-packages\django\apps\registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "C:\Program Files\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
self._setup(name)
File "C:\Program Files\lib\site-packages\django\conf\__init__.py", line 60, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
[Finished in 0.302s]
当我删除模型导入时,一切都运行良好,由于我需要导入的模型,所以它不会填充我的数据库...
When I remove the model import everything just runs fine, it just won't populate my DB then since I need the imported model...
如何解决这个问题?
推荐答案
您的模型位于应用程序内部,而您的应用程序位于设置(INSTALLED_APPS
)内,因此,在访问django的设置之前,应先对其进行配置.
Your models are inside your apps, and your apps are inside your settings (INSTALLED_APPS
), so you should configure the django's settings before you can access them.
只需在导入模型之前添加这些:
Just add these before importing your models:
import django
django.setup()
还应该设置DJANGO_SETTINGS_MODULE
环境变量以指定设置文件;或根据需要使用django.configure
(
You should also set DJANGO_SETTINGS_MODULE
environment varialbe to specify your settings file; or use django.configure
if you prefer (docs).
这篇关于Atom`script`附加组件在运行脚本时无法识别Django模型/设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!