Contango
Contango

Reputation: 80372

How to convert a Python .pyd file to a wheel on Windows?

I am wondering how to convert .pyd file to a Python Wheel?

Environment: on Windows, I have compiled a set of C++ files to a .pyd file using MSVC 2019 and CMake. I am using pybind11 to create a Python module.

I have tried every technique I can possibly think of over the past few months.

Upvotes: 1

Views: 3149

Answers (1)

Contango
Contango

Reputation: 80372

Solved it by using the answer by @hoefling:

  • Created a setup.py file from said answer, filled in the blanks.
  • Then used this batch file:
@echo off
rem Build and install module. Run in a Visual Studio x64 prompt.
rem Out of the box, Python does not support building of wheels.
pip install wheel 
rem Build wheel.
python setup.py bdist_wheel
rem Install wheel.
pip install --upgrade --force-reinstall dist/MyProject-0.0.1-cp37-cp37m-win_amd64.whl
pause

The final boss to slay was working out, after a week, that if CMakeCache.txt is present in the root directory, everything will fail with a huge 20-line nonsensical error message. CMake is a reasonable build system, but it sure has some absolutely horrific bugs - there's no other word for that sort of behavior.


Appendix A

Quote from Building C and C++ Extensions:

A C extension for CPython is a shared library (e.g. a .so file on Linux, .pyd on Windows), which exports an initialization function.

To be importable, the shared library must be available on PYTHONPATH, and must be named after the module name, with an appropriate extension. When using distutils, the correct filename is generated automatically.

The initialization function has the signature:

PyObject* PyInit_modulename(void)

It returns either a fully-initialized module, or a PyModuleDef instance. See Initializing C modules for details.

So during development, it is not really that necessary to compile the .pyd file into a wheel for installation. As long as the .pyd file is available onPYTHONPATH, and it named correctly, then everything will just work.

Upvotes: 1

Related Questions