【问题标题】:Atom `script` add-on doesn't recognize Django Model/settings when running a script运行脚本时,Atom `script` 插件无法识别 Django 模型/设置
【发布时间】:2020-03-27 21:09:52
【问题描述】:

在尝试使用 atom 附加组件 script 在基于 Django 的 Web 应用程序中运行 python 脚本时,我似乎遇到了一些依赖问题。

我想使用 Atom script 插件运行以下脚本:

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)

不幸的是,它引发了以下错误:

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]

当我删除模型导入时,一切都运行良好,它不会填充我的数据库,因为我需要导入的模型...

如何解决这个问题?

【问题讨论】:

标签: python django atom-editor


【解决方案1】:

您的模型在您的应用中,您的应用在您的设置中 (INSTALLED_APPS),因此您应该先配置 django 的设置,然后才能访问它们。

只需在导入您的模型之前添加这些:

import django
django.setup()

您还应该设置DJANGO_SETTINGS_MODULE 环境变量来指定您的设置文件;如果您愿意,也可以使用django.configure (docs)。

【讨论】:

  • 还有一个问题,因为我刚刚遇到了上述解决方案的新问题。如何设置DJANGO_SETTINGS_MODULE?根据文档,我试过django-admin set DJANGO_SETTINGS_MODULE=dashex.settings,但上面写着no module named dashex。另请参阅stackoverflow.com/questions/59157787/… 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
相关资源
最近更新 更多