Python版本3.9、obspy版本1.4.1,pyinstaller版本6.9.0
打包的是这个代码,代码名称是test.py
import obspy
from obspy import read
# 读取一个地震波形数据文件(例如一个 miniSEED 文件)
st = read("https://examples.obspy.org/RJOB_061005_072159.ehz.new")
# 打印波形数据的信息
print(st)
# 选择一个特定的 Trace
tr = st[0]
# 打印 Trace 的一些基本信息
print(tr.stats)
# 对波形数据进行带通滤波(例如1到10 Hz)
tr.filter("bandpass", freqmin=1, freqmax=10)
# 绘制波形图
tr.plot()
# 保存处理后的波形数据为一个新文件
tr.write("filtered_waveform.mseed", format="MSEED")
执行命令打包
pyinstaller -F test.py
出现报错
C:\Users\Administrator\Desktop\test>test.exe
obspy\core\util\version.py:156: UserWarning: ObsPy could not determine its version number. Make sure it is properly installed. This for example happens when installing from a zip archive of the ObsPy repository which is not a supported way of installing ObsPy.
Traceback (most recent call last):
File "test.py", line 5, in <module>
st = read("https://examples.obspy.org/RJOB_061005_072159.ehz.new")
File "decorator.py", line 232, in fun
File "obspy\core\util\decorator.py", line 297, in _map_example_filename
File "obspy\core\stream.py", line 208, in read
File "obspy\core\util\base.py", line 645, in _generic_reader
File "decorator.py", line 232, in fun
File "obspy\core\util\decorator.py", line 208, in uncompress_file
File "obspy\core\stream.py", line 251, in _read
File "obspy\core\util\base.py", line 403, in _read_from_plugin
TypeError: Unknown format for file C:\Users\Administrator\AppData\Local\Temp\obspy-vxnsy1im.new
[17052] Failed to execute script 'test' due to unhandled exception!
解决方案:
一、需要打包的test.py中,新增几个模块的引用
import obspy
from obspy import read
import requests #新增
import matplotlib #新增
matplotlib.use('QtAgg') #新增,需要安装PyQt5
# 读取一个地震波形数据文件(例如一个 miniSEED 文件)
st = read("https://examples.obspy.org/RJOB_061005_072159.ehz.new")
# 打印波形数据的信息
print(st)
# 选择一个特定的 Trace
tr = st[0]
# 打印 Trace 的一些基本信息
print(tr.stats)
# 对波形数据进行带通滤波(例如1到10 Hz)
tr.filter("bandpass", freqmin=1, freqmax=10)
# 绘制波形图
tr.plot()
# 保存处理后的波形数据为一个新文件
tr.write("filtered_waveform.mseed", format="MSEED")
二、修改obspy模块的源码,路径在
自己安装的Python路径下\Lib\site-packages\obspy\imaging\cm.py
修改1:大概237行
directory = Path(inspect.getfile(
inspect.currentframe()))
#在上面这行下面添加这三句
import os
import obspy
directory = Path(os.path.dirname(obspy.__file__)+r'\imaging\cm.py')
修改2:大概283
cm_file_pattern = str(cm_file_pattern.parent.resolve()/"data" / "*.np[yz]")
#在上面这行下面添加这三句
import obspy
import os
cm_file_pattern = os.path.dirname(obspy.__file__)+r'\imaging\data\*.np[yz]'
三、执行pyinstaller命令的时候,会默认生成一个spec文件,通过记事本打开,编辑这个文件
需要添加的内容我在代码中已经标注了
import os
import obspy
dirname = os.path.dirname(os.path.dirname(obspy.__file__))
data_list = [(dirname+'\\'+x,x) for x in os.listdir(dirname) if '-info' in x] + [(dirname+'\\'+x,x) for x in os.listdir(dirname) if 'obspy' in x]
####添加上面这几句
a = Analysis(
['test.py'],
pathex=[],
binaries=[],
datas=data_list , ####这句要更改
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='test',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='test',
)
四、执行pyinstaller
pyinstaller test.spec
可以正常运行了