Importing files in Python from __init__.py

前端 未结 3 865
迷失自我
迷失自我 2020-12-28 20:32

Suppose I have the following structure:

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

a.py, b.py and c.py share some

3条回答
  •  醉话见心
    2020-12-28 21:03

    You can do this using a common file such as include.py, but it goes against recommended practices because it involves a wildcard import. Consider the following files:

    app/
        __init__.py
    foo/
        a.py
        b.py
        c.py
        include.py <- put the includes here.
        __init__.py
    

    Now, in a.py, etc., do:

    from include import *
    

    As stated above, it's not recommended because wildcard-imports are discouraged.

提交回复
热议问题