天天看点

python 生成exe文件1、安装pyinstaller2、打包python程序3、运行exe文件4、外部文件5、问题

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

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

1、安装pyinstaller

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

pip install pyinstaller           

复制

2、打包python程序

在python程序所在目录,执行以下命令

# 切换到指定目录
cd /d path
# 正常打包命令
pyinstaller -F -w -i ico_path xxx.py           

复制

  1. -F 是将所有文件打成一个exe文件,一般是必写的(注意必须是大写)
  2. -w 是程序运行时不显示cmd界面
  3. -i 修改生成的exe文件图标,可以不写(-i 不写的话 ico_path也别写)
  4. ico_path 是生成的exe文件图标位置
  5. 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

  1. 存在中文路径
  2. 使用pyinstaller时使用了-w命令与print冲突

5.3、反复运行本身

因为你开了进程,需要在main后面添加一句

multiprocessing.freeze_support()           

复制

5.4、Pyinstaller打包selenium去除chromedriver黑框问题

我的目录是

C:\Users\45906\AppData\Local\Programs\Python\Python37\Lib\site-packages\selenium\webdriver\common\service.py           

复制

将其文件中的75行修改

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