Importing files in Python from __init__.py

前端 未结 3 869
迷失自我
迷失自我 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 20:53

    Yes, but don't do it. Seriously, don't. But if you still want to know how to do it, it'd look like this:

    import __init__
    
    re = __init__.re
    logging = __init__.logging
    os = __init__.os
    

    I say not to do it not only because it's messy and pointless, but also because your package isn't really supposed to use __init__.py like that. It's package initialization code.

提交回复
热议问题