Neil Yang
Neil Yang

Reputation: 31

How to determine the python is mingw or official build?

I am using python in the MSYS2 environment. The MSYS2 has its own built MINGW python version. Also I can install the official python from the www.python.org.

Here is the problem: If I want to write a python code need to know the python version is MINGW or the official one, how can I do it?

Here are some ways I can image.

  1. Use the "sys.prefix" object. It tells the installation directory. the MSYS2 usually installed in the directory X:\msys2\.... and the official one install in the X:\Python27\ as default. But users may change the installation directory. So this is not a good way.
  2. Use the "sys.version" object can get the version strings show with the compiler name. It shows the MINGW python compiled by GCC, the official one compiled by MSC. But there may have some possibility that there is an other version's python also built by GCC or MSC.

Is there any more elegant way can do this?

Upvotes: 3

Views: 2962

Answers (3)

Dan Yeaw
Dan Yeaw

Reputation: 810

Another option is to check if GCC is in sys.version.

For example:

import sys
MSYS2 = ("GCC" in sys.version)
print(MSYS2)
> True

This works because the sys.version information has the type of compiler used, and MinGW uses GCC.

python -c "import sys; print(sys.version)"
3.7.4 (default, Aug 15 2019, 18:17:27)  [GCC 9.2.0 64 bit (AMD64)]

Upvotes: 1

Vasily Galkin
Vasily Galkin

Reputation: 322

Mingw python3 build patched official sysconfig.get_platform() function so it returns "mingw" and can be used for distinguishing from official python builds.

https://github.com/Alexpux/MINGW-packages/blob/abd06ca92d876b9db05dd65f27d71c4ebe2673a9/mingw-w64-python2/0410-MINGW-build-extensions-with-GCC.patch#L54

Upvotes: 6

Uwe Koloska
Uwe Koloska

Reputation: 478

sys.platform gives msys when in msys-Python.

Upvotes: -3

Related Questions