首页 > 解决方案 > 无法运行python文件

问题描述

我一直在尝试运行 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(运行 Python 3.8)和 Linux CentOS(运行 Python 3.6)上遇到了同样的问题。

任何帮助深表感谢。

标签: python-3.xdjangoapachedaemon

解决方案


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


推荐阅读