简体   繁体   English

如何将 Python.pyd 文件转换为 Windows 上的轮子?

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

I am wondering how to convert .pyd file to a Python Wheel ?我想知道如何将.pyd文件转换为Python Wheel

Environment: on Windows, I have compiled a set of C++ files to a .pyd file using MSVC 2019 and CMake .环境:在 Windows 上,我使用MSVC 2019CMake将一组 C++ 文件编译为.pyd文件。 I am using pybind11 to create a Python module.我正在使用pybind11创建一个 Python 模块。

I have tried every technique I can possibly think of over the past few months.在过去的几个月里,我尝试了所有我能想到的技术。

Solved it by using the answer by @hoefling :通过使用@hoefling 的答案解决了这个问题:

  • Created a setup.py file from said answer, filled in the blanks.根据上述答案创建了一个setup.py文件,并填写了空白。
  • 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.最终要杀死的老板在一周后解决了,如果CMakeCache.txt存在于根目录中,那么一切都会失败,并出现一个巨大的 20 行无意义的错误消息。 CMake is a reasonable build system, but it sure has some absolutely horrific bugs - there's no other word for that sort of behavior. CMake是一个合理的构建系统,但它肯定有一些绝对可怕的错误 - 这种行为没有其他词。


Appendix A附录 A

Quote from Building C and C++ Extensions :引自构建 C 和 C++ 扩展

A C extension for CPython is a shared library (egaso file on Linux, .pyd on Windows), which exports an initialization function. CPython 的 C 扩展是一个共享库(Linux 上的 egaso 文件,Windows 上的 .pyd),它导出初始化 function。

To be importable, the shared library must be available on PYTHONPATH, and must be named after the module name, with an appropriate extension.要导入,共享库必须在 PYTHONPATH 上可用,并且必须以模块名称命名,并带有适当的扩展名。 When using distutils, the correct filename is generated automatically.使用 distutils 时,会自动生成正确的文件名。

The initialization function has the signature:初始化 function 具有签名:

 PyObject* PyInit_modulename(void)

It returns either a fully-initialized module, or a PyModuleDef instance.它返回完全初始化的模块或 PyModuleDef 实例。 See Initializing C modules for details.有关详细信息,请参阅初始化 C 模块。

So during development, it is not really that necessary to compile the .pyd file into a wheel for installation.所以在开发过程中,将.pyd文件编译成一个轮子进行安装其实是没有必要的。 As long as the .pyd file is available on PYTHONPATH , and it named correctly, then everything will just work.只要.pyd文件在PYTHONPATH上可用,并且命名正确,那么一切都会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM