Python:使用pyinstaller打包开发完成的python代码

使用pyinstaller打包开发完成的python代码

当我们使用pycharm或者其他集成环境完成代码的开发之后,希望将代码打包起来,成为exe直接运行,不再依赖开发所配置好的环境,我们就可以使用python的第三方库pyinstaller进行打包。(以下实例基于pycharm环境)

  • 代码结构
    在这里插入图片描述
    图中以py结尾的都是用python编写的代码,images和sounds是代码中需要使用的一些素材,high_score是一个普通的文件,记录游戏的最高分。代码还使用到了一个第三方库–pygame。直接在pycharm中运行是完全没有问题的。
  • 安装pyinstaller–官网

直接使用pycharm的话,可以在下面的terminal使用命令或者settings–>Project Interpreter中安装pyinstaller,这里就不演示如何安装第三方库了。
在这里插入图片描述
安装完毕之后,应该可以在terminal界面看到
在这里插入图片描述
在这里插入图片描述

  • 打包应用
(venv) E:\PythonProjects\Alien_game>pyinstaller -w -F alien_invasion.py -p game_s
tats.py -p scoreboard.py -p button.py -p game_functions.py -p ships.py -p setting
s.py -p bullet.py -p E:\PythonProjects\Alien_game\venv\Lib\site-packages\pygame

解释:

-w 窗口模式打包,不显示控制台。(即待会在运行exe文件时,不会弹出控制台界面)
-F 生成一个启动程序,程序的主文件入口(也就是程序结构的主文件,负责调用其他文件那个)
-p 其他文件模块和使用到的第三方库

完整过程

(venv) E:\PythonProjects\Alien_game>pyinstaller -w -F alien_invasion.py -p game_s
tats.py -p scoreboard.py -p button.py -p game_functions.py -p ships.py -p setting
s.py -p bullet.py -p E:\PythonProjects\Alien_game\venv\Lib\site-packages\pygame
79 INFO: PyInstaller: 4.2
79 INFO: Python: 3.6.5
80 INFO: Platform: Windows-10-10.0.17763-SP0
81 INFO: wrote E:\PythonProjects\Alien_game\alien_invasion.spec
82 INFO: UPX is not available.
103 INFO: Extending PYTHONPATH with paths
['E:\\PythonProjects\\Alien_game',
 'E:\\PythonProjects\\Alien_game\\game_stats.py',
 'E:\\PythonProjects\\Alien_game\\scoreboard.py',
 'E:\\PythonProjects\\Alien_game\\button.py',
 'E:\\PythonProjects\\Alien_game\\game_functions.py',
 'E:\\PythonProjects\\Alien_game\\ships.py',
 'E:\\PythonProjects\\Alien_game\\settings.py',
 'E:\\PythonProjects\\Alien_game\\bullet.py',
 'E:\\PythonProjects\\Alien_game\\venv\\Lib\\site-packages\\pygame',
 'E:\\PythonProjects\\Alien_game']
pygame 2.0.1 (SDL 2.0.14, Python 3.6.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
166 INFO: checking Analysis
167 INFO: Building Analysis because Analysis-00.toc is non existent
167 INFO: Initializing module dependency graph...
171 INFO: Caching module graph hooks...
179 WARNING: Several hooks defined for module 'win32ctypes.core'. Please take car
e they do not conflict.
185 INFO: Analyzing base_library.zip ...
3153 INFO: Caching module dependency graph...
3241 INFO: running Analysis Analysis-00.toc
3262 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of fi
nal executable
  required by e:\python3.6.5\python.exe
3354 INFO: Analyzing E:\PythonProjects\Alien_game\alien_invasion.py
3764 INFO: Processing pre-find module path hook distutils from 'e:\\python3.6.5\\
lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.

3765 INFO: distutils: retargeting to non-venv dir 'e:\\python3.6.5\\lib'
3833 INFO: Processing pre-find module path hook site from 'e:\\python3.6.5\\lib\\
site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-site.py'.
3834 INFO: site: retargeting to fake-dir 'e:\\python3.6.5\\lib\\site-packages\\Py
Installer\\fake-modules'
7879 INFO: Processing module hooks...
7879 INFO: Loading module hook 'hook-pycparser.py' from 'e:\\python3.6.5\\lib\\si
te-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
7880 INFO: Loading module hook 'hook-win32ctypes.core.py' from 'e:\\python3.6.5\\
lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
8086 INFO: Loading module hook 'hook-pygame.py' from 'e:\\python3.6.5\\lib\\site-
packages\\pygame\\__pyinstaller'...
8116 INFO: Loading module hook 'hook-difflib.py' from 'e:\\python3.6.5\\lib\\site
-packages\\PyInstaller\\hooks'...
8118 INFO: Excluding import of doctest from module difflib
8119 INFO: Loading module hook 'hook-distutils.py' from 'e:\\python3.6.5\\lib\\si
te-packages\\PyInstaller\\hooks'...
8120 INFO: Loading module hook 'hook-distutils.util.py' from 'e:\\python3.6.5\\li
b\\site-packages\\PyInstaller\\hooks'...
8122 INFO: Excluding import of lib2to3.refactor from module distutils.util
8122 INFO: Loading module hook 'hook-encodings.py' from 'e:\\python3.6.5\\lib\\si
te-packages\\PyInstaller\\hooks'...
8225 INFO: Loading module hook 'hook-heapq.py' from 'e:\\python3.6.5\\lib\\site-p
ackages\\PyInstaller\\hooks'...
8227 INFO: Excluding import of doctest from module heapq
8228 INFO: Loading module hook 'hook-importlib_metadata.py' from 'e:\\python3.6.5
\\lib\\site-packages\\PyInstaller\\hooks'...
8230 INFO: Loading module hook 'hook-lib2to3.py' from 'e:\\python3.6.5\\lib\\site
-packages\\PyInstaller\\hooks'...
8305 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'e:\\python3.6
.5\\lib\\site-packages\\PyInstaller\\hooks'...
8306 INFO: Import to be excluded not found: 'test'
8307 INFO: Loading module hook 'hook-pickle.py' from 'e:\\python3.6.5\\lib\\site-
packages\\PyInstaller\\hooks'...
8310 INFO: Excluding import of argparse from module pickle
8311 INFO: Loading module hook 'hook-pkg_resources.py' from 'e:\\python3.6.5\\lib
\\site-packages\\PyInstaller\\hooks'...
8842 INFO: Processing pre-safe import module hook win32com from 'e:\\python3.6.5\
\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\h
ook-win32com.py'.
8901 WARNING: Hidden import "pkg_resources.py2_warn" not found!
8901 WARNING: Hidden import "pkg_resources.markers" not found!
8903 INFO: Excluding import of __main__ from module pkg_resources
8905 INFO: Loading module hook 'hook-setuptools.py' from 'e:\\python3.6.5\\lib\\s
ite-packages\\PyInstaller\\hooks'...
9535 INFO: Import to be excluded not found: 'setuptools.py33compat'
9536 INFO: Import to be excluded not found: 'setuptools.py27compat'
9536 INFO: Loading module hook 'hook-sysconfig.py' from 'e:\\python3.6.5\\lib\\si
te-packages\\PyInstaller\\hooks'...
9537 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'e:\\python3.6.5\\li
b\\site-packages\\PyInstaller\\hooks'...
9538 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'e:\\python3
.6.5\\lib\\site-packages\\PyInstaller\\hooks'...
9539 INFO: Loading module hook 'hook-xml.py' from 'e:\\python3.6.5\\lib\\site-pac
kages\\PyInstaller\\hooks'...
9540 INFO: Loading module hook 'hook-setuptools.msvc.py' from 'e:\\python3.6.5\\l
ib\\site-packages\\PyInstaller\\hooks'...
9542 INFO: Excluding import of numpy from module setuptools.msvc
9554 INFO: Looking for ctypes DLLs
9584 INFO: Analyzing run-time hooks ...
9588 INFO: Including run-time hook 'e:\\python3.6.5\\lib\\site-packages\\PyInstal
ler\\hooks\\rthooks\\pyi_rth_pkgres.py'
9594 INFO: Looking for dynamic libraries
10307 INFO: Looking for eggs
10307 INFO: Using Python library e:\python3.6.5\python36.dll
10307 INFO: Found binding redirects:
[]
10312 INFO: Warnings written to E:\PythonProjects\Alien_game\build\alien_invasion
\warn-alien_invasion.txt
10367 INFO: Graph cross-reference written to E:\PythonProjects\Alien_game\build\a
lien_invasion\xref-alien_invasion.html
10376 INFO: checking PYZ
10377 INFO: Building PYZ because PYZ-00.toc is non existent
10377 INFO: Building PYZ (ZlibArchive) E:\PythonProjects\Alien_game\build\alien_i
nvasion\PYZ-00.pyz
11364 INFO: Building PYZ (ZlibArchive) E:\PythonProjects\Alien_game\build\alien_i
nvasion\PYZ-00.pyz completed successfully.
11374 INFO: checking PKG
11374 INFO: Building PKG because PKG-00.toc is non existent
11374 INFO: Building PKG (CArchive) PKG-00.pkg
14091 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
14093 INFO: Bootloader e:\python3.6.5\lib\site-packages\PyInstaller\bootloader\Wi
ndows-64bit\runw.exe
14094 INFO: checking EXE
14094 INFO: Building EXE because EXE-00.toc is non existent
14094 INFO: Building EXE from EXE-00.toc
14097 INFO: Copying icons from ['e:\\python3.6.5\\lib\\site-packages\\PyInstaller
\\bootloader\\images\\icon-windowed.ico']
14098 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
14098 INFO: Writing RT_ICON 1 resource with 3752 bytes
14098 INFO: Writing RT_ICON 2 resource with 2216 bytes
14099 INFO: Writing RT_ICON 3 resource with 1384 bytes
14099 INFO: Writing RT_ICON 4 resource with 38188 bytes
14099 INFO: Writing RT_ICON 5 resource with 9640 bytes
14099 INFO: Writing RT_ICON 6 resource with 4264 bytes
14100 INFO: Writing RT_ICON 7 resource with 1128 bytes
14109 INFO: Updating manifest in E:\PythonProjects\Alien_game\build\alien_invasio
n\runw.exe.rosxpanp
14109 INFO: Updating resource type 24 name 1 language 0
14115 INFO: Appending archive to EXE E:\PythonProjects\Alien_game\dist\alien_inva
sion.exe
14123 INFO: Building EXE from EXE-00.toc completed successfully.
  • 查看
    回到windows下的目录,发现代码目录下增加三个文件,build目录,dist目录和一个以spec结尾的文件。
    在这里插入图片描述
    将程序需要使用到的资源拷贝到dist目录下
    在这里插入图片描述
    运行exe文件,程序就会按照预期运行。
  • 排错
    如果构建之后无法正常运行,出现闪退或者其他问题,先打开cmd窗口,按下面的方式运行exe文件,看看报错再找办法。
    在这里插入图片描述

如果需要重新构建,将原来三个相关的文件删除即可。

猜你喜欢

转载自blog.csdn.net/rookie23rook/article/details/113942757