pyinstaller在windows下的基本使用方法是什么

发布时间:2021-11-22 17:07:35 作者:iii
来源:亿速云 阅读:385

这篇文章主要讲解了“pyinstaller在windows下的基本使用方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“pyinstaller在windows下的基本使用方法是什么”吧!

在windows中使用pyinstaller工具打包时会出现一个问题,在打包列表会看到这样的警告信息:

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

这种信息不予理会就好了。

一、基本使用

1、安装pyinstall

# pip install pyinstaller

2、查找程序需要的文件

# 制作 .spec 文件
# 进入项目目录,执行命令:(还有其它参数:-F等, 建议使用-D)
# -D会在当前目录下的dist目录中生成文件夹,处理静态文件时比较方便
# pyi-makespec -D manage.py

3、生成.exe文件

# 在manage.spec 同级目录执行
# pyinstaller manage.spec

4、进入dist目录运行项目

# 生成的exe可执行文件 runserver --noreload
# manage.exe runserver --noreload

二、基本错误处理

1、当运行exe后出现提示:No module named XXX

出现原因:出现这种情况的原因主要是由于Django有些module不会自动收集,需要手动添加

解决办法:打开生成的后缀名为.spec的文件,在hiddenimports中添加报错中没有的模块

2、当运行出现报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte

出现原因:主要是windows系统下gbk编码的问题

解决办法:打开报错信息上面一行提示的错误文件并跳转到提示的错误行数上修改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

上面是报错示例,找到"PyInstaller\loader\pyimod03_importers.py"文件,打开并编译第299行找到对应位置添加:encoding='utf-8'(注:修改前先备份好备份,以免误操作找不回)

3、当运行出现这种报错:TemplateDoesNotExist at /index/

出现原因:TemplateDoesNotExist 这个是因为没有找到templates文件

解决办法:根据错误提示将templates文件添加至对应的路径下,刷新即可。

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文件夹复制下来放到C:\Users\ja\AppData\Local\Temp_MEI25882\下面即可

4、项目缺少样式css和js

出现原因:Pyinstaller 能找到templates(html files文件),但不能找到css和js文件

解决办法:

在settings中配置django静态文件收集

# STATIC_ROOT = os.path.join(BASE_DIR, '文件夹路径')

静态文件收集命令

# python manage.py collectstatic

然后在各个app的url中添加:

# static.static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# 这句话的意思就是将STATIC_ROOT目录的静态文件复制一份到网页 STATIC_URL路径下

在.spec文件中修改datas,配置静态文件打包:

# F:\Workspoace\PyWork\bookstore\statics 要打包的css,js静态文件地址 相对应打包到dist中的位置
# F:\Workspoace\PyWork\bookstore\templates 要打包的html文件模板地址 相对应打包到dist中的位置
# datas=[(r'F:\Workspoace\PyWork\bookstore\statics',r'.\statics'), (r'F:\Workspoace\PyWork\bookstore\templates', r'.\templates')],

注:这里配置template打包上面的第3条文件迁移就不需要做了,这里同步打包了。

这里还存在一个小问题就是django的配置文件settings中:

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

STATICFILES_DIRS和STATIC_ROOT不能同时使用,如果配置了STATICFILES_DIRS需要注释掉,不然会报错。

感谢各位的阅读,以上就是“pyinstaller在windows下的基本使用方法是什么”的内容了,经过本文的学习后,相信大家对pyinstaller在windows下的基本使用方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 在windows下安装redis
  2. nginx在windows下安装

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pyinstaller windows

上一篇:react-scripts是什么

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》