wajika 发表于 2022-1-11 14:51

[已解决] python3 setup.py install 安装的项目如何增加配置文件?

本帖最后由 wajika 于 2022-1-12 14:39 编辑

我下载了某个开源项目,它可以用 python setup.py install   方式安装,我在这个项目代码里引用了一个自定义的modules,其实就是放一个目录(elastalert_modules)到这个项目里,在ubuntu 20.04 里运行时出现下面的错误提示


elastalert.sh
--------------------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/util.py", line 28, in get_module
    base_module = __import__(module_path, globals(), locals(), )
ModuleNotFoundError: No module named 'elastalert_modules'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 516, in load_alerts
    alert_field =
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 516, in <listcomp>
    alert_field =
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 500, in create_alert
    alert_class = self.alerts_mapping.get(alert) or get_module(alert)
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/util.py", line 31, in get_module
    raise EAException("Could not import module %s: %s" % (module_name, e)).with_traceback(sys.exc_info())
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/util.py", line 28, in get_module
    base_module = __import__(module_path, globals(), locals(), )
elastalert.util.EAException: Could not import module elastalert_modules.wechat_qiye_alert.WeChatAlerter: No module named 'elastalert_modules'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 156, in load
    rule = self.load_configuration(rule_file, conf, args)
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 219, in load_configuration
    self.load_modules(rule, args)
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 483, in load_modules
    rule['alert'] = self.load_alerts(rule, alert_field=rule['alert'])
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 519, in load_alerts
    raise EAException('Error initiating alert %s: %s' % (rule['alert'], e)).with_traceback(sys.exc_info())
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 516, in load_alerts
    alert_field =
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 516, in <listcomp>
    alert_field =
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 500, in create_alert
    alert_class = self.alerts_mapping.get(alert) or get_module(alert)
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/util.py", line 31, in get_module
    raise EAException("Could not import module %s: %s" % (module_name, e)).with_traceback(sys.exc_info())
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/util.py", line 28, in get_module
    base_module = __import__(module_path, globals(), locals(), )
elastalert.util.EAException: Error initiating alert ['elastalert_modules.wechat_qiye_alert.WeChatAlerter']: Could not import module elastalert_modules.wechat_qiye_alert.WeChatAlerter: No module named 'elastalert_modules'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/bin/elastalert", line 11, in <module>
    load_entry_point('elastalert2==2.2.1', 'console_scripts', 'elastalert')()
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/elastalert.py", line 2151, in main
    client = ElastAlerter(args)
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/elastalert.py", line 132, in __init__
    self.rules = self.rules_loader.load(self.conf, self.args)
File "/usr/local/lib/python3.8/dist-packages/elastalert2-2.2.1-py3.8.egg/elastalert/loaders.py", line 164, in load
    raise EAException('Error loading file %s: %s' % (rule_file, e))
elastalert.util.EAException: Error loading file /home/wajika/elastalert2/es_rules/bpm.yaml: Error initiating alert ['elastalert_modules.wechat_qiye_alert.WeChatAlerter']: Could not import module elastalert_modules.wechat_qiye_alert.WeChatAlerter: No module named 'elastalert_modules'

如果我进入包含elastalert_modules目录的位置,在运行elastalert.sh ,那么就不会提示No module named 'elastalert_modules',这个问题怎么理解? 我需要创建一个PYTHONPATH?

ubuntu里用自带的python的话 怎么将elastalert_modules目录放到 setup.py install 安装的位置?


52小柯柯 发表于 2022-1-11 16:26

os.path.append("这个模块的路径")

yjn866y 发表于 2022-1-11 19:30

1.import sys
2.sys.path.append(’这个模块的路径')

wajika 发表于 2022-1-12 14:39

问题解决了,因为在ubuntu中 普通用户和root 所安装的python 包位置不一样
页: [1]
查看完整版本: [已解决] python3 setup.py install 安装的项目如何增加配置文件?