如何将模块用作独立的CLI以及另一个模块(API)的子模块?

2024-09-28 21:25:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我想弄清楚如何让一个模块独立运行(作为CLI)和作为另一个模块的子模块运行(作为API)。你知道吗

最小示例:http://www.filedropper.com/submoduleconfusion-master


对于文件结构:

  • 子模块融合/
    • 型号a/
      • 模式

假设有一个函数位于SubmoduleConfusion.mod_a.mod_a中,那么我将使用from mod_a.mod_a import f


由于modèa也可以作为自己的独立模块运行

对于文件结构:

  • 型号a/
    • 模式

from mod_a import f


Tags: 模块文件fromimportcomapimodhttp
1条回答
网友
1楼 · 发布于 2024-09-28 21:25:33

最好的办法是使模块pip可安装(添加设置.py外部(从模块路径向上1步),带有控制台脚本的入口点。你知道吗

https://github.com/jtara1/misc_scripts/tree/master/misc_scripts/templates

我用这个模板设置.py以及设置.cfg在我创建的大部分模块中。它使用setuptools\u scm,它将使用项目的git版本(标记)来定义项目中的版本设置.py. 你知道吗

换句话说,您需要在尝试安装模块或上载模块之前git tag 0.1.0git push tags。你知道吗

我的设置.py还将推断模块的名称与父目录的名称相同(github项目名称)

所以目录应该是这样的

我的\u模块

  • 图像
  • 我的\u模块
    • __主\uuuuuuuuuuuy.py
  • 你知道吗设置.py你知道吗
  • 你知道吗设置.cfg你知道吗
  • 你知道吗自述文件.md你知道吗

当前在中的设置函数中定义的别名设置.py,以后可能会将其移动到变量

https://github.com/jtara1/misc_scripts/blob/master/misc_scripts/templates/setup.py#L113

相关问题 更多 >