本篇博客主要介绍的是pyinstaller在windows下的基本使用和基础避坑

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

django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal302", "gdal30
1", "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

5、配置运行时浏览器自启动

在配置文件中修改为如下配置即可

"""
WSGI config for bookstore project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
""" import os
import time
import threading
import webbrowser from django.core.wsgi import get_wsgi_application def open_w():
time.sleep(1)
webbrowser.open_new_tab('http://127.0.0.1:8100') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookstore.settings") application = get_wsgi_application() t1 = threading.Thread(target=open_w)
t1.start()

二、基本错误处理

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打包工具的更多相关文章

  1. pyinstaller打包工具简单使用

    python脚本如果在没有安装python的机器上不能运行,所以将脚本打包成exe文件将可跨平台使用,那么怎么打包了,python提供了专门的模块:pyinstaller,下面就介绍下怎么用 1.安装 ...

  2. Pyinstaller 打包工具的使用!!!

    打包成一个文件夹: pyinstaller xxx.py 打包成单个文件: pyinstaller -F xxx.py 打包成不显示终端的单个文件: pyinstaller -F -w xxx.py ...

  3. pyinstaller 打包工具的使用方法

    pyinstaller的安装 下载后可以输入pip list查看是否安装成功 然后切换到项目的根目录输入 pyinstaller -i favicon.ico -w -c game.py -p Que ...

  4. $python打包工具pyinstaller的用法

    pyinstaller是一个很好用的python打包工具,在Windows环境下可以将python脚本打包成一个exe可执行文件,并且脚本中所依赖的各种第三方库在打包时候都会被统一处理到一起,这样打包 ...

  5. Python程序打包工具PyInstaller

    Python程序执行 py文件:直接提供源码,需要使用者自行安装Python并且安装依赖的各种库 pyc文件:pyc文件是Python解释器可以识别的二进制码,是跨平台的,需要使用者安装相应版本的Py ...

  6. python打包工具pyinstaller的使用

    安装PyInstaller pip install pyinstaller 安装完后,检查安装成功与否: pyinstaller --version 安装成功后,就可以使用下面的命令了: pyinst ...

  7. python3使用pyinstaller打包apscheduler出的错

    本来只是想用Python做一个定时任务小工具在服务器上运行,可是服务器在隔离区,各种禁止上外网,使用pip导出列表那种下载库的方法不管用,导致Python的各种库都下不到,官网离线下载又各种缺依赖,好 ...

  8. Pyinstaller打包Selenium脚本为exe文件执行问题

    由于同事辞职,许多运维工具的维护工作就交到我这里处理了,运维居然没人会Python脚本! 用Selenium编写的一个爬虫脚本cctv.py,需要给不懂软件的人运行.为了不让他去搭建,安装各种包,库, ...

  9. pyinstaller打包pyqt文件(转)

    pyinstaller打包pyqt文件  https://www.cnblogs.com/dcb3688/p/4211390.html   打包pyqt文件 如何将pyqt生成exe的二进制文件呢,p ...

  10. PyInstaller打包python脚本的一些心得

    PyInstaller打包python脚本的一些心得 因为在公司经常要帮同事做一个从excel表格中提取出需要的内容的重复工作,比较繁琐还容易出错:于是就想着要写个程序,但是同事又不可能在电脑上也装上 ...

随机推荐

  1. 使用Apache的ab工具进行压力测试

    Apache附带的ab工具(本机使用的PHP环境是WAMP集成环境,ab工具位于D:\wamp\bin\apache\Apache2.2.21\bin)非常容易使用,ab可以直接在Web服务器本地发起 ...

  2. JAVA并发编程学习笔记之CLH队列锁

    NUMA与SMP SMP(Symmetric Multi-Processor),即对称多处理器结构,指服务器中多个CPU对称工作,每个CPU访问内存地址所需时间相同.其主要特征是共享,包含对CPU,内 ...

  3. Acrobat Pro DC 2024.005 像word一样编辑PDF

    随着数字化的推广,PDF文件凭借其强大的优势和稳定性逐渐成为各类文档交流和存储的首选格式.随之而来的是对PDF文件的阅读.编辑.转换.转曲等各种操作需求的不断增长.因此,一款强大的PDF处理软件不仅需 ...

  4. Qt在linux下实现程序编译后版本号自增的脚本

    #! /bin/bash rm -rf temp.cpp num=0 while read line do if [ $num -eq 3 ];then array=(`echo $line | tr ...

  5. 鸿蒙UI开发快速入门 —— part06:组件状态管理之@State装饰器

    1.说在前面的话 在前五个章节中,我们构建的页面基本都是静态的页面,如果希望构建一个动态的.有交互的界面,就需要引入"状态"的概念,以便随着用户的交互,界面随着发生变化,例如如下的 ...

  6. vscode使用github

    1, vscode打开terminal,生成RSA密钥,并查看蜜月 PS D:\code\SQL> git init Reinitialized existing Git repository ...

  7. 埃尼阿克ENIAC与计算机发展,及信息技术发展史

    一.埃尼阿克ENIAC 第二次世界大战期间,国军方为了研发新型的大炮和导弹,设立了"弹道研究实验室".实验室为了计算炮弹弹道,用了200多人加班加点进行计算,速度依感无法达到军方要 ...

  8. .NET 服务发现

    .NET 服务发现 https://learn.microsoft.com/en-us/dotnet/core/extensions/service-discovery?tabs=dotnet-cli ...

  9. Git commit - Angular Convention

    使用 Git 的开发者会使用 git commit 进行代码提交,也会使用 -m 提交commit message.对于一些个人开发者,也许他们会觉得"这是我个人的项目,不用太在意git c ...

  10. okhttp3设置代理(http/https)

    最近项目网络请求需要设置代理,记录一下.http和https都可以. OkHttpClient.Builder builder = new OkHttpClient.Builder(); //代理服务 ...