0

Is there a mechanism in python3 that allows modules to run with different minor versions backward compatibility? I am imagining something like:

with compatibility('python-3.7'):
   import moduleX
with compatibility('python-3.6'):
   import moduleY

I don't seem to able to find anything that allows backwards compatibility except this rejected pep497. If it is not possible, why is it hard? If I have say multiple versions of python3 in different conda/virtual envs should it be possible?

Sorry if it is obvious, but I couldn't think of why/why not this is not possible in the same major version.

Edit: I am not asking to check the python version of the script or module. Let's say I already know which version is required, then how can I run in compatibility mode?

I will give an example, checking Linux versions prior to 3.7 was done using platform module, which is now removed and the suggested alternative is to use a 3rd party module such as distro. Is there a way to call a specific legacy module using a previous version of python? It can be done for a for a simple case like this but lets say your code depends on multiple different python versions. Is there a simple way to allow that?

  • 1
    Does this answer your question? [How do I check what version of Python is running my script?](https://stackoverflow.com/questions/1093322/how-do-i-check-what-version-of-python-is-running-my-script) – mkrieger1 May 27 '22 at 15:33
  • That's not my question though. I want to run different modules with different Python3 minor versions using the same script. This differs from checking which Python3 version I am using. – Naren Nallapareddy May 27 '22 at 15:53
  • So you mean that you can't use something like `if sys.version_info[:2] == (3, 7): import moduleX; else: import moduleY`? – mkrieger1 May 27 '22 at 15:55
  • I want to use code that is no longer in active development and only requires specific version of python. This would mean I have to downgrade my python version/create a new environment. – Naren Nallapareddy May 27 '22 at 16:02
  • Okay. Yes, it would mean that. – mkrieger1 May 27 '22 at 16:05
  • Yes kind of, :) Similar to what pep 497 suggests like a from **__past__** module or similar functionality. – Naren Nallapareddy May 27 '22 at 16:07
  • In most cases, I assume upgrading the code is *simpler* (even if more *time-consuming*) than trying to make the interpreter support old syntax forever. What exactly does your code need that isn't available in recent versions of Python. (There are only two things in `__future__` that were introduced after Python 2 that I think would cause a problem, and one of them *still* isn't mandatory.) – chepner May 27 '22 at 16:21
  • Of course, I am not contesting whether code can be upgraded easily to newer version in some cases. I am asking if there is a mechanism to do it in a more user friendly way, if not what could be potential drawbacks other than increased resource utilization? – Naren Nallapareddy May 27 '22 at 16:33

1 Answers1

1

In order to run your code with a specific version of Python you have to install a Python interpreter with that version and use it to execute the code.

If the same code should be used with different Python versions and some parts need to be changed according to the version, you can use sys.version_info to determine which version is currently being used and branch accordingly, e.g. using if statements or dictionaries.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65