如何编译整个 Python 库(包括依赖项)以便它可以在 C 中使用?

How to compile a whole Python library (including dependencies) so that it can be used in C?

如何编译整个 Python 库及其依赖项,以便它可以在 C 中使用(无需调用 Python 的运行时)。也就是说,编译后的代码嵌入了 Python 解释器,Python 不需要在系统上安装。

据我了解,当使用 Cython 编译 Python 代码时:

目前还不清楚的问题是:

名为 module 的 python 库的简化示例,其中 __init__.py 是一个空文件:

module/
├── run.py
├── http/
│   ├── __init__.py
│   ├── http_request.py

http_requests.py 包含:

import requests

def get_ip():
    r = requests.get('https://ipinfo.io/ip')
    print(r.text)

run.py 包含以下内容:

from http import http_request

if __name__ == '__main__':
    http_request.get_ip()

如何在不使用 Python 运行时的情况下从 C 调用函数 get_ip(在 运行 应用程序时需要安装 Python)。

上面的例子很简单。实际用例是 collecting/processing 高采样率的 C 机器人数据。虽然 C 非常适合基本数据处理,但也有优秀的 Python 库可以进行更全面的分析。 objective 将在 C 中部分处理的数据上调用 Python 库。这将使我们能够更详细地了解数据(并在 [=52= 中处理它) ]).数据框架太大,我们的团队无法用 C 语言重写。

How to compile a whole Python library along with it's dependencies so that it can be used in C (without invoking Python's runtime).

一般来说这是不可能的。 Python 代码在 Python 解释器上实际上是 运行。

有时,当只使用 Python 的一小部分时(甚至 间接 被你的 Python 代码使用的所有东西使用)你可能会使用 Cython (which is actually a superset of a small subset of Python: a lot of genuine Python features cannot be used from Cython, or uses the Python interpreter). But not every Python code can be cythonized, since Python and C have a very different (and incompatible) semantics (and memory management).

否则(而且最常见),使用您的 Python 东西的 C 代码应该 embed the Python interpreter.

如果您的目标是使一个自给自足的 C 库可用于许多 C 程序(在没有 Python 的系统上),一个更明智和更可靠的方法是 重写您的代码在 C 中。

您还可以考虑开始(在您的 C 库中)一些 Python 进程(类似于服务器,执行您的 Python 操作)并使用 inter-process communication facilities, that would be operating system specific. Of course Python needs to be installed on the system of the application using your library. For example, for Linux, you might fork some Python process in your library, and use pipe(7) or unix(7) sockets to communication from the C library to that process (perhaps using something like JSONRPC)。

您的编辑(仍然不是 MCVE) shows some HTTP interaction done in Python. You could consider doing that in C, with the help of HTTP client libraries in C like libcurl, or (if so needed) of HTTP server libraries like libonion

因此请考虑用 C 重写您的内容,但使用几个 现有 C 库(如何选择以及选择什么是一个非常不同的问题,可能 off-topic on Whosebug)。否则,接受对 Python.

的依赖

The actual use case is collecting/processing robotics data in C at a high sampling rate. Whilst C is great for basic data processing there are excellent Python libraries which allow for much more comprehensive analysis.

你可以在Python(见this) but recode low level things in C to accelerate them (many software are doing that, e.g. TensorFlow, ...), perhaps as extensions in C for Python或其他一些进程中保留高级的东西。当然,这意味着一些开发工作。我不认为避免Python 完全是合理的(完全摆脱 Python 是不实用的),如果你在 Python 中使用大量代码。顺便说一句,你可能会考虑在你的 C 应用程序中嵌入一些其他语言(例如Lua、Guile、Ocaml - 据传它们都比 Python) 快,并在更高级别上保持 Python,在其他过程中保持 运行ning。

你需要在你的东西的架构设计上多下功夫。我不确定完全避免使用 Python 是否明智。混合 Python 和 C(也许通过 多个 进程协作)可能更明智。当然你会有操作系统特定的东西(特别是在 C 端,用于进程间通信)。如果在 Linux,请阅读一些有关 Linux C 系统编程的内容,例如ALP 或更新的内容。