将 Python 代码编译成只调用所需 DLL 的 exe 文件

Compiling Python code into an exe file calling just the required DLLs

关于这个主题的信息似乎很少 - 我已经尝试 py2exe 无济于事(错误 Python 3.5),以及 pyinstaller.后者似乎包括系统上安装的每个扩展模块,除非它们在构建时被特别排除,即使是小型构建,最终也会达到 50+。我还看到了 pyinstcxFreeze,您可以在其中指定要包含的包,并将 Python 与它捆绑在一起,但我想要简单和小的。我所做的所有工作最多涉及 NumPySciPyPandas。通常,这些都可以减少到只有 NumPy 的函数,以及我在 Cython 中重写的最复杂的函数(使用 memoryviews)。环顾互联网,我发现 python.exe 是一个 C++ 程序,它只调用 Python.h 并加载 python35.dll - 这表明一些像这个 C++ 代码示例一样简单的东西将允许一个人访问所有Python 和 NumPy 功能如下:

#include "Python.h"
#include <\numpy\core\include\numpy\ required includes>

int
wmain(int argc, wchar_t **argv)
{
    wchar_t *myargs[3] = { argv[0], L"-m", L"myscript.py" };
    return Py_Main(3, myargs);
}

示例来自: https://blogs.msdn.microsoft.com/pythonengineering/2016/04/26/cpython-embeddable-zip-file/

有没有人有好的方法或好的参考来生成一个小的构建,其中只包含你需要的 exe 到 运行?或者可以确认以上内容实际上可以修改吗?非常感谢,我认为这个技能是一个小众学科......

如果能带依赖,可以用boost::python帮忙

下面有一个完整的示例应用程序,它只链接 boost_pythonpython2.7 共享库。 -O3 构建只有 51K。

导入一个python模块:

您使用 boost::python::exec 执行导入语句,将您的 python 模块导入 boost::python::object

bp::object import(const std::string& module, const std::string& path)
{
    bp::object globals = bp::import("__main__").attr("__dict__");

    bp::dict locals;
    locals["module_name"] = module;
    locals["path"]        = path;

    // execute some python code which imports the file
    bp::exec("import imp\n"
             "new_module = imp.load_module(module_name, open(path), path, ('bp', 'U', imp.PY_SOURCE))\n",
             globals,
             locals);
    return locals["new_module"];
}

// get an object containing the contents of the file "test.py"
bp::object module = import("test", "test.py");

获取 python 模块中元素的句柄:

// get a handle to something declared inside "test.py"
bp::object Script = module.attr("Script");

实例化一个对象:

// Instantiate a script object
bp::object script = Script();

调用Script的成员函数:

// calls Script.run(), passing no arguments
script.attr("run")();

您还可以将 C++ 代码公开给 python:

使用 boost::python::class:

将 C++ class 暴露给您刚刚导入的模块
struct Foo
{
    void func();
}

bp::object FooWrapper(
    bp::class_<Foo>("Foo")
        .def("func", &Foo::func)
    );

bp::object foo    = FooWrapper(); // instantiate a python wrapped Foo object
bp::object script = Script(foo);  // create a Script instance, passing foo 

工作示例:

test.py

class Script(object):
    def __init__(self, ifc):
        print 'created script'
        self.ifc = ifc

    def run(self):
        print 'running'
        self.ifc.execute(5)

    def result(self, i):
        print 'result={}'.format(i)

main.cpp

#include <boost/python.hpp>

namespace bp = boost::python;

bp::object import(const std::string& module, const std::string& path)
{
    bp::object globals = bp::import("__main__").attr("__dict__");

    bp::dict locals;
    locals["module_name"] = module;
    locals["path"]        = path;

    bp::exec("import imp\n"
             "new_module = imp.load_module(module_name, open(path), path, ('bp', 'U', imp.PY_SOURCE))\n",
             globals,
             locals);
    return locals["new_module"];
}

///////////////////////////

class Runner
{
public:
    void init(bp::object script)
    {
        // capture methods at creation time so we don't have to look them up every time we call them
        _run    = script.attr("run");
        _result = script.attr("result");
    }

    void run()
    {
        _run(); // call the script's run method
    }

    void execute(int i) // this function is called by the python script
    {
        _result(i * 2); // call the script's result method
    }

    bp::object _run;
    bp::object _result;
};

int main()
{
    Py_Initialize();

    // load our python script and extract the Script class
    bp::object module = import("test", "test.py");
    bp::object Script = module.attr("Script");

    // wrap Runner and expose some functions to python
    bp::object RunnerWrapper(
        bp::class_<Runner>("Runner")
            .def("execute", &Runner::execute)
        );

    // create a python wrapped instance of Runner, which we will pass to the script so it can call back through it
    bp::object wrapper = RunnerWrapper();
    bp::object script  = Script(wrapper);

    // extract the runner instance from the python wrapped instance
    Runner& runner = bp::extract<Runner&>(wrapper);

    // initialise with the script, so we can get handles to the script's methods we require
    runner.init(script);

    runner.run();

    Py_Finalize();
    return 0;

CMakeLists.txt

cmake_minimum_required (VERSION 3.2.2)

find_package(Boost COMPONENTS python REQUIRED)
find_package(PythonLibs 2.7 REQUIRED)

add_executable            (py_embed main.cpp)
target_link_libraries     (py_embed ${Boost_PYTHON_LIBRARY} ${PYTHON_LIBRARIES})
target_include_directories(py_embed SYSTEM PRIVATE ${PYTHON_INCLUDE_DIRS})

可下载的源代码here