pyinstaller踩坑记

基本命令

pip install pyinstaller
cmd里面进入jobs2.py所在的文件夹。
pyinstaller -w -F --add-data "templates;templates" --add-data "static;static" jobs2.py
上面的-w选项是去掉运行时的cmd窗口,--add-data选项是打包非python代码文件进去, -F代表编译为单个exe文件。

坑: PyQt5报错

Exception:

        Cannot find existing PyQt5 plugin directories

        Paths checked: C:/Miniconda3/conda-bld/qt_1535195524645/_h_env/Library/plugins

方案: pip install PyQt5
网上说法:
Unfortunately, conda's version of PyQt5 is broken -- it returns invalid paths when querying QLibraryInfo. The pip-installed version would work fine.
@roynielsen17, you're correct that Python 2.7 is supported until 2020; Pyinstaller will support Python 2.7 until that date as well. However, PyQt5 doesn't support Python 2.7 -- that's the core roadblock.

|

坑: 安装的时候出现permission error

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: 'd:\programdata\anaconda3\Lib\site-packages\PyQt5\Qt\bin\d3dcompiler_47.dll'
Consider using the --user option or check the permissions.
方案: 使用虚拟环境来安装
D:\GoogleDrive\2_2017Dev\Py\yescript\runcodedesign>activate base
(base) D:\GoogleDrive\2_2017Dev\Py\yescript\runcodedesign>pip install PyQt5

网上大神的说法:
You are trying to install the package to a system folder which you don't have permissions to write to.
You have three options(use only one of them):
1-setup a virtual env to install the package (recommended):
python3 -m venv env
source ./env/bin/activate
python -m pip install google-assistant-sdk[samples]
2-Install the package to the user folder:
python -m pip install --user google-assistant-sdk[samples]
3-use sudo to install to the system folder (not recommended)
sudo python -m pip install google-assistant-sdk[samples]

坑: 运行jobs.exe 报TemplateNotFound错误

这一类问题都是找不到所需要的文件。一般都需要把文件附带上去,同时重新设计相对路径。参考说明
pyinstaller -w -F --add-data "templates;templates" --add-data "static;static" jobs.py
方案:需要把下面的逻辑加上去。sys._MEIPASS是exe在运行时候的目录。

if getattr(sys, 'frozen', False):
  template_folder = os.path.join(sys._MEIPASS, 'templates') #sys._MEIPASS is a temporary folder for PyInstaller
  app = Flask(__name__, template_folder=template_folder)
else:
  app = Flask(__name__)

坑: mutiprocess打包之后不停执行,导致内存溢出

解决方案:

if __name__ == '__main__':
    multiprocessing.freeze_support()
    # my code

网上大神:
The reason is lack of fork() on Windows (which is not entirely true). Because of this, on Windows the fork is simulated by creating a new process in which code, which on Linux is being run in child process, is being run. As the code is to be run in technically unrelated process, it has to be delivered there before it can be run. The way it's being delivered is first it's being pickled and then sent through the pipe from the original process to the new one. In addition this new process is being informed it has to run the code passed by pipe, by passing --multiprocessing-fork command line argument to it. If you take a look at implementation of freeze_support() function its task is to check if the process it's being run in is supposed to run code passed by pipe or not.

坑: 引入six的时候会报错

升级setuptools
pip install --upgrade setuptools

image.png

你可能感兴趣的:(pyinstaller踩坑记)