基于Python的ERA-5多线程下载分为两部分,《基于Python的ERA-5多线程下载 (1)》,直接再本地使用,即写代码的人使用;《基于Python的ERA-5多线程下载 (2)》使用 pyinsaller 将写好的程序,打包成 .exe 文件,以便其他人使用。
一、正文
1.安装 pyinstaller
开始菜单—Anaconda3 (64-bit)—Anaconda Prompt (Anaconda3),执行以下代码,安装 pyinstaller:
pip install pyinstaller
2.代码路径修改为相对路径。
将代码中保存的路径,设置为相对路径;程序将在创建子目录,将下载程序下载至此处。
3.打包 .py 文件。
- cd 至 ERA5_Download_Edit.py文件所在目录
- 运行 pyinnstall -F ERA5_Download_Edit.py
补充:
pyinnstall ERA5_Download_Edit.py,这样也可以打包为 .exe 文件,但是会产生很多依赖包
pyinnstall -F ERA5_Download_Edit.py 将所有的所有依赖程序压缩到.exe文件中,不会产生依赖程序
pyinnstall -F -i ERA5_Download_Edit.py 使用 -i 可以为程序链接图标
4.出现问题,根据提示,修改 .spec 文件
提示:
=============================================================
A RecursionError (maximum recursion depth exceeded) occurred.
For working around please follow these instructions
=============================================================
1. In your program's .spec file add this line near the top::
import sys ; sys.setrecursionlimit(sys.getrecursionlimit() * 5)
2. Build your program by running PyInstaller with the .spec file as
argument::
pyinstaller myprog.spec
3. If this fails, you most probably hit an endless recursion in
PyInstaller. Please try to track this down has far as possible,
create a minimal example so we can reproduce and open an issue at
https://github.com/pyinstaller/pyinstaller/issues following the
instructions in the issue template. Many thanks.
Explanation: Python's stack-limit is a safety-belt against endless recursion,
eating up memory. PyInstaller imports modules recursively. If the structure
how modules are imported within your program is awkward, this leads to the
nesting being too deep and hitting Python's stack-limit.
With the default recursion limit (1000), the recursion error occurs at about
115 nested imported, with limit 2000 at about 240, with limit 5000 at about
660.
根据提示,修改 .spec 文件。
修改前
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['ERA5_Download_Edit.py'],
pathex=['E:\\python\\python3_64b\\ERA-5\\ERA5_Download_Edit_Installer'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
修改后
# -*- mode: python ; coding: utf-8 -*-
import sys
sys.setrecursionlimit(20000)
block_cipher = None
a = Analysis(['ERA5_Download_Edit.py'],
pathex=['E:\\python\\python3_64b\\ERA-5\\ERA5_Download_Edit_Installer'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
5.继续打包
在命令行(Anaconda Prompt (Anaconda3))中输入 :
此次运行的是 .spec 文件,而不是 .py 文件。
pyinstaller -i E:\python\python3_64b\ERA-5\ERA5_Download_Edit_Installer\myico.ico -F ERA5_Download_Edit.spec
直到运行结束,如图:
6.运行程序
双击打开:ERA5_Download_Edit.exe,运行程序,创建子目录开始下载。
二、结语
第一次自己做出来的.exe文件,并且能够正常运行,心里还是蛮高兴的!!!一起加油。