ImportWarning when importing a PEP 420 namespace package
See original GitHub issueThe pkg_resources
code is raising an ImportWarning
when it attempts to import a PEP 420 implicit namespace package:
ImportError: Failed to import test module: test.test_methods
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/home/bignose/Projects/debian/dput/trunk/test/test_methods.py", line 26, in <module>
import testscenarios
File "/usr/lib/python3/dist-packages/testscenarios/__init__.py", line 57, in <module>
from testscenarios.scenarios import (
File "/usr/lib/python3/dist-packages/testscenarios/scenarios.py", line 34, in <module>
from testtools import iterate_tests
File "/usr/lib/python3/dist-packages/testtools/__init__.py", line 124, in <module>
from pbr.version import VersionInfo
File "/usr/lib/python3/dist-packages/pbr/version.py", line 25, in <module>
import pkg_resources
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3037, in <module>
@_call_aside
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3021, in _call_aside
f(*args, **kwargs)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3065, in _initialize_master_working_set
for dist in working_set
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3065, in <genexpr>
for dist in working_set
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2595, in activate
declare_namespace(pkg)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2163, in declare_namespace
_handle_ns(packageName, path_item)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2083, in _handle_ns
loader = importer.find_module(packageName)
File "<frozen importlib._bootstrap_external>", line 445, in _find_module_shim
ImportWarning: Not importing directory /usr/lib/python3/dist-packages/keyrings: missing __init__
(To reproduce this, enable the -W error
option to python3
. I did this to try to catch warnings in my code, assuming that libraries would not cause them 😕)
The “Not importing directory […] missing init” contradicts the explicit specification from PEP 420:
Namespace packages [as specified in this PEP] cannot contain an init.py .
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:19 (13 by maintainers)
Top Results From Across the Web
PEP 420 – Implicit Namespace Packages
Such a directory will now be imported as a namespace package, whereas in prior Python versions an ImportWarning would be raised. Nick Coghlan ......
Read more >I don't need `__init__.py`? PEP 420 and namespace ...
today I talk about namespace packages, what PEP 420 is, and why I don't use namespace packagesplaylist: ...
Read more >5. The import system — Python 3.11.0 documentation
In this case, Python will create a namespace package for the top-level parent package whenever it or one of its subpackages is imported....
Read more >python - When do I require using __init__.py? - Stack Overflow
A PEP 420 package which serves only as a container for subpackages. ... Such a directory will now be imported as a namespace...
Read more >Changelog — pytest documentation
running pytest tests now imports the conftest twice, ... the two common test directory layouts, and how it interacts with PEP 420-namespace packages....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m seeing this as well. It is causing loads of noise in my logs when Python warnings are enabled. Here is a minimal bash script to demonstrate the error:
Actual output:
Glad I pasted my exact commands, because I started replicating it on my local workstation and couldn’t figure out what I’d done wrong. I see now I neglected to include the important
-W error
when I tried to replicate the issue above.