我正在编写一堆一起工作的 python 脚本,例如 tool1.py 将一些数据加载到数据库表中,tool2.py 从此表中读取数据,进行一些计算并将结果写入另一个表中,以及 daemon1.py是一个提供结果的网络服务器。每个工具和守护程序都有一大堆支持文件(仅该一个工具需要),并且需要自己的目录。此外,我确实有一些在所有工具之间共享的代码,例如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
Run Code Online (Sandbox Code Playgroud)
然后,我使用命令运行我的工具和守护程序python tool1/tool1.py。然而这里的问题是 tool1.py 如何访问 config.py/database.py。我考虑了以下选项,对 python 中被认为是“正确”的方式感兴趣,或者我可能错过的任何替代方案(可能是布置项目的不同方式)。提供权威答案的链接将获得额外的业力奖励。
sys和os模块,除了设置这些项目外没有其他原因。/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
Run Code Online (Sandbox Code Playgroud)
import os
import sys
sys.path.append(os.path.join(os.path.abspath(
os.path.dirname(__file__)), ".."))
Run Code Online (Sandbox Code Playgroud)
如前所述,想听听Python式的方法来做到这一点,或者任何建议或偏好。
Duc*_*sEL -1
来自 Shinbero 的另一个问题:
import sys
sys.path.insert(0,'..')
import database
Run Code Online (Sandbox Code Playgroud)
可以通过添加以下内容将目录返回到其位置:
sys.path.insert(0,'tool1') #tool1 is replaced with your directory you are calling from.
Run Code Online (Sandbox Code Playgroud)
使用第三种方法的变体来解决这个问题。
| 归档时间: |
|
| 查看次数: |
511 次 |
| 最近记录: |