Compile python extension for different C++ runtime

前端 未结 1 1276
忘了有多久
忘了有多久 2021-01-28 11:14

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 system

相关标签:
1条回答
  • 2021-01-28 11:45

    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 :)

    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.


    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:

    • allocating memory that will be freed by Python
    • freeing memory that was allocated by Python
    • passing/getting file descriptors or FILE* pointers to/from Python
    • setting any global settings like encoding or locale
    • writing to standard streams

    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:

    • 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)

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题