Python submodule imports using __init__.py

后端 未结 3 1088
心在旅途
心在旅途 2020-12-09 16:33

I\'m learning Python, and I can\'t figure out how imports in __init__.py work.

I understand from the Python tutorial that the __init__.py f

3条回答
  •  一生所求
    2020-12-09 16:57

    It's an absolute hard-and-fast rule in Python that a name must always be defined or imported within the module where you're using it. Here you never import anything inside test.py - so as the error says, do_something is not defined.

    Even if your package/__init__.py file was executed (which, as others have pointed out, it isn't), your code still wouldn't work as it is, because the import of do_something has to be done inside test.py if you want to reference it in that file.

提交回复
热议问题