Atom `script` add-on doesn't recognize Django Model/settings when running a script

◇◆丶佛笑我妖孽 提交于 2020-01-11 13:09:02

问题


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.

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...

How to possibly solve this?


回答1:


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()

You should also set DJANGO_SETTINGS_MODULE environment varialbe to specify your settings file; or use django.configure if you prefer (docs).



来源:https://stackoverflow.com/questions/59152742/atom-script-add-on-doesnt-recognize-django-model-settings-when-running-a-scri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!