0

enter image description here

I want to avoid a long import like this:

from a.b.c.d.e import 1
from a.b.c.d.e import 2

Is it possible to create an alias of a.b.c.d.e ? or can we make it shorter ?

I have tried something which is not working:

 import a.b.c.d.e as x
 import x.1
 import x.2
Amby
  • 447
  • 1
  • 8
  • 14
  • 1
    How has that become necessary? Why don't the intermediate `__init__.py`s aggregate some of the sub-modules? – jonrsharpe Aug 04 '15 at 09:27
  • 1
    `a.b.c.d.e` doesn't lead to an actual python module. `a.b.c.d.e.1` does. In that case you can say `import a.b.c.d.e.1 as x1` – Jeremy Fisher Aug 04 '15 at 13:50

1 Answers1

0

First of all, don't start your modules names from digits (although solution exists).

Next, (assuming 1.py, 2.py, 3.py are renamed to m1.py, m2.py, m3.py) approach

import a.b.c.d.e as x
m1 = x.m1
m2 = x.m2

works fine.

Also, as by jonrsharpe's comment, you'd better aggregate sub-modules imports in corresponding __init__.py files.

Community
  • 1
  • 1
pavel_form
  • 1,760
  • 13
  • 14