具有多个同名模块的 Python 项目
我有两个具有相同名称的不同包,每个包都有一组彼此名称相同的模块和类,但它们的实现方式不同。设置包/模块结构最合理的方式是什么?
现在我正在做类似的事情:
Common
utilities.py
VersionA
Package
moduleX.py
moduleY.py
VersionB
Package
moduleX.py
moduleY.py
我要求使用模块的环境,只需将路径设置为指向“Package”的正确版本。
最重要的是,两个包共享一个模块! “实用程序.py”。现在我还要求安装程序将 Utilties.py 添加到路径中。
这很令人困惑,感觉就像是黑客攻击。但是,我想不出更好的方法来做到这一点。
I have two different packages with the same name, each with a set of modules and classes with the same names as each other but they are implemented differently. What is the most logical manner to set up my package/module structure?
Right now I'm doing something like:
Common
utilities.py
VersionA
Package
moduleX.py
moduleY.py
VersionB
Package
moduleX.py
moduleY.py
I am requiring that the environment where the modules are being used, just set the path to point to the correct version of "Package".
On top of it, there is one module that both packages share! "Utilities.py". Now I am also asking the installer to add the Utilties.py to the path.
This is confusing, and feels like a hack. However, I can't figure out a better way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
定义一个全局可访问的环境变量,用于选择版本(VersionA、VersionB)。然后,在 Common/__init__.py 中放置:
现在,每当您想在文件中使用 Package 时,您应该能够执行以下操作:
就可以开始了。
Define a globally accessible env variable which you will use to select the version (VersionA,VersionB). Then, in
Common/__init__.py
put:Now whenever you want to use Package in a file you should be able to do:
and you are good to go.