基本命令
pip install pyinstaller
cmd里面進(jìn)入jobs2.py所在的文件夾需忿。
pyinstaller -w -F --add-data "templates;templates" --add-data "static;static" jobs2.py
上面的-w選項(xiàng)是去掉運(yùn)行時的cmd窗口藏斩,--add-data選項(xiàng)是打包非python代碼文件進(jìn)去, -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
網(wǎng)上說法:
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.
|
坑: 安裝的時候出現(xiàn)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.
方案: 使用虛擬環(huán)境來安裝
D:\GoogleDrive\2_2017Dev\Py\yescript\runcodedesign>activate base
(base) D:\GoogleDrive\2_2017Dev\Py\yescript\runcodedesign>pip install PyQt5
網(wǎng)上大神的說法:
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]
坑: 運(yùn)行jobs.exe 報TemplateNotFound錯誤
這一類問題都是找不到所需要的文件。一般都需要把文件附帶上去,同時重新設(shè)計相對路徑困肩。參考說明
pyinstaller -w -F --add-data "templates;templates" --add-data "static;static" jobs.py
方案:需要把下面的邏輯加上去整陌。sys._MEIPASS是exe在運(yùn)行時候的目錄拗窃。
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打包之后不停執(zhí)行,導(dǎo)致內(nèi)存溢出
解決方案:
if __name__ == '__main__':
multiprocessing.freeze_support()
# my code
網(wǎng)上大神:
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