python 经历了两天的折磨,总算尝试出来了,整体代码打包成exe(包含引用第三方pyd,dll库)

前言:经过两天的痛苦折磨,总算找到原因了,之前又是安装vscode2014,又是切换python版本的,都没用。

a:pip install pyinstaller (一定要安装最新版)

b: 本地写一个简单的测试demo,检测打包软件是否OK

c:切记千万别直接 win+R 进入到打包目录,一定要使用Anaconda环境

1:打开

2:输入命令 pyinstaller -D chatinfo.py(为程序入口)

3:将引用的包路径全部复制进去

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我将为你提供一个比较简单的实现方式,仅供参考。在这个例子中,我们将使用Python自带的ctypes模块来将Python代码打包DLL文件。 假设我们要将以下Python代码打包DLL文件: ```python # test.py import numpy as np def add(a, b): return np.add(a, b) ``` 那么我们可以按照以下步骤进行操作: 1. 安装Python的开发包,以便在Windows系统下编译Python扩展模块。可以从Python官网下载对应版本的开发包。 2. 安装Microsoft Visual C++ Build Tools,以便在Windows系统下编译C/C++代码。 3. 在命令行中进入Python的安装目录下的Scripts文件夹,执行以下命令,安装setuptools和wheel: ``` pip install setuptools wheel ``` 4. 在命令行中进入Python代码所在的目录,执行以下命令,打包Python代码为扩展模块: ``` python setup.py bdist_wheel ``` 其中,setup.py文件的内容如下: ```python from setuptools import setup, Extension import numpy as np ext_module = Extension( '_test', sources=['test.c', 'test_wrap.c'], include_dirs=[np.get_include()], libraries=['python36'], library_dirs=[r'C:\Python36\libs'], extra_compile_args=['/openmp'] ) setup( name='test', version='1.0', ext_modules=[ext_module] ) ``` 其中,'_test'表示生的扩展模块名称,sources列表中包括了Python代码和用于将Python代码转换C代码的包装器(test_wrap.c)。include_dirs需要包括NumPy的头文件路径,libraries需要包括Python文件名,library_dirs需要包括Python文件路径。 5. 在命令行中进入dist文件夹,执行以下命令,将扩展模块打包DLL文件: ``` python -m pip install wheel python -m pip install twine twine check dist/* ``` 6. 将生DLL文件复制到其他程序中,并使用ctypes模块进行加载和调用。例如,在C++程序中,可以使用以下代码: ```c++ // main.cpp #include <windows.h> #include <iostream> typedef int (*AddFunc)(int*, int*, int*, int); int main() { // 加载DLL文件 HINSTANCE hDll = LoadLibrary("test.cp36-win_amd64.pyd"); if (!hDll) { std::cout << "Load DLL failed!" << std::endl; return 1; } // 获取函数指针 AddFunc addFunc = (AddFunc)GetProcAddress(hDll, "add"); if (!addFunc) { std::cout << "Get function failed!" << std::endl; return 1; } // 准备参数 int a[3] = {1, 2, 3}; int b[3] = {4, 5, 6}; int c[3]; // 调用函数 addFunc(a, b, c, 3); for (int i = 0; i < 3; ++i) { std::cout << c[i] << " "; } std::cout << std::endl; // 卸载DLL文件 FreeLibrary(hDll); return 0; } ``` 其中,test.cp36-win_amd64.pyd是生DLL文件名。需要注意的是,Python的扩展模块使用了GIL(Global Interpreter Lock)来控制线程安全,因此在多线程环境下可能会有性能瓶颈。如果需要高性能的调用,可以使用类似Cython的工具将Python代码编译C/C++代码,再打包DLL文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值