pyinstaller 打包


[root@localhost ~]# pip3 install pyinstaller
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pyinstaller
  Downloading https://files.pythonhosted.org/packages/9e/ed/fbdad7f5d8f794c901076b814b8e9f5ce31d32c0bc3b63ddd27b61db9530/pyinstaller-4.1.tar.gz (3.5MB)
    100% |████████████████████████████████| 3.5MB 370kB/s
Requirement already satisfied: setuptools in /usr/lib/python3.6/site-packages (from pyinstaller)
Collecting altgraph (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/ee/3d/bfca21174b162f6ce674953f1b7a640c1498357fa6184776029557c25399/altgraph-0.17-py2.py3-none-any.whl
Collecting pyinstaller-hooks-contrib>=2020.6 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/2a/e6/4a47a7d41266cbca531bed3031c6412f9601ba2a2319e6d8c43f6917c51d/pyinstaller_hooks_contrib-2020.10-py2.py3-none-any.whl (166kB)
    100% |████████████████████████████████| 174kB 4.1MB/s
Installing collected packages: altgraph, pyinstaller-hooks-contrib, pyinstaller
  Running setup.py install for pyinstaller ... done
Successfully installed altgraph-0.17 pyinstaller-4.1 pyinstaller-hooks-contrib-2020.10
[root@localhost ~]# vim test.py
[root@localhost ~]# python3 test.py
hello world
[root@localhost ~]# python test.py
bash: python: command not found...
[root@localhost ~]# pyinstaller -F test.py
57 INFO: PyInstaller: 4.1
58 INFO: Python: 3.6.8
59 INFO: Platform: Linux-5.10.0-rc3-next-20201113-aarch64-with-centos-8.2.2004-Core
59 INFO: wrote /root/test.spec
61 INFO: UPX is not available.
62 INFO: Extending PYTHONPATH with paths
['/root', '/root']
74 INFO: checking Analysis
74 INFO: Building Analysis because Analysis-00.toc is non existent
74 INFO: Initializing module dependency graph...
75 INFO: Caching module graph hooks...
85 INFO: Analyzing base_library.zip ...
4668 INFO: Caching module dependency graph...
4781 INFO: running Analysis Analysis-00.toc
4794 INFO: Analyzing /root/test.py
4796 INFO: Processing module hooks...
4797 INFO: Loading module hook 'hook-difflib.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
4798 INFO: Excluding import of doctest from module difflib
4798 INFO: Loading module hook 'hook-encodings.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
4865 INFO: Loading module hook 'hook-heapq.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
4866 INFO: Excluding import of doctest from module heapq
4866 INFO: Loading module hook 'hook-pickle.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
4868 INFO: Excluding import of argparse from module pickle
4868 INFO: Loading module hook 'hook-xml.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
5223 INFO: Looking for ctypes DLLs
5223 INFO: Analyzing run-time hooks ...
5231 INFO: Looking for dynamic libraries
5466 INFO: Looking for eggs
5466 INFO: Using Python library /lib64/libpython3.6m.so.1.0
5469 INFO: Warnings written to /root/build/test/warn-test.txt
5499 INFO: Graph cross-reference written to /root/build/test/xref-test.html
5509 INFO: checking PYZ
5509 INFO: Building PYZ because PYZ-00.toc is non existent
5510 INFO: Building PYZ (ZlibArchive) /root/build/test/PYZ-00.pyz
5984 INFO: Building PYZ (ZlibArchive) /root/build/test/PYZ-00.pyz completed successfully.
5988 INFO: checking PKG
5988 INFO: Building PKG because PKG-00.toc is non existent
5988 INFO: Building PKG (CArchive) PKG-00.pkg
8297 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
8299 INFO: Bootloader /usr/local/lib/python3.6/site-packages/PyInstaller/bootloader/Linux-64bit-aarch/run
8299 INFO: checking EXE
8299 INFO: Building EXE because EXE-00.toc is non existent
8299 INFO: Building EXE from EXE-00.toc
8300 INFO: Appending archive to ELF section in EXE /root/dist/test
8307 INFO: Building EXE from EXE-00.toc completed successfully.

在ARM linux上运行:
[root@localhost dist]# ls
test
[root@localhost dist]# ./test
hello world

将其放到另一台linux上,内核版本不同的ARM linux上运行
[root@localhost ~]# uname -a
Linux localhost.localdomain 4.18.0-147.8.1.el8_1.aarch64+debug #1 SMP Thu Apr 9 13:52:42 UTC 2020 aarch64 aarch64 aarch64 GNU/Linux
[root@localhost ~]# ./test
-bash: ./test: Permission denied
[root@localhost ~]# chmod 777 test
[root@localhost ~]# ./test
hello world


在x86 linux 上运行

tiantao@BoardServer2:~$ ./test
-bash: ./test: cannot execute binary file: Exec format error


卸载掉python3 ,test.py 已经无法执行了,但是pyinstaller打包的还是可以执行,说明打包后已经依赖python3
[root@localhost dist]# python3 test.py
-bash: /usr/bin/python3: No such file or directory
[root@localhost dist]# ./test
hello world

通过readelf 来看,这个test的已经是elf 格式的,这也证明了通过pyinstaller打包是分架构的
[root@localhost dist]# readelf -a test
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           AArch64
  Version:                           0x1
  Entry point address:               0x401a34
  Start of program headers:          64 (bytes into file)
  Start of section headers:          5683040 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         29
  Section header string table index: 28

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: Pyinstaller是一个可以将Python脚本打包成可执行文件的工具,其主要功能是将Python程序自动转换为独立且可执行的二进制文件。使用Pyinstaller打包可以实现在没有Python环境的计算机上执行Python程序的目的。 Pyinstaller的使用相对简单,只需在命令行输入打包命令即可,同时还可以为打包程序添加各种参数,比如生成单个可执行文件,优化程序运行速度等。 使用Pyinstaller的步骤大致分为如下几个: 1. 安装Pyinstaller:可以使用pip等命令行工具进行安装,安装完成后即可使用。 2. 编写Python程序:编写好Python程序后,使用Pyinstaller打包命令即可将程序打包。 3. 打包程序:可以使用命令行进行打包操作,也可以使用Pyinstaller提供的GUI界面来完成打包操作,打包完成后即可在dist目录下找到生成的可执行文件。 4. 配置打包参数:根据需要,可以对打包参数进行调整,比如设置生成的可执行文件名称、添加支持的文件类型等。 总的来说,Pyinstaller打包是一个非常方便的工具,可以让Python程序更方便地在其他计算机中运行,也可以更好地保护程序的源代码,使其不易被别人窃取。 ### 回答2: PyInstaller是一个很好用的Python打包工具,可以将Python代码打包成一个独立的可执行文件,方便程序的使用和部署。下面详细介绍一下PyInstaller的使用。 1. 安装PyInstaller 可以使用pip安装PyInstaller,输入以下命令即可: ```bash pip install pyinstaller ``` 2. 打包命令 打包命令非常简单,只需要进入项目的根目录,然后在终端中输入以下命令即可: ```bash pyinstaller -F file.py ``` 其中,file.py为你要打包的Python文件名。-F参数表示生成单个可执行文件。生成的可执行文件在dist目录下。 3. 打包选项 PyInstaller还提供了很多打包选项,可以根据需要进行设置。下面列举一些常用选项: - -F,--onefile:生成单个可执行文件。 - -D,--onedir:生成目录的打包文件,包含多个文件。 - -i,--icon=FILE.ico:设置生成的可执行文件图标。 - --add-data:添加附加文件或目录。例:--add-data 'images/*.png:images'。 - --hidden-import:隐式导入,添加未自动检测到的模块。例:--hidden-import ‘pymysql’。 4. 打包注意事项 打包注意事项如下: - PyInstaller打包时,可能会遇到一些Python模块无法正常导入的情况。这时,可以使用-h命令查看帮助文档,确定需要添加哪些选项。 - 打包出来的可执行文件可能会被杀毒软件误判为病毒,这时可以尝试关闭杀毒软件或者将生成的可执行文件添加到白名单中。 总的来说,PyInstaller非常适合用于将Python程序打包成可执行文件。在使用过程中,需要注意的一些细节,但是只要根据需要设置选项,就能够很方便地生成可执行文件。 ### 回答3: PyInstaller是一个用于将Python应用程序打包为可执行文件的工具。使用它可以轻松地将Python程序打包成独立的可执行文件,无需安装Python解释器,同时还可以将程序和所有相关依赖打包成单个文件。 PyInstaller支持在Windows、Mac OS X和Linux等多个平台上打包Python应用程序,并且可以将Python脚本转换成一些常见的可执行文件格式,例如Windows下的exe、Mac OS X下的app和Linux下的bin。 使用PyInstaller打包Python程序需要以下步骤: 第一步:安装PyInstaller 使用pip安装PyInstaller:pip install pyinstaller 第二步:创建打包文件的.spec文件 在命令行中执行pyinstaller命令,创建一个.spec文件。该文件包含了打包所需要的参数。 例如:pyinstaller hello.py --name=hello --onefile 执行上述命令后,会在当前目录下生成一个.spec文件,它包含了打包所需要的参数。 第三步:编辑.spec文件 使用文本编辑器打开.spec文件,根据需要修改其中的参数。参数的详细说明可以在PyInstaller的官方文档中查看。 第四步:执行打包命令 使用命令行中执行pyinstaller命令,指定.spec文件作为参数来进行打包。 例如:pyinstaller hello.spec 执行该命令后,将生成可执行文件和各种附属文件,包括打包的Python解释器和所需的库文件等。 使用PyInstaller打包Python程序可以帮助开发者简化应用程序的部署过程,使得用户可以更加方便地使用Python应用程序。同时,打包后的程序也具有更好的可移植性和安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值