How to fix Python skyfield FileNotFoundError: [Errno 2] No such file or directory: 'de421.bsp'
Problem:
When trying to use the Python skyfield
library, you see an exception like
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'
Solution
Take a look at the api.load(...)
line in your code:
ephem = api.load_file('de421.bsp')
It tries to load the data from the file de421.bsp
in the current directory. This file contains positional data of objects in the sky and you need to manually download that file.
You can download the file from NASA. Just take care to either place it into the right directory or modifying the path in the api.load()
call to point to the file.
URL for downloading the file:
https://ssd.jpl.nasa.gov/ftp/eph/planets/bsp/de421.bsp
My preferred way to download it is using wget
:
wget https://ssd.jpl.nasa.gov/ftp/eph/planets/bsp/de421.bsp
This command will place the file into the current directory.