Skip to content
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

Python standard library Modulefinder not working with editable installed packages #1077

Open
micdet-delen opened this issue May 24, 2024 · 1 comment

Comments

@micdet-delen
Copy link

micdet-delen commented May 24, 2024

Hello,

This is related to the Python images: https://github.com/devcontainers/images/tree/main/src/python

When having a Python package installed as editable (pip install -e ...) the standard library Modulefinder cannot find the package.

See also: thebjorn/pydeps#222

I'll try to recap/provide a how-to-reproduce:

  1. install a (custom) package as editable: pip install -e mypackage
  2. have it imported by a simple test file: echo "import mypackage" > test.py
  3. verify that the package can indeed be imported by python: python test.py (should not throw import errors)
  4. have modulefinder (not) find the package:
$ python -m modulefinder test.py 

  Name                      File
  ----                      ----
m __main__                  test.py

Missing modules:
? mypackage imported from __main__

This does not occur when using the Debian provided Python version.

In the latest version that I've tested (3.12-bookworm) this also does not occur when installing the package normally.

In a previous version however (3.11-bullseye) installing it normally threw a stacktrace, ending in:

massive stacktrace before this
  File "/usr/local/lib/python3.11/modulefinder.py", line 308, in import_module
    fp, pathname, stuff = self.find_module(partname,
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/modulefinder.py", line 489, in find_module
    return _find_module(name, path)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/modulefinder.py", line 69, in _find_module
    if spec.loader.is_package(name):
       ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'is_package'
@samruddhikhandale
Copy link
Member

This issue seems to be related to how Python's modulefinder handles editable packages installed via pip install -e.

In Python, an editable install (pip install -e) creates a link in your site-packages directory to your project's source code directory. This allows you to make changes to your package without having to reinstall it. However, modulefinder might not be able to find these packages because it's looking for actual package files, not links.

The error message AttributeError: 'NoneType' object has no attribute 'is_package' suggests that modulefinder is trying to call the is_package method on a None object, which likely means it couldn't find the package you're trying to import.

As a workaround, you could try to install the package normally (without -e) before running modulefinder. If you need to make changes to the package, you can reinstall it after making your changes.

If this doesn't work, you might want to consider using a different tool for finding modules, or filing a bug report with the Python team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants