Supporting multiple Python module versions (with the same version of Python)

天涯浪子 提交于 2020-06-08 07:42:08

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/36001016/supporting-multiple-python-module-versions-with-the-same-version-of-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!