ImportError: No Module Named <parent dir>

左心房为你撑大大i 提交于 2019-12-05 00:25:10

You are initially executing draw_lib.py. So the 'root directory' is / throughout the program.

Then, when you attempt 'from dir import draw_lib.py' in drawing.py it wont work because the root directory is still / and not dir/.

import draw_lib

Will work in drawing.py.

Example:

/
    __init__.py
    main.py
    test/
        __init___.py
        case.py

In main.py, put this:

import test.case

print 'main.py'

if __name__ == "__main__":
        test.case.test()

In test/case.py, put this:

import main

def test():
        print 'case.py'

My output:

main.py
main.py
case.py

As you can see, I imported main.py from a nested file. You'll see main.py two times. Once for the initial startup the second time when you import it in case.py.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!