# Constants (astropy.constants)¶

## Introduction¶

astropy.constants contains a number of physical constants useful in Astronomy. Constants are Quantity objects with additional metadata describing their provenance and uncertainties.

## Getting Started¶

To use the constants in International System of Units (SI units), you can import the constants directly from the astropy.constants sub-package:

>>> from astropy.constants import G


Or, if you want to avoid having to explicitly import all of the constants you need, you can do:

>>> from astropy import constants as const


and then subsequently use, for example, const.G. Constants are fully-fledged Quantity objects, so you can conveniently convert them to different units. For example:

>>> print(const.c)
Name   = Speed of light in vacuum
Value  = 299792458.0
Uncertainty  = 0.0
Unit  = m / s
Reference = CODATA 2018

>>> print(const.c.to('km/s'))
299792.458 km / s

>>> print(const.c.to('pc/yr'))
0.306601393788 pc / yr


You can then use them in conjunction with unit and other nonconstant Quantity objects:

>>> from astropy import units as u
>>> F = (const.G * 3. * const.M_sun * 100 * u.kg) / (2.2 * u.au) ** 2
>>> print(F.to(u.N))
0.3675671602160826 N


It is possible to convert most constants to Centimeter-Gram-Second (CGS) units using, for example:

>>> const.c.cgs
<Quantity   2.99792458e+10 cm / s>


However, some constants are defined with different physical dimensions in CGS and cannot be directly converted. Because of this ambiguity, such constants cannot be used in expressions without specifying a system:

>>> 100 * const.e
Traceback (most recent call last):
...
TypeError: Constant u'e' does not have physically compatible units
across all systems of units and cannot be combined with other
values without specifying a system (eg. e.emu)
>>> 100 * const.e.esu
<Quantity 4.8032045057134676e-08 Fr>


## Collections of Constants (and Prior Versions)¶

Constants are organized into version modules. The constants for astropy 2.0 can be accessed in the astropyconst20 module. For example:

>>> from astropy.constants import astropyconst20 as const
>>> print(const.e)
Name   = Electron charge
Value  = 1.6021766208e-19
Uncertainty  = 9.8e-28
Unit  = C
Reference = CODATA 2014


Physical CODATA constants are in modules with names like codata2010, codata2014, or codata2018:

>>> from astropy.constants import codata2014 as const
>>> print(const.h)
Name   = Planck constant
Value  = 6.62607004e-34
Uncertainty  = 8.1e-42
Unit  = J s
Reference = CODATA 2014


Astronomical constants defined (primarily) by the International Astronomical Union (IAU) are collected in modules with names like iau2012 or iau2015:

>>> from astropy.constants import iau2012 as const
>>> print(const.L_sun)
Name   = Solar luminosity
Value  = 3.846e+26
Uncertainty  = 5e+22
Unit  = W
Reference = Allen's Astrophysical Quantities 4th Ed.

>>> from astropy.constants import iau2015 as const
>>> print(const.L_sun)
Name   = Nominal solar luminosity
Value  = 3.828e+26
Uncertainty  = 0.0
Unit  = W
Reference = IAU 2015 Resolution B 3


The astronomical and physical constants are combined into modules with names like astropyconst13, astropyconst20, and astropyconst40 for different versions. However, importing these prior version modules directly will lead to inconsistencies with other subpackages that have already imported astropy.constants. Notably, astropy.units will have already used the default version of constants. When using prior versions of the constants in this manner, quantities should be constructed with constants instead of units.

To ensure consistent use of a prior version of constants in other Astropy packages (such as astropy.units) that import constants, the physical and astronomical constants versions should be set via ScienceState classes. These must be set before the first import of either astropy.constants or astropy.units. For example, you can use the CODATA2010 physical constants and the IAU 2012 astronomical constants:

>>> from astropy import physical_constants, astronomical_constants
>>> physical_constants.set('codata2010')  # doctest: +SKIP
<ScienceState physical_constants: 'codata2010'>
>>> physical_constants.get()  # doctest: +SKIP
'codata2010'
>>> astronomical_constants.set('iau2012')  # doctest: +SKIP
<ScienceState astronomical_constants: 'iau2012'>
>>> astronomical_constants.get()  # doctest: +SKIP
'iau2012'


Then all other packages that import astropy.constants will self-consistently initialize with that prior version of constants.

The versions may also be set using values referring to the version modules:

>>> from astropy import physical_constants, astronomical_constants
>>> physical_constants.set('astropyconst13')  # doctest: +SKIP
<ScienceState physical_constants: 'codata2010'>
>>> physical_constants.get()  # doctest: +SKIP
'codata2010'
>>> astronomical_constants.set('astropyconst13')  # doctest: +SKIP
<ScienceState astronomical_constants: 'iau2012'>
>>> astronomical_constants.get()  # doctest: +SKIP
'iau2012'


If either astropy.constants or astropy.units have already been imported, a RuntimeError will be raised.

>>> import astropy.units
>>> from astropy import physical_constants, astronomical_constants
>>> astronomical_constants.set('astropyconst13')
Traceback (most recent call last):
...


To temporarily set constants to an older version (e.g., for regression testing), a context manager is available, as follows:

>>> from astropy import constants as const
>>> with const.set_enabled_constants('astropyconst13'):
...     print(const.h)
Name   = Planck constant
Value  = 6.62606957e-34
Uncertainty  = 2.9e-41
Unit  = J s
Reference = CODATA 2010
>>> print(const.h)
Name   = Planck constant
Value  = 6.62607015e-34
Uncertainty  = 0.0
Unit  = J s
Reference = CODATA 2018


The context manager may be used at any time in a Python session, but it uses the prior version only for astropy.constants, and not for any other subpackage such as astropy.units.

## Reference/API¶

### astropy.constants Package¶

Contains astronomical and physical constants for use in Astropy or other places.

A typical use case might be:

>>> from astropy.constants import c, m_e
>>> # ... define the mass of something you want the rest energy of as m ...
>>> m = m_e
>>> E = m * c**2
>>> E.to('MeV')
<Quantity 0.510998927603161 MeV>


The following constants are available:

Name Value Unit Description
G 6.6743e-11 m3 / (kg s2) Gravitational constant
N_A 6.02214076e+23 1 / (mol) Avogadro’s number
R 8.31446262 J / (K mol) Gas constant
Ryd 10973731.6 1 / (m) Rydberg constant
alpha 0.00729735257   Fine-structure constant
atm 101325 Pa Standard atmosphere
b_wien 0.00289777196 m K Wien wavelength displacement law constant
c 299792458 m / (s) Speed of light in vacuum
e 1.60217663e-19 C Electron charge
eps0 8.85418781e-12 F/m Vacuum electric permittivity
g0 9.80665 m / s2 Standard acceleration of gravity
h 6.62607015e-34 J s Planck constant
hbar 1.05457182e-34 J s Reduced Planck constant
k_B 1.380649e-23 J / (K) Boltzmann constant
m_e 9.1093837e-31 kg Electron mass
m_n 1.6749275e-27 kg Neutron mass
m_p 1.67262192e-27 kg Proton mass
mu0 1.25663706e-06 N/A2 Vacuum magnetic permeability
muB 9.27401008e-24 J/T Bohr magneton
sigma_T 6.65245873e-29 m2 Thomson scattering cross-section
sigma_sb 5.67037442e-08 W / (K4 m2) Stefan-Boltzmann constant
u 1.66053907e-27 kg Atomic mass
GM_earth 3.986004e+14 m3 / (s2) Nominal Earth mass parameter
GM_jup 1.2668653e+17 m3 / (s2) Nominal Jupiter mass parameter
GM_sun 1.3271244e+20 m3 / (s2) Nominal solar mass parameter
L_bol0 3.0128e+28 W Luminosity for absolute bolometric magnitude 0
L_sun 3.828e+26 W Nominal solar luminosity
M_earth 5.97216787e+24 kg Earth mass
M_jup 1.8981246e+27 kg Jupiter mass
M_sun 1.98840987e+30 kg Solar mass
R_earth 6378100 m Nominal Earth equatorial radius
R_jup 71492000 m Nominal Jupiter equatorial radius
R_sun 695700000 m Nominal solar radius
au 1.49597871e+11 m Astronomical Unit
kpc 3.08567758e+19 m Kiloparsec
pc 3.08567758e+16 m Parsec

#### Functions¶

 set_enabled_constants(modname) Context manager to temporarily set values in the constants namespace to an older version.

#### Classes¶

 Constant A physical or astronomical constant. EMConstant An electromagnetic constant.