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

2024/10/1 9:50:13

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!

Answer

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://en.xdnf.cn/q/70977.html

Related Q&A

ImportError: cannot import name signals

Im using Django 1.3.0 with Python 2.7.1. In every test I write the following imports I get the importError above:from django.utils import unittest from django.test.client import ClientThe full stack tr…

Return a Pandas DataFrame as a data_table from a callback with Plotly Dash for Python

I would like to read a .csv file and return a groupby function as a callback to be displayed as a simple data table with "dash_table" library. @Lawliets helpful answer shows how to do that wi…

Nose: How to skip tests by default?

I am using Pythons nose and I have marked some of my tests as "slow", as explained in the attrib plugin documentation.I would like to skip all "slow" Tests by default when running n…

SQLAlchemy ORM select multiple entities from subquery

I need to query multiple entities, something like session.query(Entity1, Entity2), only from a subquery rather than directly from the tables. The docs have something about selecting one entity from a s…

How to ensure data is received between commands

Im using Paramiko to issue a number of commands and collect results for further analysis. Every once in a while the results from the first command are note fully returned in time and end up in the out…

Format Excel Column header for better visibility and Color

I have gone through many posts but did not found the exact way to do the below. Sorry for attaching screenshot(Just for better visibility) as well , I will write it also. Basically it looks like -Name…

Using multiple keywords in xattr via _kMDItemUserTags or kMDItemOMUserTags

While reorganizing my images, in anticipation of OSX Mavericks I am writing a script to insert tags into the xattr fields of my image files, so I can search them with Spotlight. (I am also editing the …

JAX Apply function only on slice of array under jit

I am using JAX, and I want to perform an operation like @jax.jit def fun(x, index):x[:index] = other_fun(x[:index])return xThis cannot be performed under jit. Is there a way of doing this with jax.ops …

Using my own corpus for category classification in Python NLTK

Im a NTLK/Python beginner and managed to load my own corpus using CategorizedPlaintextCorpusReader but how do I actually train and use the data for classification of text?>>> from nltk.corpus…

Python ImportError for strptime in spyder for windows 7

I cant for the life of me figure out what is causing this very odd error.I am running a script in python 2.7 in the spyder IDE for windows 7. It uses datetime.datetime.strptime at one point. I can run …