600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Python生成exe可执行文件的两种方法(py2exe和pyinstaller)

Python生成exe可执行文件的两种方法(py2exe和pyinstaller)

时间:2024-05-16 15:09:17

相关推荐

Python生成exe可执行文件的两种方法(py2exe和pyinstaller)

C:\Users\Administrator>python -m pip install py2exe

D:\Python\Python38\python.exe: No module named pip

上述出现没有pip模块的错误,说明没有安装pip,安装即可

windows安装pip

C:\Users\Administrator>python -m ensurepip

安装完了之后将会在D:\python\Python38\Scripts里面看到pip3.8.exe,pip3.exe

再次安装py2exe之后将在脚本目录看到build_exe.exe

C:\Users\Administrator>python -u D:\python\setup.py py2exe

running py2exe

Traceback (most recent call last):

File "D:\python\setup.py", line 10, in <module>

setup(

File "D:\Python\Python38\lib\distutils\core.py", line 148, in setup

dist.run_commands()

File "D:\Python\Python38\lib\distutils\dist.py", line 966, in run_commands

self.run_command(cmd)

File "D:\Python\Python38\lib\distutils\dist.py", line 985, in run_command

cmd_obj.run()

File "D:\Python\Python38\lib\site-packages\py2exe\distutils_buildexe.py", line

188, in run

self._run()

File "D:\Python\Python38\lib\site-packages\py2exe\distutils_buildexe.py", line

267, in _run

builder.analyze()

File "D:\Python\Python38\lib\site-packages\py2exe\runtime.py", line 160, in an

alyze

self.mf.import_hook(modname)

File "D:\Python\Python38\lib\site-packages\py2exe\mf3.py", line 120, in import

_hook

module = self._gcd_import(name)

File "D:\Python\Python38\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_i

mport

return self._find_and_load(name)

File "D:\Python\Python38\lib\site-packages\py2exe\mf3.py", line 357, in _find_

and_load

self._scan_code(module.__code__, module)

File "D:\Python\Python38\lib\site-packages\py2exe\mf3.py", line 388, in _scan_

code

for what, args in self._scan_opcodes(code):

File "D:\Python\Python38\lib\site-packages\py2exe\mf3.py", line 417, in _scan_

opcodes

yield "store", (names[oparg],)

IndexError: tuple index out of range

上述是python3.8新版本报错,下载python3.4版本,可以解决!

hi.py

import osdef test(info):print("Tony职业:"+info)if __name__=="__main__":info=input("Tony职业是什么?")test(info)os.system('pause') #暂停,回车或任意键再销毁控制台

setup.py

from distutils.core import setupimport py2exeoptions={"bundle_files":1, #1.打包并包含python解释器;2.打包但不包含解释器;3.默认不打包"compressed":1,#1.压缩;2.不压缩"optimize":2, #0.不优化;1.一般优化;2.额外优化}setup(console=["D:\python\hi.py"], #console|windowsoptions={"py2exe":options},zipfile=None)

python -u D:\python\setup.py py2exe

将会在dist目录下面有一个hi.exe的可执行文件,这种生成的执行文件包含有python解释器,不需要安装有python环境也可以执行!

还有下面另外一种方法:

pip install -i /simple pyinstaller

pyinstaller -F -w -i xxx.ico yy.py

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。