python 代码如何打包成可执行文件.exe文件(Pyinstaller)

前言


python写的代码如何打包.exe可执行程序,让别人电脑上没安装过 python 的小伙伴也可以直接运行?
本篇讲如何用PyInstaller库一步步打包python代码。

PyInstaller 环境准备


我的电脑环境:

  1. 系统ubuntu 20
  2. python 3.6.5

使用pip安装PyInstaller

pip install PyInstaller 

安装完查看版本号

leenhem@DESKTOP-I6DTQAI:/mnt/d/workspace/code/pachong$ pip show PyInstaller
Name: pyinstaller
Version: 4.3
Summary: PyInstaller bundles a Python application and all its dependencies into a single package.
Home-page: http://www.pyinstaller.org/
Author: Hartmut Goebel, Giovanni Bajo, David Vierra, David Cortesi, Martin Zibricky
Author-email: None
License: GPLv2-or-later with a special exception which allows to use PyInstaller to build and distribute non-free programs (including commercial ones)
Location: /home/leenhem/.local/lib/python3.8/site-packages
Requires: setuptools, altgraph, pyinstaller-hooks-contrib
Required-by: 

打包可执行文件


我写了一段简单的 requests 代码code404.py

"""
使用requests库获取我的博客首页文章地址
cod404_码404 blog:https://www.code404.icu/
"""
import requests
import re

r = requests.get("https://www.code404.icu/")
# 匹配首页blog地址
res = re.findall(r'<a class="item-img-inner" href="(.+?)" title=', r.text)
for i in res:
    print("blog:", i)

命令行运行结果

leenhem@DESKTOP-I6DTQAI:/mnt/d/workspace/code/pachong$ python3 code404.py 
blog: https://www.code404.icu/901.html
blog: https://www.code404.icu/898.html
blog: https://www.code404.icu/886.html
blog: https://www.code404.icu/876.html
blog: https://www.code404.icu/871.html
blog: https://www.code404.icu/862.html
blog: https://www.code404.icu/851.html
blog: https://www.code404.icu/849.html
blog: https://www.code404.icu/844.html
blog: https://www.code404.icu/838.html
blog: https://www.code404.icu/835.html
blog: https://www.code404.icu/830.html

这段代码依赖第三方库requests,需在python目录Lib\site-packages下找到requests

找到后整个文件夹全部复制到代码根目录

cd 到项目跟目录pachong下执行命令

pyinstaller -F yoyoblog.py

执行过程如下

leenhem@DESKTOP-I6DTQAI:/mnt/d/workspace/code/pachong$ pyinstaller -F code404.py 
124 INFO: PyInstaller: 4.3
124 INFO: Python: 3.8.5
141 INFO: Platform: Linux-4.4.0-22000-Microsoft-x86_64-with-glibc2.29
143 INFO: wrote /mnt/d/workspace/code/pachong/code404.spec
174 INFO: UPX is not available.
180 INFO: Extending PYTHONPATH with paths
['/mnt/d/workspace/code/pachong', '/mnt/d/workspace/code/pachong']
274 INFO: checking Analysis
274 INFO: Building Analysis because Analysis-00.toc is non existent
275 INFO: Initializing module dependency graph...
285 INFO: Caching module graph hooks...
336 INFO: Analyzing base_library.zip ...
3658 INFO: Processing pre-find module path hook distutils from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks/pre_find_module_path/hook-distutils.py'.
3664 INFO: distutils: retargeting to non-venv dir '/usr/lib/python3.8'
7524 INFO: Caching module dependency graph...
7701 INFO: running Analysis Analysis-00.toc
8139 INFO: Analyzing /mnt/d/workspace/code/pachong/code404.py
8451 INFO: Processing pre-safe import module hook six.moves from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks/pre_safe_import_module/hook-six.moves.py'.
12641 INFO: Processing module hooks...
12641 INFO: Loading module hook 'hook-certifi.py' from '/home/leenhem/.local/lib/python3.8/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'...
12657 INFO: Loading module hook 'hook-cryptography.py' from '/home/leenhem/.local/lib/python3.8/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'...
13115 INFO: Loading module hook 'hook-difflib.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13123 INFO: Loading module hook 'hook-distutils.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13157 INFO: Loading module hook 'hook-distutils.util.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13166 INFO: Loading module hook 'hook-encodings.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13334 INFO: Loading module hook 'hook-heapq.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13340 INFO: Loading module hook 'hook-lib2to3.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13400 INFO: Loading module hook 'hook-multiprocessing.util.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13404 INFO: Loading module hook 'hook-pickle.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13410 INFO: Loading module hook 'hook-sysconfig.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13414 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13421 INFO: Loading module hook 'hook-xml.py' from '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
13649 INFO: Looking for ctypes DLLs
13688 INFO: Analyzing run-time hooks ...
13696 INFO: Including run-time hook '/home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_multiprocessing.py'
13706 INFO: Including run-time hook '/home/leenhem/.local/lib/python3.8/site-packages/_pyinstaller_hooks_contrib/hooks/rthooks/pyi_rth_certifi.py'
13727 INFO: Looking for dynamic libraries
22824 INFO: Looking for eggs
22829 INFO: Python library not in binary dependencies. Doing additional searching...
23392 INFO: Using Python library /lib/x86_64-linux-gnu/libpython3.8.so.1.0
23419 INFO: Warnings written to /mnt/d/workspace/code/pachong/build/code404/warn-code404.txt
23511 INFO: Graph cross-reference written to /mnt/d/workspace/code/pachong/build/code404/xref-code404.html
23585 INFO: checking PYZ
23586 INFO: Building PYZ because PYZ-00.toc is non existent
23586 INFO: Building PYZ (ZlibArchive) /mnt/d/workspace/code/pachong/build/code404/PYZ-00.pyz
24411 INFO: Building PYZ (ZlibArchive) /mnt/d/workspace/code/pachong/build/code404/PYZ-00.pyz completed successfully.
24429 INFO: checking PKG
24429 INFO: Building PKG because PKG-00.toc is non existent
24429 INFO: Building PKG (CArchive) PKG-00.pkg
27409 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
27414 INFO: Bootloader /home/leenhem/.local/lib/python3.8/site-packages/PyInstaller/bootloader/Linux-64bit/run
27414 INFO: checking EXE
27415 INFO: Building EXE because EXE-00.toc is non existent
27415 INFO: Building EXE from EXE-00.toc
27421 INFO: Appending archive to ELF section in EXE /mnt/d/workspace/code/pachong/dist/code404
27524 INFO: Building EXE from EXE-00.toc completed successfully.

看到successfully 就是成功了

pyinstaller 一些常用参数命令


  • -F 表示生成单个可执行文件
  • -w 表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!
  • -p 表示你自己自定义需要加载的类路径,一般情况下用不到
  • -i 表示可执行文件的图标

更多参数可以通过 pyinstaller -h 查看

执行可执行文件文件
打包完成后会在 dist 目录看到一个 code404 文件

运行可执行文件


可以在shell窗口执行linux 可执行 文件

leenhem@DESKTOP-I6DTQAI:/mnt/d/workspace/code/pachong$ ./dist/code404 
blog: https://www.code404.icu/901.html
blog: https://www.code404.icu/898.html
blog: https://www.code404.icu/886.html
blog: https://www.code404.icu/876.html
blog: https://www.code404.icu/871.html
blog: https://www.code404.icu/862.html
blog: https://www.code404.icu/851.html
blog: https://www.code404.icu/849.html
blog: https://www.code404.icu/844.html
blog: https://www.code404.icu/838.html
blog: https://www.code404.icu/835.html
blog: https://www.code404.icu/830.html

说明


  1. Pyinstaller只是将Python解释器和脚本打包成一个可执行文件,不同于编译成真正的机器码。所以代码的运行效率是降低的,只是解决了小白用户的操作难度。
  2. 打包时会分析项目的依赖,并将相关的依赖打包进来,并进行加密处理
  3. 不同的操作系统,需要在不同的操作系统进行打包。我们这次打包环境是ubuntu,所以打包出来的就是linux下的可执行文件,没有.exe后缀

版权声明:本文为作者原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原创文章,作者:老C,如若转载,请注明出处:https://www.code404.icu/904.html

发表评论

登录后才能评论