This blog is about the basic use of pyinstaller on windows and basic pitfall avoidance
There is a problem when using the pyinstaller tool to package in windows, you will see this warning message in the package list:
: 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 ''!
Just ignore this kind of information.
I. Basic use
1. Install pyinstall
# pip install pyinstaller
2. Finding the documents needed for the program
# Make .spec files # Enter the project directory, execute the command: (there are other parameters: -F, etc., it is recommended to use -D) # -D will generate a folder in the current directory in the dist directory, which is convenient when dealing with static files # pyi-makespec -D
3、Generate .exe file
# Execute in a sibling directory # pyinstaller
4. Enter the dist directory and run the project
# generated exe executable runserver --noreload # runserver --noreload
II. Basic error handling
1, when running exe after the prompt: No module named XXX
Reason: The reason for this situation is mainly due to the fact that some Django mods will not be collected automatically and need to be added manually.
Solution: Open the generated file with the extension .spec and add the module that is not in the error in hiddenimports.
2、When running an error is reported:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte
Reason: mainly windows system gbk encoding problem
Solution: Open the error file above the error message and jump to the error line to modify with open(), add: encoding='utf-8' in it.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 890, in _bootstrap
File "", line 936, in _bootstrap_inner
File "", line 167, in format_exc
File "", line 121, in format_exception
File "", line 521, in __init__
File "", line 533, in _load_lines
File "", line 533, in _load_lines
File "", line 533, in _load_lines
[Previous line repeated 2 more times]
File "", line 531, in _load_lines
File "", line 285, in line
File "", line 16, in getline
File "", line 47, in getlines
File "", 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
The above is an example of error reporting, find the "PyInstaller\loader\pyimod03_importers.py" file, open and compile line 299 to find the corresponding location to add: encoding='utf-8' (Note: modify the backup before the backup, so as to avoid misuse can not be found back)
3、When running this error: TemplateDoesNotExist at /index/
Reason: TemplateDoesNotExist This is because the templates file was not found.
Solution: Add the templates file to the corresponding path according to the error prompt and refresh it.
TemplateDoesNotExist at /index/
index/
Request Method: GET
Request URL: http://127.0.0.1:8000/index/
Django Version: 3.2.9
Exception Type: TemplateDoesNotExist
Exception Value:
index/
Exception Location: django\template\, line 19, in get_template
Python Executable: F:\Workspoace\PyWork\bookstore\dist\
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:
: C:\Users\ja\AppData\Local\Temp\_MEI25882\templates\index\ (Source does not exist)
.app_directories.Loader: C:\Users\ja\AppData\Local\Temp\_MEI25882\django\contrib\admin\templates\index\ (Source does not exist)
.app_directories.Loader: C:\Users\ja\AppData\Local\Temp\_MEI25882\django\contrib\auth\templates\index\ (Source does not exist)
In the above example, copy the template folder and put it under C:\Users\ja\AppData\Local\Temp_MEI25882\.
4, the project lacks style css and js
Reason: Pyinstaller can find templates (html files) but not css and js files.
Solution:
Configure django static file collection in settings
# STATIC_ROOT = (BASE_DIR, 'Folder path')
Static file collection commands
# python collectstatic
Then add it in the url of each app:
# (settings.STATIC_URL, document_root=settings.STATIC_ROOT) # This phrase means that it will beSTATIC_ROOTMake a copy of the static files in the directory to the web page STATIC_URLfollowing the path
Modify datas in the .spec file to configure static file packaging:
# F:\Workspoace\PyWork\bookstore\statics Addresses of css and js static files to be packaged Corresponding to the location in the dist. # F:\Workspoace\PyWork\bookstore\templates Address of the html file template to be packaged Corresponding to the location in the dist. # datas=[(r'F:\Workspoace\PyWork\bookstore\statics',r'.\statics'), (r'F:\Workspoace\PyWork\bookstore\templates', r'.\templates')],
Note: Here configure template packaging above the 3rd file migration do not need to do, here synchronized packaging.
Another small problem here is in the django configuration file settings:
# STATICFILES_DIRS = [ # (BASE_DIR, "statics"), # ] STATIC_ROOT = (BASE_DIR, 'statics')
STATICFILES_DIRS and STATIC_ROOT can't be used at the same time, if you configure STATICFILES_DIRS you need to comment it out, otherwise it will report an error.
To this point this article on the use of Pyinstaller packaging tools and avoid the pit of the article is introduced to this, more related Pyinstaller packaging tools content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!