Skyfield is an elegant Python library for high-precision astronomy calculations. It allows users to compute the positions of planets, satellites, stars, and other celestial bodies from any point on Earth or in space, at any moment in time. It leverages JPL ephemeris data to achieve high accuracy. It's actively maintained with regular releases, often several per year, reflecting ongoing development and bug fixes.
pip install skyfieldVerified import paths — ran on the pinned version, not inferred.
This quickstart calculates the astrometric position of Mars as seen from Greenwich, UK, on Christmas Day 2024. It demonstrates loading ephemeris data, defining an observer's location, specifying a time, and performing an observation to retrieve celestial coordinates.
Ensure internet connectivity and proper file permissions. For constrained environments, pre-download the necessary `.bsp` files and specify their path: `eph = load('/path/to/my_data_folder/de421.bsp')`.Access the attributes directly from the returned object (e.g., `astrometric = obs.radec(); ra = astrometric.ra`) or continue using sequence unpacking if only the main three values are needed. Avoid direct index access on the returned object.
Always construct `skyfield.timelib.Time` objects using methods from the `timescale` object (e.g., `ts.utc(year, month, day)`, `ts.utcfromtimestamp()`). If converting from a `datetime` object, use `ts.utcfromdatetime(my_datetime_obj)` and ensure `my_datetime_obj` is timezone-aware and in UTC if possible.
pip install skyfield
Convert your `datetime` object to a Skyfield `Time` object using `ts.from_datetime()` or by manually passing its components to `ts.utc()` or `ts.ut1()`. ```python from skyfield.api import load from datetime import datetime ts = load.timescale() dt_obj = datetime.utcnow() # Your datetime object skyfield_time = ts.from_datetime(dt_obj) # Correct way # or: skyfield_time = ts.utc(dt_obj.year, dt_obj.month, dt_obj.day, dt_obj.hour, dt_obj.minute, dt_obj.second) ```
Load a larger ephemeris file that includes the major planets, such as `de421.bsp`, `de422.bsp`, or `de430t.bsp`.
```python
from skyfield.api import load
planets = load('de421.bsp') # Load a larger ephemeris file
mars = planets['mars']
```Ensure that both TLE lines are exactly 69 characters long, including any trailing spaces, and that they conform precisely to the TLE format. Verify the source of your TLE data for correctness. ```python from skyfield.api import EarthSatellite, load ts = load.timescale() # Correct TLE example (both lines are 69 characters) line1 = '1 25544U 98067A 23348.69460010 .00000880 00000-0 29528-4 0 9997' line2 = '2 25544 51.6416 118.9950 0005917 217.4338 249.9547 15.49884521404169' satellite = EarthSatellite(line1, line2, 'ISS (ZARYA)', ts) ```
Ensure an active internet connection for Skyfield to download the ephemeris, or provide the correct path to a pre-downloaded file. The `load()` function handles downloading by default. Example: `from skyfield.api import load; planets = load('de421.bsp')`