Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic usage of pyinstaller under windows?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the basic use of pyinstaller under windows". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the basic use of pyinstaller in windows"?

There is a problem when packaging with the pyinstaller tool in windows, and you will see a warning message like this in the package 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.sitemapsgiving!

It would be nice to ignore this kind of information.

I. basic use

1. Install pyinstall

# pip install pyinstaller

2. Find the files needed by the program

# make .spec file # enter the project directory and execute the command: (there are other parameters:-F, etc., it is recommended to use-D) #-D will generate a folder in the dist directory under the current directory, which is more convenient when dealing with static files # pyi-makespec-D manage.py

3. Generate .exe file

# execute # pyinstaller manage.spec in the manage.spec sibling directory

4. Enter the dist directory to run the project

# generated exe executable runserver-- noreload# manage.exe runserver-- noreload2, basic error handling 1, prompt when running exe: No module named XXX

Cause of occurrence: this is mainly due to the fact that some module in Django will not be collected automatically and need to be added manually

Solution: open the generated file with the suffix .spec and add modules in hiddenimports that are not in the error report.

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

The reason: it is mainly the problem of gbk coding in windows system.

Solution: open the error file indicated on the above line of the error message and jump to the number of error lines prompted to modify with open (), and add: encoding='utf-8' to 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 example of an error report. Find the "PyInstaller\ loader\ pyimod03_importers.py" file, open and compile line 299 and find the corresponding location to add: encoding='utf-8' (Note: back up the backup before modification, in case you can't find it by mistake)

3. When this error occurs in the 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)

In the above example, copy down the template folder and put it under C:\ Users\ ja\ AppData\ Local\ Temp_MEI25882\.

4. The project lacks styles css and js

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

Solution:

Configure 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 to the url of each app:

# static.static (settings.STATIC_URL, document_root=settings.STATIC_ROOT) # means to copy a static file from the STATIC_ROOT directory to the STATIC_URL path of the web page

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

# F:\ Workspoace\ PyWork\ bookstore\ statics the css,js static file address to be packaged corresponds to the location packaged in dist # F:\ Workspoace\ PyWork\ bookstore\ templates the html file template address to be packaged corresponds to the location packaged in dist # datas= [(ringing F:\ Workspoace\ PyWork\ bookstore\ statics',r'.\ statics'), (ringing F:\ Workspoace\ PyWork\ bookstore\ templates', ritual.\ templates')]

Note: there is no need to do the migration of the third file above the template package is configured here, it is packaged synchronously.

There is also a small problem, 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.

Thank you for your reading, the above is the content of "what is the basic use of pyinstaller under windows". After the study of this article, I believe you have a deeper understanding of what is the basic use of pyinstaller under windows. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report