How do I find out which CPython version I am using?
Asked Answered
Q

6

6

I am trying to install OpenCV from Unofficial Windows Binaries for Python Extension Packages.

I downloaded the following file : opencv_python‑3.4.3‑cp37‑cp37m‑win_amd64.whl, and when I did pip install "opencv_python‑3.4.3‑cp37‑cp37m‑win_amd64.whl", an error message popped.

The error : opencv_python-3.4.3+contrib-cp37-cp37m-win_amd64.whl is not a supported wheel on this platform.

From what I understood after some googling and SO-ing, this is an issue due to a mismatch between the CPython builds - between the downloaded wheel file and the Python environment on my system.

Therefore, I tried finding ways to determine which CPython version is on my system, but failed.

What I tried so far:

import platform
platform.python.implementation()

Which gave:

'CPython' 

Further, I tried, platform.architecture() which gave:

('64bit', 'WindowsPE')

I later just scoured through my site-packages folder and found somefiles such as __init__.cpython-36.pyc, hence assuming that I am using CPython 3.6.

Is there a more programming based method to check the same through the terminal?

Any kind of help is appreciated. TIA.

Quite answered 21/10, 2018 at 11:24 Comment(8)
have you try python -V or running python in command prompt?Aery
@cryptonome : No great help there - On doing what you said I got a not-very-concise result.Quite
platform.python_version()Aceydeucy
If you have Python 3.7 installed, run pip -V and check what python version is printed. On your machine, pip may be an alias to pip3.6 and not pip3.7; if that's true, you need to either adjust your %PATH% so the correct binaries come first. Or use py -3.7 -m pip install to explicitly select the correct one.Briefing
@RolfofSaxony Your trick worked. Thank you. Please put it as an answer so that I can accept it.Quite
@Briefing Thank you very much for the more technical outlook.Quite
"a not-very-concise result" which is? i never run python in windows, but i think it safe to say IDLE on windows would also still have the version printed. hell, even if IDLE doesn't have it printed you can just import sys; sys.versionAery
@cryptonome The result is too large to put it in a comment. I got the help I needed, thank you for trying.Quite
A
5

The platform module will provide the python version using:

>>> import platform
>>> platform.python_version()
'3.6.6'

Although that said simply running python from the command line should provide a header which gives you this information as well.

$ python
Python 3.6.6 (default, Sep 12 2018, 18:26:19) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.

The sys module can weigh in here as well, as it has both version and api_version routines.

>>> sys.version
'3.8.10 (default, Mar 15 2022, 12:22:08) \n[GCC 9.4.0]'
>>> sys.api_version
1013
>>> sys.version_info
sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)
>>> 

sys.version A string containing the version number of the Python interpreter plus additional information on the build number and compiler used. This string is displayed when the interactive interpreter is started. Do not extract version information out of it, rather, use version_info and the functions provided by the platform module.

sys.api_version The C API version for this interpreter. Programmers may find this useful when debugging version conflicts between Python and extension modules.

Aceydeucy answered 21/10, 2018 at 11:48 Comment(3)
This does not show the cpython version.Aphrodisia
The other answer by quantotto unfortunately does not work, but would give the correct output (ie. cp37m vs. cp37)Aphrodisia
@DreamFlasher What it does not show is the ABI (Application Binary Interface) reference that you appear to want, which will be in the name of the python wheel you installed. What it does show is the python version.Aceydeucy
U
2

It might also be related to specific ABI tag of your python. Maybe yours is not cp37m, where 'm' stands for compilation with pymalloc.

To get your ABI tag, you can pip install wheel and then get it programmatically:

>>> from wheel.pep425tags import get_abi_tag
>>> get_abi_tag()
'cp37m'
Utrecht answered 5/3, 2020 at 10:50 Comment(1)
Fails with ModuleNotFoundError: No module named 'wheel.pep425tags'Aphrodisia
H
1

Seems that pep425tags.py was replaced with vendored/packaging/tags.py in wheel 0.37, yet no alternative functions of get_abi_tag() was provided.

Simply add old pep425tags.py to root directory of wheel 0.37, and the answer by quantotto works for me.

You can see the replacement details in pull request #346.

Hsining answered 30/9, 2022 at 15:41 Comment(0)
R
1

I needed to get this information in a script to determine which wheel to install from a local filesystem and ended up going with this (it does require having wheel installed):

python -c 'from wheel.vendored.packaging import tags; print(f"{tags.interpreter_name()}{tags.interpreter_version()}", end="")'

Which outputs: cp38 on my system.

Relict answered 12/10, 2023 at 22:1 Comment(1)
pip install wheel before for this to work.Calicle
C
0

To print the version and the list of compatible tags:

$ path/to/pythonX.Y -m pip debug --verbose
pip version: pip 23.3.1 from /<user home>/env/lib/python3.8/site-packages/pip (python 3.8)
sys.version: 3.8.15 (default, Nov 24 2022, 04:20:39) 
[GCC 12.2.0]
...
Compatible tags: 429
  cp38-cp38-manylinux_2_36_armv7l
  cp38-cp38-manylinux_2_35_armv7l
  ...
  py30-none-any

Thanks to @sinoroc for the answer.

Calicle answered 9/11, 2023 at 5:44 Comment(0)
P
0

Display supported CPython series:

python3 -m pip install pyversion-info
python3 -c 'from pyversion_info import VersionDatabase; vd = VersionDatabase.fetch(); print(vd.cpython.supported_series())'

This gives for example the following output:

['3.8', '3.9', '3.10', '3.11', '3.12']

Note that this uses pyversion-info which requires Python 3.7 or higher. See pyversion-info for more info.

Punchinello answered 28/8, 2024 at 15:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.