In Python, how to import from the __init__.py in the same directory

后端 未结 2 438
庸人自扰
庸人自扰 2021-02-03 11:04

Suppose I have a module rules with the following structure:

rules
├── conditions.py
├── __init__.py

In the script conditions

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-03 11:35

    If you get

    ImportError: attempted relative import with no known parent package

    when you do like

    import . from something
    

    especially from the script executed, just try

    from __init__ import something
    

    Even though it could be problematic when there are many __init__.pys in sys.path, it would be helpful in some simple situaions.

提交回复
热议问题