Skip to content

Autoprofile fails if submodule contains a .py with with an invalid module name. #395

Closed

Description

@Erotemic

I saved a file "foo-v1.py" into a utils directory and that broke autoprofiling, as it recognized it as a Python file and tried to import it, but "foo-v1" is not a valid module name. A fix would be checking that the file has valid module name characters. We could also skip importing files that fail to import.

Activity

  1. TTsangSC commented on Aug 30, 2025

    @TTsangSC
    Collaborator

    Sorry for the radio silence on this issue, was busy with #391 last week and my vacation the week before.

    Frankly I was caught by surprise, I'd have guessed that invalid module names should've been excluded by default. But it seems that both static analysis and pkgutil are all too happy to leave them be, and so we end up with stuff like

    try:
        import mypkg.utils.foo-v1  # This is syntactically invalid
    except ImportError:  # This normally handles failed imports
        ...
    else:  # This handles the profiling of module contents upon successful import
        ...

    in the module written by line_profiler.autoprofile.eager_preimports.write_eager_import_module().

    I've since pushed a fix at #397.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions