Force importing module from current directory

后端 未结 3 1301
情歌与酒
情歌与酒 2020-12-25 12:59

I have package p that has modules a and b. a relies on b:

b.py contents:



        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-25 13:15

    After rereading the Python import documentation, the correct answer to my original problem is:

    To ensure that b imports a from its own package its just enough to write the following in the b:

    import a
    

    Here is the quote from the docs:

    The submodules often need to refer to each other. For example, the surround module might use the echo module. In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path.

    Note: As J.F. Sebastian suggest in the comment below, use of implicit imports is not advised, and they are, in fact, gone in Python 3.

提交回复
热议问题