Relative import problems in Python 3

后端 未结 7 1006
庸人自扰
庸人自扰 2021-02-13 21:01

Python imports drive me crazy (my experience with python imports sometime doesn\'t correspond at all to idiom \'Explicit is better than implicit\' :( ):

[app]
           


        
7条回答
  •  一整个雨季
    2021-02-13 21:53

    Make sure your package1 is a folder. Create a class in __init__.py -- say class1. Include your logic in a method under class1 -- say method1.

    Now, write the following code -

    from .package1 import class1
    class1.method1()
    

    This was my way of resolving it. To summarize, your root directory is . so write your import statement using . notations, e.g. from .package or from app.package.

提交回复
热议问题