我正在写一堆python脚本,它们都在一起工作,例如tool1.py将一些数据加载到数据库表中,tool2.py从这个表读取,做一些计算并将结果写入另一个表,以及daemon1.py是一个提供结果的Web服务器。每个工具和守护进程都有一大堆支持文件(只有那一个工具才需要),并且需要自己的目录。另外,我确实在所有工具之间共享了一些代码,例如config.py和database.py。直觉上,我构建了这个项目:
/README.md
/requirements.txt
/config.py # contains shared configuration
/database.py # shared code to connect to the database
/tool1/
tool1.py # entrypoint
[...] # bunch of other files only needed by tool1
/tool2/
tool2.py #entrypoint
[...] # bunch of other files only needed by tool2
/deamon1/
daemon1.py #entrypoint
[...] # bunch of other files only needed by daemon1
然后我使用命令python tool1/tool1.py
运行我的工具和守护进程。但问题是tool1.py如何访问config.py/database.py。我考虑了以下选项,对python中被认为是“正确”的方式感兴趣,或者我可能错过的任何替代方案(也许是一种不同的方式来布局项目)。额外的业力将获得奖励,以获得权威答案的链接。
sys
和os
除了设置这些项目之外没有其他原因的模块。import os
import sys
sys.path.append(os.path.join(os.path.abspath(
os.path.dirname(__file__)), ".."))
from tool1dir import tool1
tool1.run()
如上所述,我希望听到pythonesque的方式,或任何建议或偏好。