# Insert custom units into top level namespace

**URL:** <https://community.openastronomy.org/t/insert-custom-units-into-top-level-namespace/656>\
**Category:** Astropy\
**Tags:** astropy, question\
**Created:** [May 3, 2023, 10:29pm UTC](https://community.openastronomy.org/t/insert-custom-units-into-top-level-namespace/656 "2023-05-03T22:29:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![evidlo](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/evidlo/32/445_2.png) [@evidlo](https://community.openastronomy.org/u/evidlo)\
**Post date:** [May 3, 2023, 10:29pm UTC](https://community.openastronomy.org/t/insert-custom-units-into-top-level-namespace/656/1 "2023-05-03T22:29:51Z")

</div>

I have a `units.py` module in my project where I define a few custom units. How can i add these units so they are accessible like any of the other built-in units, like `u.meter`?

For example, if I have my units defined like this:

**myproj/units.py**

```python
import astropy.units as u

foobar = def_unit('foobar')
...

```

I want to be able to access built-in units like this:

```python
from myproj.units import u

x = np.array((1, 2, 3)) * u.meter
y = np.array((1, 2, 3)) * u.foobar

```

The `pint` library does this automatically with it’s `u.define` function. I would like to replicate this with astropy.

---

<div class="post-metadata">

**Author:** ![dhomeier](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/dhomeier/32/65_2.png) [@dhomeier](https://community.openastronomy.org/u/dhomeier)\
**Post date:** [May 4, 2023, 11:59am UTC](https://community.openastronomy.org/t/insert-custom-units-into-top-level-namespace/656/2 "2023-05-04T11:59:44Z")

</div>

Would adding them with `u.add_enabled_units([foobar])` etc. as described in  
[https://docs.astropy.org/en/stable/units/combining\_and\_defining.html#defining-units](https://docs.astropy.org/en/stable/units/combining_and_defining.html#defining-units)  
satisfy your needs, or are you trying to create a complete wrapper around astropy’s `units` that already has all your custom units enabled?

---

<div class="post-metadata">

**Author:** ![evidlo](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/evidlo/32/445_2.png) [@evidlo](https://community.openastronomy.org/u/evidlo)\
**Post date:** [May 4, 2023, 7:01pm UTC](https://community.openastronomy.org/t/insert-custom-units-into-top-level-namespace/656/3 "2023-05-04T19:01:55Z")

</div>

The latter. I would prefer to not have any distinction when accessing custom created units vs the built-in ones.

---

<div class="post-metadata">

**Author:** ![pllim](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/pllim/32/51_2.png) [@pllim](https://community.openastronomy.org/u/pllim)\
**Post date:** [May 5, 2023, 6:55pm UTC](https://community.openastronomy.org/t/insert-custom-units-into-top-level-namespace/656/4 "2023-05-05T18:55:11Z")

</div>

In my package with custom unit, I basically have user import from the units module in my package. So, not completely transparent, but once the quantity is defined, it works just like any other quantity. Perhaps not the answer you are looking for but I am not aware of a better way (would be interested if there is one).

[https://synphot.readthedocs.io/en/latest/synphot/units.html](https://synphot.readthedocs.io/en/latest/synphot/units.html)

---

<div class="post-metadata">

**Author:** ![evidlo](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/evidlo/32/445_2.png) [@evidlo](https://community.openastronomy.org/u/evidlo)\
**Post date:** [May 9, 2023, 3:49am UTC](https://community.openastronomy.org/t/insert-custom-units-into-top-level-namespace/656/5 "2023-05-09T03:49:49Z")

</div>

The only thing I can think to do is create a `units` module in my package and import all astropy units there, as @pllim said.

`foobar/units.py`

```python
from astropy.units import *

my_unit = def_unit('my_unit')

```

And then the units can be accessed the same way

```python
from foobar import units

x = 1 * units.m
y = 1 * units.my_unit

```

---

<div class="post-metadata">

**Author:** ![dhomeier](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/dhomeier/32/65_2.png) [@dhomeier](https://community.openastronomy.org/u/dhomeier)\
**Post date:** [May 11, 2023, 12:23pm UTC](https://community.openastronomy.org/t/insert-custom-units-into-top-level-namespace/656/6 "2023-05-11T12:23:53Z")

</div>

But are they all available under your `units` this way? Wouldn’t they need to be added to ` __all__ ` similar to [synphot.units — synphot v1.2.1.dev3+g11db7cf](https://synphot.readthedocs.io/en/latest/_modules/synphot/units.html) ?  
Wondering if something like

```auto
__all__ = ['my_unit', 'foobar'] + dir(astropy.units)

```

would work then.

---

<div class="post-metadata">

**Author:** ![evidlo](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/evidlo/32/445_2.png) [@evidlo](https://community.openastronomy.org/u/evidlo)\
**Post date:** [May 11, 2023, 4:32pm UTC](https://community.openastronomy.org/t/insert-custom-units-into-top-level-namespace/656/7 "2023-05-11T16:32:47Z")

</div>

It seems to work fine.

It still would be nice to have Pint-like behavior for `def_unit`, though.
