gpt4 book ai didi

python 3 项目结构(单机与单机)多个模块

转载 作者:行者123 更新时间:2023-12-01 02:14:04 25 4
gpt4 key购买 nike

在 python 项目中,我有以下目录结构

├── cooccurrence
│   ├── cooccurrence.py
│   ├── __init__.py
├── README.md
└── tests
├── __init__.py
└── test_coccurrence.py

这导致我的测试源文件中的测试代码具有相当仪式性的行:

from cooccurrence.cooccurrence import CoCreate

如果我只需要一个模块,我将如何简化整个设置,相反,我应该采用什么样的项目结构来管理同一包下的多个模块?

为了进行测试,我只需使用 python -m unittest discovery -v,如果有一个解决方案也可以在 PyCharm 中无缝启用该项目,我们将非常感激。

最佳答案

您可以导入 __init__.py 中的文件,这样它将在包级别可用。例如,您可以在 cooccurrence/__init__.py 中执行以下操作:

from cooccurrence import CoCreate

然后在您的测试文件中:

from cooccurrence import CoCreate

这将是 Pythonic 的方式

关于python 3 项目结构(单机与单机)多个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48516002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com