支持多个Python模块版本(具有相同的Python版本) [英] Supporting multiple Python module versions (with the same version of Python)

查看:191
本文介绍了支持多个Python模块版本(具有相同的Python版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,但找不到清晰的答案.

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

我非常需要支持同一Python模块的N版本.

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.

使用方程序随后导入所需的正确版本 (例如:import my_module_1.1).

The consuming program then imports the correct version that it needs (e.g.: import my_module_1.1).

这是满足此多模块版本需求的最佳方法(也是最Python的方法)吗?

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

谢谢!

推荐答案

我对烧瓶支持多个API版本"进行了谷歌搜索,发现

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

这篇关于支持多个Python模块版本(具有相同的Python版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆