PyEphem

4.2.1 · active · verified Fri Apr 10

PyEphem is a Python library for computing positions of the planets and stars. It provides precise astronomical calculations, including positions of celestial bodies, times of sunrise/sunset, moon phases, and more, based on standard algorithms. The current version is 4.2.1, with releases typically occurring a few times a year for minor updates and bug fixes, and major versions every 1-2 years.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an observer, set its location and time (important: use UTC!), create a celestial body, compute its position relative to the observer, and print its coordinates, explicitly converting radian outputs to degrees for human readability.

import ephem
import datetime

# Create an observer at a specific location and time (UTC)
boston = ephem.Observer()
boston.lat = '42.35'  # North latitude
boston.lon = '-71.05' # West longitude
boston.elevation = 0  # Meters above sea level
boston.date = datetime.datetime.utcnow() # Set date to current UTC time

# Create a planet object (Mars)
mars = ephem.Mars()

# Compute its position for the observer's location and time
mars.compute(boston)

# Print celestial coordinates, converting radians to degrees for readability
print(f"Mars Right Ascension (RA): {ephem.degrees(mars.ra)}")
print(f"Mars Declination (Dec): {ephem.degrees(mars.dec)}")
print(f"Mars Azimuth: {ephem.degrees(mars.az)}")
print(f"Mars Altitude: {ephem.degrees(mars.alt)}")
print(f"Mars Distance: {mars.range:.2f} AU")

view raw JSON →