[Python-Dev] Making submodules available when importing top one

Guido van Rossum guido at python.org
Wed Oct 16 18:41:38 CEST 2013


On Wed, Oct 16, 2013 at 9:26 AM, Facundo Batista
<facundobatista at gmail.com>wrote:

> (all this using Python 3.4.0a3+)
>
> In the stdlib, I see that (as an example):
>
> >>> import os
> >>> os.path.abspath
> <function abspath at 0xb7123734>
> >>> os.path
> <module 'posixpath' from '/.../python/trunk/Lib/posixpath.py'>
>
>
> However, for other (newer) modules:
>
> >>> import urllib
> >>> urllib.requests.urlopen
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'module' object has no attribute 'requests'
> >>> import concurrent
> >>> concurrent.futures
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'module' object has no attribute 'futures'
>
>
> So, is there a reason to not expose the submodules in the top module?
> This should be fixed?
>

os.path is special. The behavior of other modules is intentional.

In general it is not a good idea to automatically import all submodules
when you import a top-level package; imaging what this would do if you have
a large top-level package (maybe a namespace package) containing hundreds
of modules.

For specific top-level packages that want to provide all (or some)
submodules upon import of just the top-level package, they should have the
sub-imports in the top-level __init__.py.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20131016/71786c2b/attachment.html>


More information about the Python-Dev mailing list