python性能優(yōu)化和pyinstaller使用

Python 性能優(yōu)化常見技巧有:改進(jìn)算法選擇合適的數(shù)據(jù)結(jié)構(gòu);并行編程;減少冗余數(shù)據(jù);循環(huán)優(yōu)化;字符串的優(yōu)化;函數(shù)和模塊優(yōu)化;表達(dá)式優(yōu)化等。根據(jù) 80/20 原則,實(shí)現(xiàn)程序的重構(gòu)、優(yōu)化、擴(kuò)展以及文檔相關(guān)的事情通常需要消耗 80% 的工作量。
Python應(yīng)用也有許多優(yōu)化措施,如使用異步、理解性能測(cè)試工具,以及使用多解釋器等。

性能分析主要分為兩類:基于事件的性能分析(event-based profiling)和統(tǒng)計(jì)式的性能分析(statistical profiling)。優(yōu)化通常包含兩方面的內(nèi)容:減小代碼的體積,提高代碼的運(yùn)行效率。Python中最常用的性能分析工具主要有:cProfiler, line_profiler以及memory_profiler等。

使用python時(shí),要養(yǎng)成使用 os.path.join 的習(xí)慣

pipenv shell
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple joblib
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple MarkupSafe
pyinstaller -D -w kyj050.py

對(duì)應(yīng)依賴比較多的程序,建議使用 -D, -F更適合單文件的py腳本

pyinstaller -D -w predict_rec.py

RecursionError: maximum recursion depth exceeded
在*.spec文件中增加兩行
import sys
sys.setrecursionlimit(100000)

pyinstaller predict_rec.spec
pyinstaller recc.spec

Pyinstaller UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position解決方案
chcp 65001
pyinstaller rec_f.spec

OCR虛擬環(huán)境打包腳本
pipenv --three
pipenv shell
pip install D:\program\python\paddlepaddle-1.7.2-cp37-cp37m-win_amd64.whl
pip install flask
pip install tornado

網(wǎng)絡(luò)好的話可以跳過以下兩步
pip install D:\program\python\opencv_python-4.4.0-cp37-cp37m-win_amd64.whl
pip install D:\program\python\matplotlib-3.3.2-cp37-cp37m-win_amd64.whl
pip install D:\program\python\PyQt5-5.15.1-5.15.1-cp35.cp36.cp37.cp38.cp39-none-win_amd64.whl
pip install matplotlib==3.3.0
AttributeError: module 'sip' has no attribute 'setapi'
pip install --upgrade matplotlib
pip install pyqt5
python -m pip install pypiwin32

pip uninstall matplotlib
pip uninstall pyqt5

WinXP虛擬環(huán)境安裝
pipenv --three
pipenv shell
pip install D:\program\python\paddlepaddle-1.5.2-cp37-cp37m-win_amd64.whl
pip install D:\program\python\paddlepaddle-1.6.3-cp37-cp37m-win_amd64.whl

網(wǎng)絡(luò)好的話可以跳過以下兩步
pip install D:\program\python\opencv_python-4.4.0-cp37-cp37m-win_amd64.whl
pip install D:\program\python\matplotlib-3.3.2-cp37-cp37m-win_amd64.whl
pip install flask
pip install D:\program\python\numpy-1.19.3-cp37-cp37m-win_amd64.whl

ImportError: DLL load failed: 動(dòng)態(tài)鏈接庫(kù)(DLL)初始化例程失敗

  1. 卸載目前安裝的tensorflow:pip uninstall tensorflow
  2. 安裝舊版本的tensorflow:pip install --ignore-installed --upgrade tensorflow==1.5

-c的作用是返回錯(cuò)誤信息!如果有報(bào)錯(cuò)的話,將在控制臺(tái)顯示。這里,要做好截圖的準(zhǔn)備,因?yàn)榭刂婆_(tái)報(bào)錯(cuò)后是一閃而過的,如下。

pyinstaller -p C:\Users\PL-RD.virtualenvs\no_server-NBCSumUj\Lib\site-packages -D -c rec.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\no_server-NBCSumUj\Lib\site-packages -F -c rec.py

pyinstaller -p C:\Users\PL-RD.virtualenvs\env-A89Qz4pK\Lib\site-packages -D -c rec.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\env-A89Qz4pK\Lib\site-packages -F -c rec.py

pyinstaller -p C:\Users\PL-RD.virtualenvs\pack_exe-NbvpC7L7\Lib\site-packages -D -c api.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\pack_exe-NbvpC7L7\Lib\site-packages -F -c api.py
pyinstaller api.py --hidden-import pkg_resources

pyinstaller -p C:\Users\PL-RD.virtualenvs\pack_exe-NbvpC7L7\Lib\site-packages -D -c rec.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\pack_exe-NbvpC7L7\Lib\site-packages -F -c rec_f.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\model_api-drqWMESO\Lib\site-packages -F -w rec_f.py

get all libraries used in the script from Lib\site-packages to your application directory will work

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容