5

My code requires Python version of 3.6 or higher. To ensure this, I use the following:

import sys
version = sys.version_info
assert version.major > 3 or (version.major == 3 and version.minor >= 6)

But this doesn't seem like the best way to do this (from a good coding practices viewpoint). Is there a better way?

What is the appropriate way to make sure your script is being run on an appropriate version of Python?

XYZT
  • 605
  • 5
  • 17
  • Such assert do not help: it give no information to normal users. And remember that python will check syntax, so it will raise other exeptions if code is not compatible with multiple major versions. – Giacomo Catenazzi Mar 08 '18 at 19:14
  • That's fair. So, it's better to just let the code fail on the actual incompatibility issue rather than pre-emptively make it fail? – XYZT Mar 08 '18 at 19:46
  • python scripts are not directly useable, so they need an installer. I would check there. Or just doing a loader. Or like you did, but with a real message, and than just an import and call to function in such module (so no real code in the file that test the version). – Giacomo Catenazzi Mar 08 '18 at 19:55

1 Answers1

4

This information should be specified in your packaging information, so users are informed of the python version requirement before they ever install your python package.

Your project should have a setup.py script that is used to install your package. You can specify the python dependency in that setup script

setup(
    name='my_package',
    version='1.0.0',
    python_requires='>=3.6.0'
    ...
)

This way, it won't allow anyone to build, download, or install your package to an incompatible python version.

Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • Thanks! What if this was for a script that's not part of a package to be installed but simply a simple "*.py" script that's being executed? (Of course, not the ideal way to deploy code but regardless) – XYZT Mar 08 '18 at 19:45
  • @NikhilMahajan You can check like this `sys.version_info >= (3, 6)`. then print some message to indicate that the python version is too old, and the do `sys.exit()` – Brendan Abel Mar 08 '18 at 21:12
  • There are use cases when you want to check the version number dynamically and raise an error. For example, institutions/companies can have different jupyter kernels running, with different package versions. In that case, depending on the kernel (or the packages it's using) you can get nasty and subtle errors that you want to avoid and instead just flag immediately. You might find this useful: https://stackoverflow.com/questions/11887762/how-do-i-compare-version-numbers-in-python – Marses Jun 08 '21 at 13:40
  • @Marses Even in those cases, there's no reason to wait until runtime to catch those conflicts and then force the user to manually solve the version conflicts when `pip` can do all of that for you if you just specified those conflicts ahead of time. – Brendan Abel Jun 09 '21 at 02:42
  • How would `pip` do this? People don't use `pip` in the circumstances I'm talking about. – Marses Jun 09 '21 at 08:15
  • @Marses How are you installing packages and dependencies without something like `pip`? And why aren't you? – Brendan Abel Jun 09 '21 at 18:04
  • 1
    We write the packages modules and scripts. Of course, when it's something more permanent you use version control and host it on a repo, but often for timely or quick things you just write a script/notebook on a shared drive or something. – Marses Jun 14 '21 at 09:04