Registry / serialization / astral

astral

JSON →
library3.2pypypi✓ verified 23d ago

Astral is a Python package for calculating the times of various aspects of the sun and moon, including dawn, sunrise, noon, sunset, dusk, and moon phases. It also provides functions for solar azimuth and elevation. The library includes a self-contained geocoder for looking up location information by name. The current version is 3.2, and it maintains an active release cadence, frequently adding new features and improvements.

pip install astral
INSTALL
IMPORT
SIG · ASTRAL
A
astral
serializationpythonv3.2
Install
1.6s avg
Import
71ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.074s · 18.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.068s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LocationInfo
from astral.location import LocationInfo
Observer
from astral.geocoder import lookup from astral import Observer
sun
from astral.sun import sun
moonphase
from astral.moon import moonphase
Astral
from astral.geocoder import lookup, database city = lookup("London", database())
from astral import Astral a = Astral() city = a['London']
The `Astral` class and direct dictionary-like access for cities (`a[city_name]`) are part of an older API. Modern usage prefers `lookup` from `astral.geocoder` to retrieve `LocationInfo` objects.

This example demonstrates how to define a location using `LocationInfo`, retrieve an `Observer` object, and then calculate the sun's key events (dawn, sunrise, noon, sunset, dusk) for a specific date.

import datetime from astral.location import LocationInfo from astral.sun import sun # Define a location l = LocationInfo('London', 'England', 'Europe/London', 51.5, 0.1) # Get an Observer object from the LocationInfo observer = l.observer # Get sun times for today today = datetime.date.today() s = sun(observer, date=today) print(f"Sun Information for {l.name} on {today}:") print(f" Dawn: {s['dawn']}") print(f" Sunrise: {s['sunrise']}") print(f" Noon: {s['noon']}") print(f" Sunset: {s['sunset']}") print(f" Dusk: {s['dusk']}")
Debug
Known issues
breakingIn version 3.0, `astral` migrated from `pytz` to Python's built-in `zoneinfo` for timezone handling. This means for Python versions prior to 3.9, you will need to explicitly install `backports.zoneinfo` to ensure timezone functionality works correctly.
fix
For Python < 3.9, install `backports.zoneinfo`: `pip install backports.zoneinfo`. Ensure your code handles `zoneinfo` objects instead of `pytz` timezone objects.
affects: >=3.0
deprecatedThe direct use of `astral.Astral()` and accessing cities like `a['London']` is part of an older API and may not function as expected or be supported in future versions. The `Astral` class itself was removed in version 3.0.
fix
Migrate to using `astral.geocoder.lookup(name, database())` to retrieve `LocationInfo` objects, and then access the `observer` attribute to get the `Observer` object.
affects: >=3.0
gotchaWhen passing `datetime` objects to functions like `sun()` or `azimuth()`, if the datetime object is 'naive' (i.e., lacks timezone information), `astral` will assume it represents a UTC time. This can lead to incorrect calculations if the user expects local time.
fix
Always use timezone-aware `datetime` objects. For Python >= 3.9, use `zoneinfo.ZoneInfo('timezone_name')`. For Python < 3.9, ensure `backports.zoneinfo` is installed and use its `ZoneInfo` for timezone localization.
affects: All
gotchaThe Google Geocoder (`astral.geocoder.GoogleGeocoder`) requires an API key, as mandated by Google. Attempting to use it without providing a valid API key will result in errors.
fix
When initializing `GoogleGeocoder`, pass your Google Maps API key as the `api_key` parameter: `GoogleGeocoder(api_key='YOUR_API_KEY')`. For simple location lookups without an API key, use the built-in `astral.geocoder.database()` and `lookup()`.
affects: >=1.6
gotchaThe `astral` package currently does not adjust solar elevation calculations for changes in observer elevation. While the effect is generally very small, users requiring extreme precision for high elevations might find this behavior unexpected.
fix
Be aware of this limitation when performing highly precise solar elevation calculations, particularly for observers at very high altitudes. The documentation indicates the difference is usually negligible for most applications.
affects: All
Errors
Common errors & fixes
ValueError: Sun never reaches 6.0 degrees below the horizon, at this location
This error occurs when calculating sun events at high latitudes where the sun does not dip below 6 degrees below the horizon, making certain calculations invalid.
fix
Adjust the parameters or handle exceptions for locations where the sun does not meet the expected conditions.
ValueError: Sun never transits at a zenith of 96.0 on YYYY-MM-DD
This error arises when attempting to calculate sun transit events at locations or dates where the sun does not reach the specified zenith angle.
fix
Verify the location and date parameters to ensure they are within valid ranges for sun transit calculations.
ModuleNotFoundError: No module named 'astral'
This error indicates that the Astral library is not installed in the Python environment.
fix
Install the Astral library using pip: `pip install astral`.
ImportError: cannot import name 'Location' from 'astral'
This error occurs due to changes in the Astral library's API, where 'Location' has been replaced with 'LocationInfo'.
fix
Update the import statement to: `from astral import LocationInfo`.
TypeError: sun() missing 1 required positional argument: 'observer'
This error happens when the 'sun' function is called without providing the required 'observer' parameter.
fix
Ensure that the 'observer' parameter is provided when calling the 'sun' function, e.g., `sun(observer=location.observer, date=datetime.date.today())`.
Upgrade
Version history
3.2latest on PyPI · released Nov 5, 2022
Audit
Dependencies
backports.zoneinfooptionalRequired for Python versions prior to 3.9 when timezone functionality is used, as `astral` transitioned from `pytz` to `zoneinfo` in version 3.0.
Agent activity
36 hits · last 30 days
node
30
OpenAI (training)
1
Resources
astral — pip install astral · libregistry