For those with problems compiling with Python on Windows

20 views
Skip to first unread message

kgschlosser

unread,
May 14, 2023, 4:23:27 AM5/14/23
to python-cffi
For those that have seen these kinds of errors.

```distutils.errors.DistutilsPlatformError: Microsoft Visual C++ 14.2 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
```

And you have the latest version of Visual Studio installed....

I don't know how many people have gotten bitten by MSVC compiler detection problems in SetupTools and Distutils. It has happened to me and it can be extremely frustrating to figure out. Even more so if your OS of choice is something other than Windows. The end game is to provide cross platform support and the lack of a built in packaging framework that is able to set up a proper build environment under Windows is a real sore spot.

This is where I come to the rescue. I wrote a script that sets up a correct build environment. It actually makes the environment that you see when using Visual Studio but it does it without using the problematic vcvars* batch files (which is what distutils and setup tools uses). Setup tools is not designed to detect Visual Studio installations. It is only made to locate Build Tools installations. There in lies the problem as most CI environments are packaged with Visual Studio and most developers typically would have Visual Studio installed and not Build Tools.

This script is able to locate Visual C 10 all the way up to the most recent release of Visual Studio. It is also able to detect all of the version of Build Tools associated with all of the different versions of Visual C. It is able to locate all applicable Windows SDK's for the C version that is being used. .NET it does it, F# does that too, HTMLHelp as well. No monkey patching of Distutils or Setup tools is done, that means no odd issues can pop up.

The ease of use is what makes it great. import the library and call a single function That's all there is to it. The folks over at Cython have added a link in their docs. It is available on Pypi as well.





I just wanted to share this so folks can avoid any issues.

You can add the library to pyproject.toml so it gets pulled from pypi and is available to use. in the setup.py a simple check of sys.platform to make sure windows is the os then import the library and make the single call to setup the environment.





Reply all
Reply to author
Forward
0 new messages