-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
__init__.py required for pkgutil.walk_packages in python3 #73444
Comments
PEP-420 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:
And a test script # test.py
In both python2 and python3 I get the following output:
Removing the __init__.py files and only using python3, I get this:
The modules are definitely importable:
Originally asked as a question on stackoverflow: http://stackoverflow.com/questions/41203765/init-py-required-for-pkgutil-walk-packages-in-python3 |
While it is rather trivial to implement the proposed functionality - all that's required here is to eliminate the check for __init__.py from pkgutil._iter_file_finder_modules - this would have undesired impacts on, e.g., pydoc.apropos: I think this is a fundamental problem with namespace packages: they are nice and flexible for specific imports, but they make it impossible to know whether a directory found on the filesystem is *intended* as a Python package or not. |
Ok, I was exaggerating here. To do it right would require a more complex change, but that's all that's needed to get an estimate of the effect the real thing would have. |
This is almost wrong. PEP-420 added a new way for "namespace pacakge." (See https://dev.to/methane/don-t-omit-init-py-3hga) Then, should pkgutil.walk_packages walk into all directories (e.g. node_modules) ? I don't think so. |
If the resolution here is that this is behaving as intended (which personally I disagree with), I think this issue should remain open as a documentation task - the docs should clearly state that this does not apply to PEP-420 namespace packages. |
I am totally agree with Wolfgang:
I think we shouldn't treat normal directory as namespace package until some portion in the directory is imported, or it is specified explicitly. So walk_packages() should be used like: walk_packages("/path/to/namespace", "namespace") I already rejected similar issue: bpo-29642. If you can not agree with me, please make an thread in python-dev ML or discuss.python.org. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: