Use of Pyinstaller Packaging Tools and Pit Avoidance

  • 2021-12-13 08:40:45
  • OfStack

Directory 1. Basic Usage 2. Basic Error Handling 1. Prompt when running exe: No module named XXX2. Error report when running: UnicodeDecodeError: 'gbk' codec can 't decode byte 0x80 in position 658: illegal multibyte3. Error report when running: TemplateDoesNotExist at/index/4. Project missing styles css and js

This blog mainly introduces the basic use and basic pit avoidance of pyinstaller under windows

There is a problem when packaging with the pyinstaller tool in windows, and you will see this warning message in the packaging list:

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'!

It would be nice to ignore this kind of information.

1. Basic use

1. Install pyinstall


# pip install pyinstaller

2. Find the files required by the program


#  Production  .spec  Documents 
#  Enter the project directory , Execute a command: ( There are other parameters: -F Etc ,  Recommended use -D)
# -D Will be in the current directory dist Directory generates folders, and it is convenient to deal with static files 
# pyi-makespec -D manage.py

3. Generate the. exe file


#  In manage.spec  Peer directory execution 
# pyinstaller manage.spec

4. Enter the dist directory to run the project


#  Generated exe Executable file  runserver --noreload
# manage.exe runserver --noreload

2. Basic error handling

1. When running exe, a prompt appears: No module named XXX

Reason: The main reason for this is that some module of Django will not be collected automatically and need to be added manually

Solution: Open the generated file with the suffix. spec, and add the module not in the error report in hiddenimports

2. When an error occurs in operation: UnicodeDecodeError: 'gbk' codec can 't decode byte 0x80 in position 658: illegal multibyte

The main reason is the problem of gbk coding in windows system

Solution: Open the error file prompted by 1 line above the error message and jump to the number of prompted error lines to modify with open (), and add: encoding= 'utf-8' in it

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

The above is an error reporting example. Find the file "PyInstaller\ loader\ pyimod03_importers. py", open and compile line 299, find the corresponding position, and add: encoding= 'utf-8' (Note: Back up the backup before modification, so as not to be found by misoperation)

3. When this error occurs in operation: TemplateDoesNotExist at/index/

Cause: 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/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)

The example above copies the template folder and places it under C:\ Users\ ja\ AppData\ Local\ Temp_MEI25882\

4. The project lacks styles css and js

Cause: Pyinstaller can find templates (html files file), but not css and js files

Solution:

Configuring django Static File Collection in settings


# STATIC_ROOT = os.path.join(BASE_DIR, ' Folder path ')

Static file collection command


# python manage.py collectstatic

Then add in the url of each app:


# static.static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
#  The meaning of this sentence is to put STATIC_ROOT Static file replication of directories 1 Share to the web page  STATIC_URL Under the path 

Modify datas in the. spec file to configure static file packaging:


# F:\Workspoace\PyWork\bookstore\statics  To be packed css , js Static file address   Correspondingly packaged to dist Position in 
# F:\Workspoace\PyWork\bookstore\templates  To be packed html File template address   Correspondingly packaged to dist Position in 
# datas=[(r'F:\Workspoace\PyWork\bookstore\statics',r'.\statics'), (r'F:\Workspoace\PyWork\bookstore\templates', r'.\templates')],

Note: Here configuration template packaging above the third file migration does not need to do, here synchronous packaging.

There is also a small problem here, which is in the configuration file settings of django:


# STATICFILES_DIRS = [
#     os.path.join(BASE_DIR, "statics"),
# ]
STATIC_ROOT = os.path.join(BASE_DIR, 'statics')

STATICFILES_DIRS and STATIC_ROOT cannot be used at the same time. If STATICFILES_DIRS is configured, it needs to be commented out, otherwise an error will be reported.


Related articles: