0
点赞
收藏
分享

微信扫一扫

《Nuitka打包实战指南》实战打包Pandas

实战打包Pandas

打包示例源码下载:

请看文章末尾
 

版本信息:

pandas==1.3.5

Nuitka==0.6.19.1

打包环境:

Windows10 64位

打包代码如下:

import pandas as pd

df = pd.read_csv('./hello.csv')
data = df.head()
print(data)

 

项目目录如下:

运行截图如下:

打包分析如下:

  1. Pandas是一个相对来说比较大的第三方库,而且我们这个程序也没有用到其他库,所以我们用--nofollow-imports命令来避免引入过多的依赖,从而减少打包所需的时间。
  2. 如果用了--nofollow-imports,那么pandas的库是不会自动被添加到hello.dist文件夹中的,当然我们可以在打包之后手动复制进去。
  3. 不过也可以先去Python安装路径下的site-packages文件夹中把pandas这个库复制到项目目录下,然后用--include-data-dir打包进去,我们就采用这种方法。
  4. 程序用到一个资源文件hello.csv,所以我们要用--include-data-file命令打包进去。

此时项目目录如下:

打包命令如下:

nuitka --standalone --nofollow-imports --include-data-file=./hello.csv=./ --include-data-dir=./pandas=./pandas hello.py

打包结束后,运行hello.exe文件,发现闪退。于是将hello.exe文件拖入命令行窗口中回车运行:

这里提示我们少了这几个依赖:numpy,pytz和dateutil。我们直接从Python安装路径下的site-packages文件夹中复制过来,再次运行:

 还少一个six模块,同样复制six.py文件到hello.dist文件夹中。现在hello.dist看起来是这样的:

 红框中的就是我们刚刚复制过来的。再次运行exe,就变正常了:

所以我们可以修改下打包命令,把numpy,pytz,dateutil和six.py这几个文件(夹)先复制到项目路径下,然后用--include-date-dir和--include-data-file打包进去。修改后的项目目录和命令如下:

nuitka --standalone --nofollow-imports --include-data-file=./hello.csv=./ --include-data-file=./six.py=./ --include-data-dir=./pandas=./pandas --include-data-dir=./numpy=./numpy --include-data-dir=./pytz=./pytz --include-data-dir=./dateutil=./dateutil hello.py

打包示例源码下载:

链接:https://pan.baidu.com/s/1ERCw0g9qKS6ozVfj6NUSqA 
提取码:4c83

举报

相关推荐

0 条评论