我想弄清楚如何让一个模块独立运行(作为 CLI)并作为另一个模块的子模块(作为 API)。
最小示例:http : //www.filedropper.com/submoduleconfusion-master
对于文件结构:
假设有一些函数位于SubmoduleConfusion.mod_a.mod_a
然后我会使用from mod_a.mod_a import f
由于 mod_a 也可以作为它自己的独立模块运行,
对于文件结构:
from mod_a import f
最好的办法是使用控制台脚本的入口点使模块 pip 可安装(在模块路径的 1 步之外添加 setup.py)。
https://github.com/jtara1/misc_scripts/tree/master/misc_scripts/templates
我在我创建的大部分模块中都使用了这个模板 setup.py 和 setup.cfg。它使用 setuptools_scm,它将使用来自项目的 git 版本(标签)来定义 setup.py 中的版本。
换句话说,你需要git tag 0.1.0
和git push --tags
想要安装的模块或上传到安装之前。
我的setup.py也会推断出模块的名字和父目录同名(github项目名)
所以目录应该是这样的
我的模块
当前在 setup.py 中的 setup 函数中定义的别名的名称,稍后可能会将其移动到变量
https://github.com/jtara1/misc_scripts/blob/master/misc_scripts/templates/setup.py#L113
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句