简体   繁体   English

如何在没有本地 Python 安装的情况下创建嵌入并运行 Python 代码的应用程序?

[英]How to create an application which embeds and runs Python code without local Python installation?

Hello fellow software developers.各位软件开发人员大家好。

I want to distribute a C program which is scriptable by embedding the Python interpreter.我想分发一个 C 程序,它可以通过嵌入 Python 解释器编写脚本。
The C program uses Py_Initialize, PyImport_Import and so on to accomplish Python embedding. C程序使用Py_Initialize、PyImport_Import等实现Python嵌入。

I'm looking for a solution where I distribute only the following components:我正在寻找一种只分发以下组件的解决方案:

  • my program executable and its libraries我的程序可执行文件及其库
  • the Python library (dll/so) Python 库 (dll/so)
  • a ZIP-file containing all necessary Python modules and libraries.包含所有必需的 Python 模块和库的 ZIP 文件。

How can I accomplish this?我怎样才能做到这一点? Is there a step-by-step recipe for that?有没有一个循序渐进的方法?

The solution should be suitable for both Windows and Linux.该解决方案应该适用于 Windows 和 Linux。

Thanks in advance.提前致谢。

Have you looked at Python's official documentation : Embedding Python into another application ? 你看过Python的官方文档:将Python嵌入到另一个应用程序中吗?

There's also this really nice PDF by IBM : Embed Python scripting in C application . IBM还有这个非常好的PDF: 在C应用程序中嵌入Python脚本

You should be able to do what you want using those two resources. 您应该能够使用这两种资源做您想做的事情。

I simply tested my executable on a computer which hasn't Python installed and it worked. 我只是在没有安装Python的计算机上测试我的可执行文件并且它工作正常。

When you link Python to your executable (no matter if dynamically or statically) your executable already gains basic Python language functionality (operators, methods, basic structures like string, list, tuple, dict, etc.) WITHOUT any other dependancy. 当您将Python链接到可执行文件(无论是动态还是静态)时,您的可执行文件已经获得了基本的Python语言功能(运算符,方法,基本结构,如字符串,列表,元组,字典等),没有任何其他依赖性。

Then I let Python's setup.py compile a Python source distribution via python setup.py sdist --format=zip which gave me a ZIP file I named pylib-2.6.4.zip . 然后我让Python的setup.py通过python setup.py sdist --format=zip编译一个Python源代码,这给了我一个名为pylib-2.6.4.zip的ZIP文件。

My further steps were: 我的进一步措施是:

char pycmd[1000]; // temporary buffer for forged Python script lines
...
Py_NoSiteFlag=1;
Py_SetProgramName(argv[0]);
Py_SetPythonHome(directoryWhereMyOwnPythonScriptsReside);
Py_InitializeEx(0);

// forge Python command to set the lookup path
// add the zipped Python distribution library to the search path as well
snprintf(
    pycmd,
    sizeof(pycmd),
    "import sys; sys.path = ['%s/pylib-2.6.4.zip','%s']",
    applicationDirectory,
    directoryWhereMyOwnPythonScriptsReside
);

// ... and execute
PyRun_SimpleString(pycmd);

// now all succeeding Python import calls should be able to
// find the other modules, especially those in the zipped library

...

Did you take a look at Portable Python ? 你看过Portable Python了吗? No need to install anything. 无需安装任何东西。 Just copy the included files to use the interpreter. 只需复制包含的文件即可使用解释器。

Edit : This is a Windows only solution. 编辑:这是一个仅限Windows的解决方案。

I think here is the answer you want Unable to get python embedded to work with zip'd library 我想这里是你想要的答案无法将python嵌入到zip'd库中

Basically, you need: 基本上,你需要:

Py_NoSiteFlag=1;
Py_SetProgramName(argv[0]);
Py_SetPythonHome(".");
Py_InitializeEx(0);
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path = ['.','python27.zip','python27.zip/DLLs','python27.zip/Lib','python27.zip/site-packages']");

in your c/c++ code for loading the python standard library. 在用于加载python标准库的c / c ++代码中。

And in your python27.zip , all .py source code are located at python27.zip/Lib as described in the sys.path variable. python27.zip ,所有.py源代码都位于python27.zip/Lib ,如sys.path变量中所述。

Hope this helps. 希望这可以帮助。

Have you looked at Embedding Python in Another Application in the Python documentation? 你有没有看过在Python文档中将Python 嵌入到另一个应用程序中?

Once you have that, you can use an import hook (see PEP 302 ) to have your embedded Python code load modules from whatever place you choose. 完成后,您可以使用导入钩子(请参阅PEP 302 )让您的嵌入式Python代码从您选择的任何位置加载模块。 If you have everything in one zipfile, though, you probably just need to make it the only entry on sys.path . 但是,如果你有一个zipfile中的所有内容,你可能只需要将它作为sys.path上的唯一条目。

There's a program called py2exe. 有一个名为py2exe的程序。 I don't know if it's only available for Windows. 我不知道它是否仅适用于Windows。 Also, the latest version that I used does not wrap everything up into one .exe file. 此外,我使用的最新版本不会将所有内容包装到一个.exe文件中。 It creates a bunch of stuff that has to be distributed - a zip file, etc.. 它创建了一堆必须分发的东西 - 一个zip文件等。

You can compile to one.exe file using pyinstaller您可以使用 pyinstaller 编译为 one.exe 文件

pip install pyinstaller

pyinstaller --onefile urPythonScriptName.py

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

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