有多个python版本_支持多个Python模块版本(具有相同版本的Python)

本文探讨了如何在同一包中支持不同版本的Python模块,提出了使用不同命名的文件来区分不同版本的方法,并讨论了使用子目录按版本组织模块的优缺点。通过导入特定版本的模块来实现需求。
摘要由CSDN通过智能技术生成

I looked around but cannot find a clear answer to my question.

I have a very legitimate need for supporting N-versions of the same Python module.

If they are stored in the same same package/directory, they would have to be uniquely named, like in the following example:

.../some/package/my_module_1.0.py

.../some/package/my_module_1.1.py

.../some/package/my_module_2.0.py

-- etc. --

And each would, in turn, store its version number via the "version" attribute.

The consuming program then imports the correct version that it needs

(e.g.: import my_module_1.1).

Is this the optimal (and most pythonic) way to accomplish this multi-module version need?

Thank you!

解决方案

I did some googling for "flask support multiple API versions", and found this stackoverflow post to be pretty handy.

In short, the only real difference suggested in that post vs what you've provided is that they use subdirs for each version, and each version is a module itself.

If you're able/allowed to share common functions/objects between versions, this structure might make maintaining/managing your module easier.

some_package

+--- my_module/

+--- v1_0/

+--- __init__.py

+--- some_file.py

+--- v2_0/

+--- __init__.py

+--- some_file.py

+--- __init__.py

+--- common_stuff.py

But if you cannot share things between versions, then your current idea works just fine, and is simpler.

And then you could import it like this:

import my_module.vX_Y as my_module

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值