我有一个项目是这样组织的:
├── main.py
└── scripts
├── __init__.py
├── script1.py
├── script2.py
├── script3.py
├── .
├── .
├── .
└── script30.py
每个script{i}.py
只包含一个函数foo
。我想要实现的是main.py
中这些foo的dict以及相应的模块名称:
result = {'script1': scripts.script1.foo, ..., 'scripts30': scripts.script30.foo}
我也希望实现的是
i
导入script{i}
而执行此操作main.py
内部(将来scripts
中的模块数量可能会增加,每次都要更新main.py
会让人恼火)李>这可能吗?我所有的方法都导致了纯粹的丑陋。 我也欢迎重组我的项目的建议
您正在描述stdlib^{} 的基本用法,实际上非常类似于他们为动态发现模块而展示的文档化用例
在这里,它适用于您的示例项目:
也许,您的项目更适合使用entry-points。每个子模块都可以将其} 或^{} 中的更高级别API对其进行迭代
foo
注册为命名组中的入口点,然后您可以使用setuptools ^{包的
__init__.py
脚本可以执行导入包目录中的模块等操作这意味着您可以这样做:
__int__.py
:这将允许您在
main.py
脚本中获得所需的结果,因此:样本输出:
相关问题 更多 >
编程相关推荐