pyinstaller打包tensorflow文件

安装

pyinstaller的安装环境一定要与tensorflow的安装环境一致。

我的tensorflow安装在anaconda创建的一个的环境中,该环境名为tensorflow3.5,因此pyinstaller要安装于此,在cmd中输入如下,即可安装成功

activate tensorflow3.5
pip install pyinstaller

使用

例如我的要打包inference_RS_CMD.py文件,那么最好将它依赖的其他py文件都放在同一目录下,如下:

在该文件夹进入cmd,激活环境,并输入:

pyinstaller -F inference_RS_CMD.py 

可能出现的问题及解决方案

1.出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position

     解决方案:预先在cmd中输入 :chcp 65001  #使用code - 8

2.出现Cannot find existing PyQt5 plugin directories,往往后面还会跟有一个路径,我遇见的是C:\qt5b\qt_1524647842210\_h_env\Library\plugins或者C:\bob\Qt-opensource\5.11.1\msvc2017_64\plugins

     解决方案:

   (1)首先看自己的环境中有没有安装qyqt5,若安装有qyqt5,那就根据它的提示在c盘中新建这个路径,把qyqt5(python的Lib/site-package路径)中的内容都拷贝到该路径中。

   (2)如果(1)不行并且你的代码中没有用到pyqt5,就卸载pyqt5,亲测有效。(卸载pyqt5后spyder可能打不开...)

   (3)如果你的代码用到pyqt5,百度吧

3.出现"RecursionError: maximum recursion depth exceeded"

     解决方案:在.spec文件中添加

    import sys
    sys.setrecursionlimit(5000)

    之后直接在cmd中运行:

pyinstaller xxx.spec

返回exe文件的路径

在写python程序中,有可能需要获取当前运行脚本的路径。打包成exe的脚本和直接运行地脚本在获取路径上稍微有点不同。

获取exe绝对路径的代码为:

import os
import sys
if getattr(sys, 'frozen', False):  #如果是exe文件
        root = os.path.dirname(sys.executable)
elif __file__:
        root = os.path.dirname(__file__)

猜你喜欢

转载自blog.csdn.net/Mr_health/article/details/82669463