python 生成exe文件

2026-08-06 11:36:48

大家好,又见面了,我是你们的朋友全栈君。

在windows下,可以使用pyinstaller打包python程序为exe可执行程序。

1、安装pyinstaller在cmd命令行窗口运行以下命令安装pyinstaller

代码语言:javascript复制pip install pyinstaller2、打包python程序在python程序所在目录,执行以下命令

代码语言:javascript复制# 切换到指定目录

cd /d path

# 正常打包命令

pyinstaller -F -w -i ico_path xxx.py-F 是将所有文件打成一个exe文件,一般是必写的(注意必须是大写)-w 是程序运行时不显示cmd界面-i 修改生成的exe文件图标,可以不写(-i 不写的话 ico_path也别写)ico_path 是生成的exe文件图标位置py_path 是目标py文件位置3、运行exe文件打包完成后,在对应目录会出现build和dist文件夹,exe文件就出现在dist文件夹,直接运行即可。

4、外部文件以我的chromedriver为例

打包生成exe文件后,依赖的文件还有chromedriver和谷歌浏览器(还需要版本一致)

所以在生成exe文件后,还需要将chromedriver和对应的谷歌浏览器版本一起

5、问题5.1、’pyinstall’ 不是内部或外部命令,也不是可运行的程序 或批处理文件。改为安装pyinstaller

5.2、exe点开之后就出现failed to execute script xxx存在中文路径使用pyinstaller时使用了-w命令与print冲突5.3、反复运行本身因为你开了进程,需要在main后面添加一句

代码语言:javascript复制multiprocessing.freeze_support()5.4、Pyinstaller打包selenium去除chromedriver黑框问题我的目录是

代码语言:javascript复制C:\Users\45906\AppData\Local\Programs\Python\Python37\Lib\site-packages\selenium\webdriver\common\service.py将其文件中的75行修改

代码语言:javascript复制def start(self):

"""

Starts the Service.

:Exceptions:

- WebDriverException : Raised either when it can't start the service

or when it can't connect to the service

"""

try:

cmd = [self.path]

cmd.extend(self.command_line_args())

self.process = subprocess.Popen(cmd, env=self.env,

close_fds=platform.system() != 'Windows',

stdout=self.log_file,

stderr=self.log_file,

#修改前

#stdin=PIPE)

#修改后

stdin=PIPE,creationflags=134217728)这里注意,是chromedriver的命令行黑框,并不是window本身的命令行,windows的黑框在你打包的时候添加-w即可

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131113.html原文链接:https://javaforall.cn