Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: how to import a module a.b as a_.b?

I hava a package a and the tree is:

a/
  /__init__.py
  /b.py

And how can I import a.b as a_.b ?

like image 404
atupal Avatar asked Dec 11 '25 05:12

atupal


1 Answers

You'll have to do it in two lines:

import a as a_
from a import b

print(a_.b)
# <module 'a.b' from '.\\a\\b.py'>

Or:

import a as a_
import a.b

print(a_.b)
# <module 'a.b' from '.\\a\\b.py'>

The first has the disadvantage that it puts b into your namespace and the second has the disadvantage that it puts a into your namespace. If you want to, you can fix that by using del b and del a, respectively.

Alternatively, you can also write the second line as from a import b as _ or import a.b as _, respectively, which will prevent b and a from appearing in your namespace.

like image 55
flornquake Avatar answered Dec 13 '25 19:12

flornquake