Python:具有多个包的项目,但我想要一个配置文件

标签 python setup-project directory-structure

我有一个要分发的项目,其中包含多个子包。一种用于清理数据,一种用于转换数据,另一种用于计算一些统计数据。我想避免有 3 个 config.py 文件,我可以将 config.py 文件放在顶层吗?例如,不要这样做:

MyPackage
|
├──mypackage
|     |
|     ├── __init__.py
|     |
|     ├── data_clean
|     |   ├── __init__.py
|     |   ├── config.py
|     |   └── f1.py
|     |
|     ├── data_transform
|     |   ├── __init__.py
|     |   ├── config.py
|     |   └── g1.py
|     |
|     └── stat_calc
|        ├── __init__.py
|        ├── config.py
|        ├── s1.py
|        └── command_line_interface.py # <- users will use this from cmd.exe
|      
├── README.txt    
|
└── setup.py

我想要:

MyPackage
|
├──mypackage
|     |
|     ├── __init__.py
|     |
|     ├── config.py  # <---- put all 3 config.py stuff into here
|     |
|     ├── data_clean
|     |   ├── __init__.py
|     |   └── f1.py
|     |
|     ├── data_transform
|     |   ├── __init__.py
|     |   └── g1.py
|     |
|     └── stat_calc
|         ├── __init__.py
|         ├── s1.py
|         └── command_line_interface.py # <- users will use this from cmd.exe
| 
├── README.txt    
|
└── setup.py

这有意义吗?这是Pythonic/正确的吗?

最佳答案

假设你的模块将存储在 PYTHONPATH 中,答案是肯定的。只需导入它:

从 MyPackage 导入配置

关于Python:具有多个包的项目,但我想要一个配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43370861/

相关文章:

python - 在python中将列表保存为带有两位小数的csv?

Python 3 小数精度

.net - 尝试加载自定义配置时 Visual Studio 安装和部署项目中的 FileNotFoundException

c# - 当我打开解决方案时,如何让我的 VS2010 安装项目没有错误?

python - 序列化公钥时为"Could not deserialize key data"

python - 马里奥在pygame中跑过屏幕太快

visual-studio-2008 - 使用 Visual Studio 2008 指定 Windows 应用程序的图标 - 初学者的问题

perl - 如何在 Perl 中复制整个目录?

linux - 如何在 Linux 中创建一个列为降序子目录的 txt 文件?

python - 我应该将我的 python 文件放在 venv 文件夹中的什么位置?