Compile C Extension of Python on Windows

Compile C Extension of Python on Windows

When compiling a Python C extension on Windows, the build must use a Microsoft C/C++ compiler and Windows SDK that are compatible with the Python version being used. If either the compiler tools or SDK headers are missing, pip install, python setup.py build, or conda build may fail with errors like the following.

Unable to find vcvarsall.bat

This usually means Python’s build tooling cannot find the Visual C++ build environment script.

I installed Visual C++ 2017, and this error was eliminated.

A durable way to fix it is to install the Microsoft C++ build tools that match your Python version. In the Visual Studio installer, make sure the C++ workload is selected, including the MSVC compiler and Windows SDK.

After installation, open a fresh terminal and verify that the compiler is available:

where cl
cl

If cl is still not found, launch a Visual Studio Developer Command Prompt, or run the appropriate vcvarsall.bat script before building.

Cannot open include file: 'io.h'

Another possible error is:

c:\miniconda3\include\pyconfig.h(59): fatal error C1083: Cannot open include file: 'io.h': No such file or directory

The header io.h comes from the Windows SDK / Universal CRT headers. This error usually means the SDK headers are not installed or are not visible to the compiler environment.

Installing the Windows 10 SDK can solve this problem, although I had not tried it at the time.

A practical checklist is:

  1. Open the Visual Studio Installer.
  2. Modify the installed Build Tools or Visual Studio installation.
  3. Enable the C++ build tools workload.
  4. Ensure an installed Windows SDK is selected.
  5. Restart the terminal and rebuild the extension.

Then retry the build:

python -m pip install .

or, for an old-style project:

python setup.py build

If the project uses Conda, also confirm that the active environment is the one you intend to build against:

where python
python -c "import sys; print(sys.executable); print(sys.version)"

The important point is that Python, the MSVC compiler, and the Windows SDK must all be visible in the same shell session.

Leave a Reply