Grant Curell
Grant Curell

Reputation: 1783

Why does an import with the top level directory work in Python without init?

enter image description here

I cloned the project here: https://github.com/open-switch/opx-tools and loaded it up in PyCharm to do some debugging. What I don't understand is the original line says from opx_tools.opx_config_utils import *. I have OpenSwitch running on an actual switch with this import and it works just fine, but I don't understand why.

opx_tools is the name of the top level directory and there is no __init__ file in it nor is there a file called opx_config_utils in the top level directory. opx_config_utils exists, but it is in the lib directory. In my PyCharm instance changing opx_tools to lib in the import fixes the import and allows me to search declarations etc.

What is the difference between my PyCharm environment and the "production" environment? Why does opx_tools work?

Upvotes: 0

Views: 88

Answers (1)

progmatico
progmatico

Reputation: 4964

Since version 3.3, Python let's you define namespace packages implicitly. Contrary to regular packages, these do not need to be contained inside a single folder and sub-folders, and do not require the __init__.py files.

See PEP 420 for details.

Upvotes: 1

Related Questions