构建由3个子程序组成的程序的正确方法

问题描述 投票:5回答:2

我正在建立一个由3个程序组成的系统,让它们分别称为A,B和C。现在我认为我的文件结构是一场灾难。

在我的根文件夹中,我有一些运行程序的.bat文件的快捷方式,还有一个名为program_data的文件夹。在该文件夹中,我有4个单独的文件夹,每个程序一个,再加上一个公用文件夹。问题是我需要每个程序以及这些程序中的子脚本才能从公用文件夹导入,并且我需要程序C和B才能调用程序A API。

目前,我在子文件中附加了sys.path以便从更高级别导入函数。”>

构造这样的东西的正确方法是什么?

当前结构:

root
├── Configuration.lnk
├── Documentation.lnk
├── Program A.lnk
├── Program B.lnk
├── Program C.lnk
├── programs_data
│   ├── Program A
│   │   ├── Program A API.py
│   │   ├── Program A.bat
│   │   ├── Program A.py
│   │   ├── src
│   │   │   ├── server.py
│   │   │   ├── test_functions.py
│   │   │   └── validation.py
│   │   └── targets
│   │       ├── sql_querys
│   │       │   ├── query1.sql
│   │       │   ├── query2.sql
│   │       │   └── queryn.sql
│   │       ├── target1.py
│   │       ├── target2.py
│   │       ├── target3.py
│   │       └── targetn.py
│   ├── Program B
│   │   ├── Program B.bat
│   │   ├── Program B.py
│   │   ├── classifiers
│   │   │   ├── classifier1.py
│   │   │   ├── classifier2.py
│   │   │   └── classifiern.py
│   │   ├── events.log
│   │   ├── o365_token.txt
│   │   └── src
│   │       ├── batchECImport.py
│   │       ├── classifier.py
│   │       └── logger.py
│   ├── Program C
│   │   ├── Program C.bat
│   │   ├── Program C.py
│   │   ├── Reports
│   │   │   ├── report 1
│   │   │   │   └── report.py
│   │   │   └── report 2
│   │   │       └── report.py
│   │   ├── o365_token.txt
│   │   ├── schedule.xlsx
│   │   └── src
│   │       └── report.py
│   └── common
│       ├── APIMailboxManager.py
│       ├── Documentation
│       │   └── Documentation.pdf
│       ├── FlexibleProcess.py
│       ├── config.py
│       ├── misc.py
│       ├── print_functions.py
│       ├── production_support_passwords.py
│       ├── reports_log.db
│       └── reports_log.py
└── schedule spreadsheet.Ink

谢谢!

我正在建立一个由3个程序组成的系统,让我们分别称为A,B和C。现在我认为我的文件结构是一场灾难。在我的根文件夹中,我有一些运行....>

python-3.x file design-patterns structure
2个回答
1
投票

您可以使用__init__.py文件在每个目录中配置模块的导入路径。文档here。在这些文件中,您只应将相对路径添加到common文件夹。如果您想避免重复的代码,我认为没有其他方法可以拥有一个公用文件夹...


0
投票

我建议将您的根文件夹放在PYTHONPATH环境变量(https://www.tutorialspoint.com/What-is-PYTHONPATH-environment-variable-in-Python)中,这样您就不必追加到sys.path。

© www.soinside.com 2019 - 2024. All rights reserved.