如何修复 Python skyfield FileNotFoundError: [Errno 2] No such file or directory: de421.bsp

问题:

尝试使用 Python skyfield 库时,你看到类似这样的异常

skyfield_de421_error.txt
Input In [2], in <cell line: 11>()
      8 from calendar import monthrange
     10 ts = api.load.timescale()
---> 11 ephem = api.load_file('de413.bsp')

File /usr/local/lib/python3.10/dist-packages/skyfield/iokit.py:412, in load_file(path)
    410 base, ext = os.path.splitext(path)
    411 if ext == '.bsp':
--> 412     return SpiceKernel(path)
    413 raise ValueError('unrecognized file extension: {}'.format(path))

File /usr/local/lib/python3.10/dist-packages/skyfield/jpllib.py:71, in SpiceKernel.__init__(self, path)
     69 self.path = path
     70 self.filename = os.path.basename(path)
---> 71 self.spk = SPK.open(path)
     72 self.segments = [SPICESegment(self, s) for s in self.spk.segments]
     73 self.codes = set(s.center for s in self.segments).union(
     74                  s.target for s in self.segments)

File /usr/local/lib/python3.10/dist-packages/jplephem/spk.py:49, in SPK.open(cls, path)
     46 @classmethod
     47 def open(cls, path):
     48     """Open the file at `path` and return an SPK instance."""
---> 49     return cls(DAF(open(path, 'rb')))

FileNotFoundError: [Errno 2] No such file or directory: 'de421.bsp'

解决方案

查看你代码中的 api.load(...) 行:

example_load_de421.py
ephem = api.load_file('de421.bsp')

它尝试从当前目录的 de421.bsp 文件加载数据。此文件包含天空中物体的位置数据,你需要手动下载该文件。

你可以从 NASA下载该文件。只需注意将其放入正确的目录或修改 api.load() 调用中的路径以指向该文件。

下载文件的 URL:

de421.bsp_url.txt
https://ssd.jpl.nasa.gov/ftp/eph/planets/bsp/de421.bsp

我首选的下载方式是使用 wget

download_de421_command.sh
wget https://ssd.jpl.nasa.gov/ftp/eph/planets/bsp/de421.bsp

此命令会将文件放入当前目录。


Check out similar posts by category: Python, Skyfield