无法 运行 python 文件

Unable to run python file

我一直在尝试 运行 一个 python 脚本,但我不断收到以下错误。

错误:

Traceback (most recent call last):
  File "cloud_copasi/background_daemon/cloud_copasi_daemon.py", line 18, in <module>
    django.setup()
  File "/Users/cloudcopasi/cloud-copasi/venv/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/cloudcopasi/cloud-copasi/venv/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/Users/cloudcopasi/cloud-copasi/venv/lib/python3.8/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'web_interface'

我正在尝试 运行 (cloud_copasi_daemon) 的脚本文件是:

import sys, time
import django
django.setup()

from tools.daemon import Daemon
import tools.background_script
from tools.response import RemoteLoggingResponse
from cloud_copasi import settings
import logging

log=logging.getLogger(__name__)

class MyDaemon(Daemon):

    #Set the level we wish to log at. Logs are sent back to the central server
    #Choices are all, debug, info, error, none



    def __init__(self, *args, **kwargs):


        return super(MyDaemon, self).__init__(*args, **kwargs)

    def stop(self, *args, **kwargs):

        return super(MyDaemon, self).stop(*args, **kwargs)

    def run(self):
        log.debug('Daemon running')

        while True:
            min_repeat_time = settings.DAEMON_POLL_TYME #Seconds
            start_time = time.time()

            try:

                tools.background_script.run()

                log.debug('Background script finished')

            except Exception as e:
                log.exception(e)



            finish_time = time.time()
            difference = finish_time - start_time

            if difference < min_repeat_time:
                time.sleep(min_repeat_time - difference)



if __name__ == "__main__":
    daemon = MyDaemon('/tmp/Cloud-COPASI.pid')
    if len(sys.argv) == 2:
        if 'start' == sys.argv[1]:
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
        else:
            print ("Unknown command")
            sys.exit(2)
        sys.exit(0)
    else:
        print("usage: %s start|stop|restart" % sys.argv[0])
        sys.exit(2)

"web_interface" 是 Django 应用程序,我已验证引用它的路径是正确的。我不知道我还需要在哪里修复文件路径才能使这个 python 脚本正常工作。

我在 Mac OS Big Sur (运行ning Python 3.8) 和 Linux CentOS(运行宁Python3.6).

非常感谢任何帮助。

当您尝试像这样 bootstrap django 时,您需要确保设置环境变量 PYTHONPATH 以包含 web_interface 所在的文件夹。我猜你的 cloud_copasi_daemon.pyweb_interface 在不同的文件夹中,所以当你 运行 python cloud_copasi_daemon.py 它会在你调用脚本的直接文件夹中查找并且可以'没找到。