0

Is it possible to mark or label (not just with comments) source code with a python version to indicate which version of python it is compatible with and raise an error or warning when it is run with an older or incompatible python interpreter?

Edit:

if sys.version_info < (3, 8): 
    raise Exception("You need Python 3.8+")

This should do what I want.

1
  • you can use sys.version_info to recognize used Python and raise error. And usually it is used at the beginning of code. ie. sys.version_info.major >= 3 and sys.version_info.minor > 5 Commented Jul 15, 2020 at 1:06

2 Answers 2

1

You can use sys.version_info to recognize what version is used to run code and raise error.

Usually it is used at script beginning.

import sys

if sys.version_info.major < 3 or sys.version_info.minor < 8:
    raise Exception("You need Python 3.8+")

It can be used also to import different modules in different versions.

Sign up to request clarification or add additional context in comments.

1 Comment

I can see how your answer will work but will it break in the future with Python 4.0. I think I have found a similar check but will not break in 4.X versions of Python. if sys.version_info < (3, 8): raise Exception("You need Python 3.8+")
0
if sys.version_info < (3, 8): 
    raise Exception("You need Python 3.8+")

This should do the job!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.