如何避免使用我的 python 包构建 C 库? [英] How to avoid building C library with my python package?

查看:16
本文介绍了如何避免使用我的 python 包构建 C 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 ctypes 的 C 库构建一个 python 包.我想让我的包便携(Windows、Mac 和 Linux).

I'm building a python package using a C library with ctypes. I want to make my package portable (Windows, Mac and Linux).

我找到了一种策略,在安装包的过程中使用 build_extpip 来构建库.它根据目标平台创建 libfoo.dlllibfoo.dyliblibfoo.so.

I found a strategy, using build_ext with pip to build the library during the installation of my package. It creates libfoo.dll or libfoo.dylib or libfoo.so depending on the target's platform.

这个问题是我的用户需要安装 CMake.

The problem with this is that my user needs CMake installed.

是否存在另一种避免在安装过程中构建的策略?我是否必须在我的包中捆绑构建的库?

Does exist another strategy to avoid building during the installation? Do I have to bundle built libraries in my package?

我想让我的用户使用 pip install mylib.

I want to keep my users doing pip install mylib.

编辑:感谢@Dawid 评论,我正在尝试使用命令 python setup.py bdist_wheel 制作 python wheel任何成功.

Edit: thank to @Dawid comment, I'm trying to make a python wheel with the command python setup.py bdist_wheel without any success.

如何使用嵌入式库为不同平台创建我的 python 轮子?

编辑 2:我使用 python 3.4 并在 Mac OS X 上工作,但我可以访问 Windows 计算机和 Linux 计算机

Edit 2: I'm using python 3.4 and working on Mac OS X, but I have access to Windows computer, and Linux computer

推荐答案

根据我的研究,您肯定走在正确的道路上...正如 Daniel 所说,您唯一的选择是自己构建和分发二进制文件.

You're certainly heading down the right path according to my research... As Daniel says, the only option you have is to build and distribute the binaries yourself.

一般来说,打包用户指南.我不会在那里重复建议,因为您已经清楚地找到了它.然而,其中的关键点是 Python 社区,特别是 PyPA 正试图标准化使用 平台轮 来打包二进制扩展.遗憾的是,此时存在一些问题:

In general, the recommended way to install packages is covered well in the packaging user guide. I won't repeat advice there as you have clearly already found it. However the key point in there is that the Python community, specifically PyPA are trying to standardize on using platform wheels to package binary extensions. Sadly, there are a few issues at this point:

  1. 您无法为所有 Linux 变体创建发行版,但您可以为兼容的子集构建轮子.请参阅 https://www.python.org/dev/peps/pep-0513/ 了解详情.
  2. 关于构建扩展的建议有些不完整,反映出缺乏二进制分发的完整解决方案.
  3. 然后人们尝试构建自己的库并将其作为数据文件分发,这让 setuptools 感到困惑.
  1. You cannot create distributions for all Linux variants, but you can build wheels for a compatible subset. See https://www.python.org/dev/peps/pep-0513/ for details.
  2. The advice on building extensions is somewhat incomplete, reflecting the lack of a complete solution for binary distributions.
  3. People then try to build their own library and distribute it as a data file, which confuses setuptools.

我认为您遇到了最后一个问题.解决方法是强制发行版构建平台通过覆盖 is_pure() 来始终返回 False.但是,您可以保留原始构建说明,而 bdist_wheel 应该处理它.

I think you are hitting this last issue. A workaround is to force the Distribution to build a platform wheel by overriding is_pure() to always return False. However you could just keep your original build instructions and bdist_wheel should handle it.

但是,一旦您构建了轮子,您仍然需要分发它以及它使用或使用的其他二进制包.此时,您可能需要使用推荐工具之一,例如conda 或像 devpi 这样的 PyPI 代理来为您服务.

Once you've built the wheel, though, you still need to distribute it and maybe other binary packages that it uses or use it. At this point, you probably need to use one of the recommended tools like conda or a PyPI proxy like devpi to serve up your wheels.

回答有关交叉编译的额外问题

正如此处所涵盖的那样 Python 2.6和更高版本允许对 Windows 32/64 位构建进行交叉编译.其他平台上没有对其他软件包的正式支持,人们尝试取得有限的成功它.您最好在每个 Linux/Mac/Windows 环境中进行本机构建.

As covered here Python 2.6 and later allows cross-compilation for Windows 32/64-bit builds. There is no formal support for other packages on other platforms and people have had limited success trying to do it. You are really best off building natively on each of your Linux/Mac/Windows environments.

这篇关于如何避免使用我的 python 包构建 C 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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