[New-bugs-announce] [issue29258] __init__.py required for pkgutil.walk_packages in python3

Anthony Sottile report at bugs.python.org
Thu Jan 12 23:47:54 EST 2017


New submission from Anthony Sottile:

PEP420 makes __init__.py files optional: https://docs.python.org/3/whatsnew/3.3.html#pep-420-implicit-namespace-packages

Though it seems without them, pkgutil.walk_packages does not function as desired: https://docs.python.org/3/library/pkgutil.html#pkgutil.walk_packages

Consider the following example:

```
$ tree foo
foo
├── bar
│   ├── baz.py
│   └── __init__.py
├── __init__.py
└── womp.py
```

And a test script

# test.py
```
import pkgutil

import foo


for _, mod, _ in pkgutil.walk_packages(foo.__path__, foo.__name__ + '.'):
    print(mod)
```

In both python2 and python3 I get the following output:

```
$ python2.7 test.py
foo.bar
foo.bar.baz
foo.womp
$ python3.5 test.py
foo.bar
foo.bar.baz
foo.womp
```

Removing the __init__.py files and only using python3, I get this:

```
$ find -name '__init__.*' -delete
$ python3.5 test.py
foo.bar
```

The modules are definitely importable:

```
$ python3.5 -c 'import foo.bar.baz'
$
```

Originally asked as a question on stackoverflow: http://stackoverflow.com/questions/41203765/init-py-required-for-pkgutil-walk-packages-in-python3

----------
components: Library (Lib)
messages: 285361
nosy: Anthony Sottile
priority: normal
severity: normal
status: open
title: __init__.py required for pkgutil.walk_packages in python3
type: behavior
versions: Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29258>
_______________________________________


More information about the New-bugs-announce mailing list