Pyinstaller打包工具的使用以及避坑
本篇博客主要介紹的是pyinstaller在windows下的基本使用和基礎(chǔ)避坑
在windows中使用pyinstaller工具打包時(shí)會(huì)出現(xiàn)一個(gè)問題,在打包列表會(huì)看到這樣的警告信息:
django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal302", "gdal301", "gdal300", "gdal204", "gdal203", "gdal202", "gdal201", "gdal20"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.collect_submodules: failed to import 'django.contrib.gis.sitemaps'!
這種信息不予理會(huì)就好了。
一、基本使用
1、安裝pyinstall
# pip install pyinstaller
2、查找程序需要的文件
# 制作 .spec 文件 # 進(jìn)入項(xiàng)目目錄,執(zhí)行命令:(還有其它參數(shù):-F等, 建議使用-D) # -D會(huì)在當(dāng)前目錄下的dist目錄中生成文件夾,處理靜態(tài)文件時(shí)比較方便 # pyi-makespec -D manage.py
3、生成.exe文件
# 在manage.spec 同級目錄執(zhí)行 # pyinstaller manage.spec
4、進(jìn)入dist目錄運(yùn)行項(xiàng)目
# 生成的exe可執(zhí)行文件 runserver --noreload # manage.exe runserver --noreload
二、基本錯(cuò)誤處理
1、當(dāng)運(yùn)行exe后出現(xiàn)提示:No module named XXX
出現(xiàn)原因:出現(xiàn)這種情況的原因主要是由于Django有些module不會(huì)自動(dòng)收集,需要手動(dòng)添加
解決辦法:打開生成的后綴名為.spec的文件,在hiddenimports中添加報(bào)錯(cuò)中沒有的模塊
2、當(dāng)運(yùn)行出現(xiàn)報(bào)錯(cuò):UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte
出現(xiàn)原因:主要是windows系統(tǒng)下gbk編碼的問題
解決辦法:打開報(bào)錯(cuò)信息上面一行提示的錯(cuò)誤文件并跳轉(zhuǎn)到提示的錯(cuò)誤行數(shù)上修改with open(),在里面添加:encoding='utf-8' 即可
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "threading.py", line 890, in _bootstrap
File "threading.py", line 936, in _bootstrap_inner
File "traceback.py", line 167, in format_exc
File "traceback.py", line 121, in format_exception
File "traceback.py", line 521, in __init__
File "traceback.py", line 533, in _load_lines
File "traceback.py", line 533, in _load_lines
File "traceback.py", line 533, in _load_lines
[Previous line repeated 2 more times]
File "traceback.py", line 531, in _load_lines
File "traceback.py", line 285, in line
File "linecache.py", line 16, in getline
File "linecache.py", line 47, in getlines
File "linecache.py", line 103, in updatecache
File "PyInstaller\loader\pyimod03_importers.py", line 299, in get_source
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 11211: illegal multibyte sequence
上面是報(bào)錯(cuò)示例,找到"PyInstaller\loader\pyimod03_importers.py"文件,打開并編譯第299行找到對應(yīng)位置添加:encoding='utf-8'(注:修改前先備份好備份,以免誤操作找不回)
3、當(dāng)運(yùn)行出現(xiàn)這種報(bào)錯(cuò):TemplateDoesNotExist at /index/
出現(xiàn)原因:TemplateDoesNotExist 這個(gè)是因?yàn)闆]有找到templates文件
解決辦法:根據(jù)錯(cuò)誤提示將templates文件添加至對應(yīng)的路徑下,刷新即可。
TemplateDoesNotExist at /index/
index/index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/index/
Django Version: 3.2.9
Exception Type: TemplateDoesNotExist
Exception Value:
index/index.html
Exception Location: django\template\loader.py, line 19, in get_template
Python Executable: F:\Workspoace\PyWork\bookstore\dist\manage.exe
Python Version: 3.7.8
Python Path:
['C:\\Users\\ja\\AppData\\Local\\Temp\\_MEI25882\\base_library.zip',
'C:\\Users\\ja\\AppData\\Local\\Temp\\_MEI25882\\lib-dynload',
'C:\\Users\\ja\\AppData\\Local\\Temp\\_MEI25882']
Server time: Tue, 16 Nov 2021 03:13:35 +0000
Template-loader postmortem
Django tried loading these templates, in this order:Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\ja\AppData\Local\Temp\_MEI25882\templates\index\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\ja\AppData\Local\Temp\_MEI25882\django\contrib\admin\templates\index\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\ja\AppData\Local\Temp\_MEI25882\django\contrib\auth\templates\index\index.html (Source does not exist)
上面這種示例把template文件夾復(fù)制下來放到C:\Users\ja\AppData\Local\Temp_MEI25882\下面即可
4、項(xiàng)目缺少樣式css和js
出現(xiàn)原因:Pyinstaller 能找到templates(html files文件),但不能找到css和js文件
解決辦法:
在settings中配置django靜態(tài)文件收集
# STATIC_ROOT = os.path.join(BASE_DIR, '文件夾路徑')
靜態(tài)文件收集命令
# python manage.py collectstatic
然后在各個(gè)app的url中添加:
# static.static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # 這句話的意思就是將STATIC_ROOT目錄的靜態(tài)文件復(fù)制一份到網(wǎng)頁 STATIC_URL路徑下
在.spec文件中修改datas,配置靜態(tài)文件打包:
# F:\Workspoace\PyWork\bookstore\statics 要打包的css,js靜態(tài)文件地址 相對應(yīng)打包到dist中的位置 # F:\Workspoace\PyWork\bookstore\templates 要打包的html文件模板地址 相對應(yīng)打包到dist中的位置 # datas=[(r'F:\Workspoace\PyWork\bookstore\statics',r'.\statics'), (r'F:\Workspoace\PyWork\bookstore\templates', r'.\templates')],
注:這里配置template打包上面的第3條文件遷移就不需要做了,這里同步打包了。
這里還存在一個(gè)小問題就是django的配置文件settings中:
# STATICFILES_DIRS = [ # os.path.join(BASE_DIR, "statics"), # ] STATIC_ROOT = os.path.join(BASE_DIR, 'statics')
STATICFILES_DIRS和STATIC_ROOT不能同時(shí)使用,如果配置了STATICFILES_DIRS需要注釋掉,不然會(huì)報(bào)錯(cuò)。
到此這篇關(guān)于Pyinstaller打包工具的使用以及避坑的文章就介紹到這了,更多相關(guān)Pyinstaller打包工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV實(shí)現(xiàn)從灰度圖像切出Mask前景區(qū)域
本文主要介紹了如何利用OpenCV實(shí)現(xiàn)從灰度圖像,根據(jù)閾值,切出多個(gè)前景區(qū)域,過濾面積太小的圖像。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06python中sort和sorted排序的實(shí)例方法
在本篇文章中小編給大家?guī)淼氖顷P(guān)于python中sort和sorted排序的實(shí)例方法以及相關(guān)知識(shí)點(diǎn),有需要的朋友們可以學(xué)習(xí)下。2019-08-08Python利用scikit-learn實(shí)現(xiàn)近鄰算法分類的示例詳解
scikit-learn已經(jīng)封裝好很多數(shù)據(jù)挖掘的算法,這篇文章就來用scikit-learn實(shí)現(xiàn)近鄰算法分類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02Python學(xué)習(xí)小技巧之列表項(xiàng)的拼接
這篇文章主要給大家介紹了Python學(xué)習(xí)小技巧之列表項(xiàng)的拼接的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05Python 實(shí)現(xiàn)使用空值進(jìn)行賦值 None
這篇文章主要介紹了Python 實(shí)現(xiàn)使用空值進(jìn)行賦值 None,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03