question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImportWarning when importing a PEP 420 namespace package

See original GitHub issue

The 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:19 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
jdufresnecommented, Aug 1, 2017

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:

#!/bin/bash

rm -rf venv
python3 -m venv venv
venv/bin/pip install sphinxcontrib-websupport
venv/bin/python -Werror -c 'import pkg_resources'

Actual output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/jon/warning-test/venv/lib64/python3.5/site-packages/pkg_resources/__init__.py", line 2958, in <module>
    @_call_aside
  File "/home/jon/warning-test/venv/lib64/python3.5/site-packages/pkg_resources/__init__.py", line 2944, in _call_aside
    f(*args, **kwargs)
  File "/home/jon/warning-test/venv/lib64/python3.5/site-packages/pkg_resources/__init__.py", line 2985, in _initialize_master_working_set
    dist.activate(replace=False)
  File "/home/jon/warning-test/venv/lib64/python3.5/site-packages/pkg_resources/__init__.py", line 2521, in activate
    declare_namespace(pkg)
  File "/home/jon/warning-test/venv/lib64/python3.5/site-packages/pkg_resources/__init__.py", line 2103, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/home/jon/warning-test/venv/lib64/python3.5/site-packages/pkg_resources/__init__.py", line 2037, in _handle_ns
    loader = importer.find_module(packageName)
  File "<frozen importlib._bootstrap_external>", line 445, in _find_module_shim
ImportWarning: Not importing directory /home/jon/warning-test/venv/lib64/python3.5/site-packages/sphinxcontrib: missing __init__
1reaction
jaracocommented, Sep 14, 2018

I tried replicating the issue on Python 3.6 but couldn’t

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found