{"id":1912,"library":"astropy","title":"Astropy","description":"Astropy is a community-developed core Python package for astronomy and astrophysics. It provides a wide range of tools for common astronomical tasks, including units, coordinates, FITS file I/O, and cosmology. The library is actively maintained, with major releases annually, minor releases every six months, and bugfix releases as needed. The current version is 7.2.0.","status":"active","version":"7.2.0","language":"en","source_language":"en","source_url":"https://github.com/astropy/astropy","tags":["astronomy","astrophysics","units","coordinates","fits","tables","cosmology","science","numerical"],"install":[{"cmd":"pip install astropy[recommended]","lang":"bash","label":"Install with recommended dependencies"},{"cmd":"conda install astropy","lang":"bash","label":"Install with Conda (recommended for Anaconda users)"}],"dependencies":[{"reason":"Core numerical computing dependency, fundamental to Astropy's data structures.","package":"numpy","optional":false},{"reason":"Used for various scientific computing tasks, often a dependency for sub-packages.","package":"scipy","optional":true},{"reason":"Commonly used for plotting and visualization, especially with Astropy data.","package":"matplotlib","optional":true},{"reason":"Optional backend for faster ECSV table reading in astropy.io.misc (v7.2+).","package":"pyarrow","optional":true},{"reason":"Optional backend for faster ECSV table reading and DataFrame conversion (v7.2+).","package":"pandas","optional":true},{"reason":"Used for generic DataFrame conversion methods (to_df/from_df) in astropy.table (v7.2+).","package":"narwhals","optional":true},{"reason":"Improves SSL certificate verification for remote data downloads on some systems, especially Windows.","package":"certifi","optional":true}],"imports":[{"note":"The conventional and recommended alias for units is 'u'.","wrong":"import astropy.units","symbol":"units","correct":"from astropy import units as u"},{"note":"The conventional and recommended alias for coordinates is 'coord'.","wrong":"import astropy.coordinates","symbol":"coordinates","correct":"from astropy import coordinates as coord"},{"symbol":"SkyCoord","correct":"from astropy.coordinates import SkyCoord"},{"symbol":"Table","correct":"from astropy.table import Table"},{"note":"Avoid wildcard imports; import the 'fits' sub-package directly or alias it if replacing old 'pyfits'.","wrong":"from astropy.io.fits import *","symbol":"fits","correct":"from astropy.io import fits"},{"note":"The conventional and recommended alias for constants is 'const'.","wrong":"import astropy.constants","symbol":"constants","correct":"from astropy import constants as const"},{"note":"As of Astropy 8.0, cosmology classes and functions must be imported directly from `astropy.cosmology`.","wrong":"from astropy.cosmology.flrw import FlatLambdaCDM","symbol":"cosmology (classes/functions)","correct":"from astropy.cosmology import FlatLambdaCDM, z_at_value"}],"quickstart":{"code":"import numpy as np\nfrom astropy import units as u\nfrom astropy import constants as const\nfrom astropy.coordinates import SkyCoord\nfrom astropy.table import Table\n\n# Define a SkyCoord object\npos = SkyCoord(ra=10.68458 * u.deg, dec=41.26917 * u.deg, frame='icrs')\nprint(f\"Sky position: {pos}\\n\")\n\n# Create a Quantity and perform a calculation\nmass = 5.972e24 * u.kg # Earth's mass\nradius = 6371 * u.km # Earth's radius\ndensity = mass / (4/3 * np.pi * radius**3)\nprint(f\"Calculated density: {density.to(u.g / u.cm**3):.2f}\\n\")\n\n# Access a physical constant\nc = const.c\nprint(f\"Speed of light: {c.to(u.km/u.s):.0f}\\n\")\n\n# Create a simple table\ndata = [\n    (1, 'NGC 10', 12.5),\n    (2, 'M 31', 3.4),\n    (3, 'Abell 2744', 16.0)\n]\nt = Table(rows=data, names=('id', 'name', 'magnitude'))\nprint(\"Example Table:\")\nprint(t)\n","lang":"python","description":"This quickstart demonstrates key Astropy functionalities: defining celestial coordinates with `SkyCoord`, performing unit-aware calculations using `Quantity` and `astropy.units`, accessing physical constants via `astropy.constants`, and working with tabular data using `astropy.table`."},"warnings":[{"fix":"Update import statements from `from astropy.cosmology.flrw import FlatLambdaCDM` to `from astropy.cosmology import FlatLambdaCDM`.","message":"In Astropy 8.0, deprecated module-level shim files in `astropy.cosmology` were removed. All cosmology classes and functions must now be imported directly from `astropy.cosmology`.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Always update Astropy in Anaconda using `conda update astropy` instead of `pip install astropy --upgrade`.","message":"Anaconda users upgrading Astropy with `pip` can result in a corrupted installation. It is strongly recommended to use `conda` for updates in Anaconda environments.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Explicitly re-apply units or convert to base units before operations where units might be lost. Check `astropy.units` documentation for 'Known Issues' and 'Performance Tips' regarding `Quantity` behavior.","message":"Astropy `Quantity` objects can sometimes lose their units when used with certain NumPy operations or when broadcasted in specific scenarios. Careful attention is needed to ensure unit preservation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Convert `Quantity` to its value using `.value` before passing to `numpy.prod` if units are not critical for the intermediate step, or perform a manual loop for unit-aware multiplication if necessary.","message":"`numpy.prod` cannot be directly applied to `Quantity` instances because the resulting unit depends on the input shape, making correct unit handling ambiguous.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Migrate testing workflows to use `pytest` directly or follow updated guidelines in Astropy's developer documentation for testing.","message":"The `astropy.test` runner and related API (`astropy.tests.runner.keyword`, `TestRunnerBase`, `TestRunner`) are pending deprecation and will eventually be removed. Downstream packages using `packagename.test` generated with `TestRunner` may also be affected.","severity":"deprecated","affected_versions":">=7.2.0 (pending deprecation warning), >=8.0.0 (potential `AstropyDeprecationWarning`)"},{"fix":"Define logarithmic units directly, e.g., `dBW = u.dB(u.W)` instead of `dBW = u.def_unit('dBW', u.dB(u.W))`.","message":"When defining custom logarithmic units, `u.def_unit` should be avoided as it can lead to unexpected behavior. Logarithmic units should be defined directly.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}