Python Import an object that comes from one module from another module to the third module

I read sourcode for a python project and came across the following line:

from couchexport.export import Format

(source: https://github.com/wbnigeria/couchexport/blob/master/couchexport/views.py#L1 )

I went over to couchexport/export.pyfind out what happened Format(Class? Dict? Something else?). Sorry, Formatnot in this file. export.pyhowever imports Formatfrom couchexport.models where there is a class Format(source: https://github.com/wbnigeria/couchexport/blob/master/couchexport/models.py#L11 ).

When I open the source file in my IDE and look at the declaration, in the line mentioned at the beginning of this question, it leads directly to models.py.

What's happening? How can I import from one file ( export.py) the actual import from another file ( models.py) without an explicit indication?

+5
source share
1 answer

If the module ais executing from b import Foo, then it Foois a member aand is available as a.Foo. This means that now you can import it with from a import Foo.

This is usually used if you have a large library distributed between several files, and you want them to be accessible from one place. Say you have a package Foowith the following location:

foo/
    a.py
    b.py
    c.py
    __init__.py

a.py, b.py, c.py, Define the classes a, Band Crespectively.

If you want to use these classes, you usually need to write

from foo.a import A
from foo.b import B
from foo.c import C

:

  • ( )
  • , .

, __init__.py:

from a import A
from b import B
from c import C

, :

from foo import A,B,C
+16

All Articles