为不同的 C++ 运行时编译 python 扩展 [英] Compile python extension for different C++ runtime

查看:30
本文介绍了为不同的 C++ 运行时编译 python 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 plex 媒体服务器开发一个插件.Plex 使用 Python 2.7 作为插件系统.我需要使用python-levenshtein包,需要针对不同的系统进行编译.

I am developing a plugin for plex media server. Plex uses Python 2.7 for plugin system. I need to use python-levenshtein package, which needs to be compiled for different systems.

我发现了 python-Levenshtein 轮子(许多linux、macos、arm、windows),但 Windows 轮子是用 VS 2008 编译的(msvcr90.dll).以及使用 VS 2013 (msvcr130.dll) 编译的 plex 内嵌 python.

I found python-Levenshtein wheels (manylinux, macos, arm, windows), but the wheel for Windows compiled with VS 2008 (msvcr90.dll). And plex embeded python compiled with VS 2013 (msvcr130.dll).

在 Windows 10 上,我如何编译具有不同 VS 运行时的 python 包?

Being on Windows 10 how can i compile python package with different VS runtimes?

推荐答案

注意: Python 团队不支持为非官方版本的 Python 版本编译扩展,必须支持按产品 - 在这种情况下,Plex.所以假设 Plex 没有提供一种方法来做到这一点,下面的一切都是黑客和解决方法.

Note: compiling extensions for versions of Python other than the official releases is not supported by the Python team, and has to be supported by the product - in this case, Plex. So assuming Plex hasn't provided a way to do this, everything below is hacks and workarounds.

此外,如果库开发人员没有进行构建,那么您正在尝试为他们进行构建,这意味着您基本上处于他们的级别",并且需要像他们一样了解他们的代码.欢迎使用开源软件:)

Also, if the library developer is not doing the builds then you're trying to cover for them, which means you are basically at their "level" and will need to know their code as well as they do. Welcome to open source software :)

最后,这仅适用于旧版 Python 2.如果有人使用 Python 3.5 或更高版本阅读本文,只需安装最新的 Visual Studio 并使用最新的编译器 - 整个 14.x 系列都兼容,并且可以在最新的 Python 版本中正常工作.

Finally, this only applies to legacy Python 2. If anyone reading this is on Python 3.5 or later, just install the latest Visual Studio and use the latest compiler - the entire 14.x line is compatible and will work fine with recent Python versions.

假设您一直关注扩展如何与 C 运行时交互,那么针对错误"的 C 运行时版本编译扩展通常是安全的.这里需要注意的是:

Assuming you've been careful about how the extension interacts with the C Runtime, it's often safe to compile your extension against the "wrong" C runtime version. Things to watch out for here are:

  • 分配将由 Python 释放的内存
  • 释放 Python 分配的内存
  • 向/从 Python 传递/获取文件描述符或 FILE* 指针
  • 设置任何全局设置,如编码或区域设置
  • 写入标准流

如果所有这些都是孤立的,那么您最终只会在内存中拥有两个 C 运行时,并且它们不会尝试进行交互.

If all of these are isolated, then you'll just end up with two C runtimes in memory and they won't try to interact.

现在,如果由于某种原因无法做到这一点,您可以尝试使用更高版本的工具集编译扩展.最简单的方法是:

Now, if for some reason this can't be done, you can try compiling the extension using a later toolset. The easiest way to do this is:

  • 获取源代码(我假设基于 setup.py 通过轮构建)
  • 打开 VS 2013 开发人员命令提示符
  • 运行 set DISTUTILS_USE_SDK=1(这将绕过 MSVC 检测)
  • 运行 set MSSdk=1(也需要绕过旧版 Python 2 的检测)
  • 运行python setup.py bdist_wheel(你可能需要先将wheel安装到Python中)
  • get the source code (I'm assuming setup.py based build via wheel)
  • open VS 2013 Developer Command prompt
  • run set DISTUTILS_USE_SDK=1 (this will bypass MSVC detection)
  • run set MSSdk=1 (also needed to bypass detection on legacy Python 2)
  • run python setup.py bdist_wheel (you may need to install wheel into Python first)

现在您可以按照通常的方式获取wheel文件并安装它,可能是通过提取/复制文件或将完整文件名传递给pip.

Now you can take the wheel file and install it however you normally would, perhaps by extracting/copying the files or passing the full filename to pip.

一般来说,简单地构建扩展程序很可能不会成功.在这种情况下,您必须修改源代码才能工作 - 在 VC 9.0 和 VC 12.0 之间的三个 MSVC 版本中,许多名称已被弃用和删除,因此您不会看到其中任何一个的弃用警告.

In general, there's a very high chance that simply building the extension will not succeed. In that case, you'll have to modify the source code to work - a lot of names were deprecated and removed in the three MSVC versions between VC 9.0 and VC 12.0, and so you won't see deprecation warnings for any of them.

如果库开发人员已经使他们的库与 Python 3 一起工作,则许多修复应该存在但可能无法检测到(因为 Python 3 要么使用了不需要修复的 VC 10.0,要么使用了 VC 14.x,可能会被检测为 _MSC_VER >= 1900 并且不会检测到 VC 12.0).如果您提供任何修复以帮助下一个人,他们可能会很感激,但许多库开发人员现在正在放弃对 Python 3.5 之前版本的支持,因此他们可能对维护旧版支持不感兴趣.

If the library developer has already made their library work with Python 3, many of the fixes should be there but may not be detected (as Python 3 either used VC 10.0, which didn't need the fixes, or VC 14.x, which may be detected as _MSC_VER >= 1900 and won't detect VC 12.0). They may appreciate it if you contribute any fixes you make so that the next person is helped, but many library developers are dropping support for versions of Python prior to 3.5 now and so they may not be interested in maintaining legacy support.

这篇关于为不同的 C++ 运行时编译 python 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆