exe格式转换成txt(原来将 Python 程序打包成exe文件这么简单!只需三步)

一、在cmd命令行执行安装pyinstaller工具

pip install pyinstaller


二、下载安装pyinstaler运行时所需windows扩展:pywin32

https://github.com/mhammond/pywin32/releases 选择最新版的下载,注意选择与Python版本和位数对应的 如32位python3.7.4对应下载:【pywin32-223.win32-py3.7.exe】 如64位python3.7.4对应下载:【pywin32-223.win-amd64-py3.7.exe】

三、在命令行中执行打包命令即可

格式如下: pyinstaller [opts] yourprogram.py 参数含义 -F 指定打包后只生成一个exe格式的文件(建议写上这个参数) -D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项) -c –console, –nowindowed 使用控制台,无界面(默认) -w –windowed, –noconsole 使用窗口,无控制台 -p 添加搜索路径,让其找到对应的库。 -i 改变生成程序的icon图标 例1.指定文件路径打包:pyinstaller -F D:projecttanchishe.py 例2.不带窗口,后台运行打包:pyinstaller -F -w D:projecttanchishe.py 例3.更换程序图标打包:pyinstaller -F -w -i D:projecttest.ico D:projecttanchishe.py

这里咱们执行开始打包: (venv) C:Userswww_9PycharmProjectspython-tanchishe>pyinstaller -F tanchishe.py

四、打包结果如下:


exe格式转换

五、运行打包好的exe文件tanchishe.exe效果如下:


六、报错处理:

  • 执行Pyinstaller 打包的exe文件执行报错如下(我用的Python版本为:python3.7.5):

Microsoft Windows [版本 10.0.18363.778] (c) 2019 Microsoft Corporation。保留所有权利。 C:Userswww_9>C:Userswww_9PycharmProjectspython-tanchishedisttanchishe.exe Traceback (most recent call last): File "libsite-packagesPyInstallerloaderrthookspyi_rth_pkgres.py", line 13, in <module> File "c:userswww_9pycharmprojectspython-tanchishevenvlibsite-packagesPyInstallerloaderpyimod03_importers.py", line 623, in exec_module exec(bytecode, module.__dict__) File "libsite-packagespkg_resources__init__.py", line 86, in <module> ModuleNotFoundError: No module named 'pkg_resources.py2_warn' [30844] Failed to execute script pyi_rth_pkgres C:Userswww_9>

  • 解决办法(卸载高版本setuptools ,然后安装setuptools44.0):
    1. 卸载高版本:pip uninstall setuptools

    (venv) C:Userswww_9PycharmProjectspython-tanchishe>pip uninstall setuptools Found existing installation: setuptools 46.1.3 Uninstalling setuptools-46.1.3: Would remove: c:userswww_9pycharmprojectspython-tanchishevenvlibsite-packageseasy_install.py c:userswww_9pycharmprojectspython-tanchishevenvlibsite-packagespkg_resources* c:userswww_9pycharmprojectspython-tanchishevenvlibsite-packagessetuptools-46.1.3.dist-info* c:userswww_9pycharmprojectspython-tanchishevenvlibsite-packagessetuptools* c:userswww_9pycharmprojectspython-tanchishevenvscriptseasy_install-3.7.exe c:userswww_9pycharmprojectspython-tanchishevenvscriptseasy_install.exe Proceed (y/n)? y Successfully uninstalled setuptools-46.1.3

    1. 安装低版本: pip install setuptools==44.0.0

    (venv) C:Userswww_9PycharmProjectspython-tanchishe>pip install setuptools==44.0.0 Collecting setuptools==44.0.0 Downloading setuptools-44.0.0-py2.py3-none-any.whl (583 kB) |████████████████████████████████| 583 kB 26 kB/s Installing collected packages: setuptools Successfully installed setuptools-44.0.0

    1. 重新打包程序:>pyinstaller -F tanchishe.py

    (venv) C:Userswww_9PycharmProjectspython-tanchishe>pyinstaller -F tanchishe.py 97 INFO: PyInstaller: 3.6 98 INFO: Python: 3.7.5 98 INFO: Platform: Windows-10-10.0.18362-SP0 100 INFO: wrote C:Userswww_9PycharmProjectspython-tanchishetanchishe.spec 118 INFO: UPX is not available. 120 INFO: Extending PYTHONPATH with paths ['C:Userswww_9PycharmProjectspython-tanchishe', 'C:Userswww_9PycharmProjectspython-tanchishe'] 120 INFO: checking Analysis 125 INFO: Building because c:userswww_9pycharmprojectspython-tanchishevenvlibsite-packagespkg_resources__init__.py changed 125 INFO: Initializing module dependency graph... 131 INFO: Caching module graph hooks... 138 INFO: Analyzing base_library.zip ... 3643 INFO: Caching module dependency graph... 3794 INFO: running Analysis Analysis-00.toc 3800 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by c:userswww_9pycharmprojectspython-tanchishevenvscriptspython.exe 3824 INFO: Analyzing C:Userswww_9PycharmProjectspython-tanchishetanchishe.py 4200 INFO: Processing pre-find module path hook distutils 4201 INFO: distutils: retargeting to non-venv dir 'C:Program Files (x86)Microsoft Visual StudioSharedPython37_64lib' 4264 INFO: Processing pre-find module path hook site 4265 INFO: site: retargeting to fake-dir 'c:userswww_9pycharmprojectspython-tanchishevenvlibsite-packagesPyInstallerfake-modules' 5648 INFO: Processing module hooks... 5648 INFO: Loading module hook "hook-distutils.py"... 5651 INFO: Loading module hook "hook-encodings.py"... 5860 INFO: Loading module hook "hook-pkg_resources.py"... 6338 INFO: Processing pre-safe import module hook win32com Traceback (most recent call last): File "<string>", line 2, in <module> ModuleNotFoundError: No module named 'win32com' 6464 INFO: Processing pre-safe import module hook win32com Traceback (most recent call last): File "<string>", line 2, in <module> ModuleNotFoundError: No module named 'win32com' 6790 INFO: Excluding import '__main__' 6792 INFO: Removing import of __main__ from module pkg_resources 6792 INFO: Loading module hook "hook-pydoc.py"... 6794 INFO: Loading module hook "hook-pygame.py"... 6795 WARNING: Hidden import "pygame._view" not found! 6795 INFO: Loading module hook "hook-sysconfig.py"... 6798 INFO: Loading module hook "hook-xml.dom.domreg.py"... 6799 INFO: Loading module hook "hook-xml.etree.cElementTree.py"... 6800 INFO: Loading module hook "hook-xml.py"... 6827 INFO: Looking for ctypes DLLs 6842 INFO: Analyzing run-time hooks ... 6848 INFO: Including run-time hook 'pyi_rth_pkgres.py' 6853 INFO: Looking for dynamic libraries 8980 INFO: Looking for eggs 8980 INFO: Using Python library C:Program Files (x86)Microsoft Visual StudioSharedPython37_64python37.dll 8981 INFO: Found binding redirects: [] 8985 INFO: Warnings written to C:Userswww_9PycharmProjectspython-tanchishebuildtanchishewarn-tanchishe.txt 9035 INFO: Graph cross-reference written to C:Userswww_9PycharmProjectspython-tanchishebuildtanchishexref-tanchishe.html 9054 INFO: checking PYZ 9056 INFO: Building because toc changed 9057 INFO: Building PYZ (ZlibArchive) C:Userswww_9PycharmProjectspython-tanchishebuildtanchishePYZ-00.pyz 9922 INFO: Building PYZ (ZlibArchive) C:Userswww_9PycharmProjectspython-tanchishebuildtanchishePYZ-00.pyz completed successfully. 9943 INFO: checking PKG 9946 INFO: Building because toc changed 9946 INFO: Building PKG (CArchive) PKG-00.pkg 12773 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 12779 INFO: Bootloader c:userswww_9pycharmprojectspython-tanchishevenvlibsite-packagesPyInstallerbootloaderWindows-64bitrun.exe 12780 INFO: checking EXE 12783 INFO: Building because toc changed 12783 INFO: Building EXE from EXE-00.toc 12786 INFO: Appending archive to EXE C:Userswww_9PycharmProjectspython-tanchishedisttanchishe.exe 12796 INFO: Building EXE from EXE-00.toc completed successfully. (venv) C:Userswww_9PycharmProjectspython-tanchishe>

    1. 然后运行编译所生成的exe文件

    C:Userswww_9>C:Userswww_9PycharmProjectspython-tanchishedisttanchishe.exe pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html C:Userswww_9>

    1. tanchishe.exe可以正常运行了

    可参考资料:搜索收集的相关知识页面,这里记录下来,方便日后参考。

    用pyinstaller可以将.py文件转换成.exe格式,让你的程序在没有安装python环境的windows上运行: 您也可以参考这里:https://zhuanlan.zhihu.com/p/38659588 报错解决参考地址: Pyinstaller No module named pkg_resources.py2_warn: https://blog.51cto.com/cfy10/2468597?source=dra pyInstaller打包完成以后运行程序时出现ModuleNotFoundError: No module named 'pkg_resources.py2_warn: https://blog.csdn.net/BINGYiii/article/details/104193180 windows下安装setuptools与pip: https://www.cnblogs.com/longronglang/p/10443625.html 下载不同版本 settools 不同版本地址: https://pypi.Python.org/pypi/setuptools setuptools 44.0版本地址: https://pypi.org/project/setuptools/44.0.0/

    您可以还会对下面的文章感兴趣

    最新评论

    1. 腿毛大叔
      腿毛大叔
      发布于:2022-04-27 15:34:33 回复TA
      O: Processing pre-find module path hook distutils4201 INFO: distutils: retargeting to non-venv dir 'C:Program Files (x86)Microsoft Visual S
    1. 摁着强吻你
      摁着强吻你
      发布于:2022-04-27 10:07:01 回复TA
      k (most recent call last): File "<string>", line 2, in <module>ModuleNotFoundError: No module named &#
    1. 撲克臉
      撲克臉
      发布于:2022-04-27 07:45:34 回复TA
      ndError: No module named 'win32com'6464 INFO: Processing pre-safe import module
    1. 齐妮飞蓉
      齐妮飞蓉
      发布于:2022-04-27 03:44:06 回复TA
      别见外
    1. 葛民萍蓝
      葛民萍蓝
      发布于:2022-04-27 03:44:06 回复TA
      笨人的爱情是批发出去的,仅凭“让我一次爱个够”的蛮力;聪明人的爱情是零售出去的,懂得“只爱一点点”的精妙。

    ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

    使用微信扫描二维码后

    点击右上角发送给好友