在我开发的 Python 系统中,我们通常有这种模块结构。
mymodule/
mymodule/mymodule/feature.py
mymodule/test/feature.py
这允许我们的小测试框架轻松导入 test/feature.py 并运行单元测试。但是,我们现在需要一些 shell 脚本(用 Python 编写):
mymodule/
mymodule/scripts/yetanotherfeature.py
mymodule/test/yetanotherfeature.py
Yetanotherfeature.py 由模块 Debian 软件包安装到 /usr/bin 中。但我们显然不想要 .py 扩展名。所以,为了让测试框架仍然能够导入模块,我必须做这个符号链接:
mymodule/
mymodule/scripts/yetanotherfeature
mymodule/scripts/yetanotherfeature.py @ -> mymodule/scripts/yetanotherfeature
mymodule/test/yetanotherfeature.py
是否可以在 Python 中按文件名导入模块,或者您能想到一个更优雅的解决方案吗?