Why can't I refer to submodules from a top Python module?

I’m confused with how Python imports and handle submodules. Why can’t I do the following?

import concurrent ex = concurrent.futures.ThreadPoolExecutor() 

The result is

AttributeError: module ‘concurrent’ has no attribute ‘futures’

Looking at the source code I saw that the concurrent library folder has an empty __init__.py file. Shoudn’t Python treat the folder as a module and hence search for the futures submodule (which exists)?

I known I can do the import as import concurrent.futures, however It is not clear to me why I cannot just import the top module and refer to the submodules as it complies with the Python __init__.py structure.

Add Comment
1 Answer(s)

With Python modules sometimes they are separated as to force the user to import submodules explicitly as to not clutter the namespace and reduce memory usage when loading modules that have many submodules attached to them.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.