[Python] Python determine Pypi pakcage name from mdule

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I've installed a package google-cloud-tasks which I'm able to import as follows:

    >>> import google.cloud.tasks_v2


    >>> google.cloud.tasks_v2
    <module 'google.cloud.tasks_v2' from '/home/jackevans/code/example/.venv/lib/python3.12/site-packages/google/cloud/tasks_v2/__init__.py'>


    Is there a way to map this module back to the original package name google-cloud-tasks that was used to install?

    Another example here would be beautifulsoup4 (pip install beautifulsoup4), which is imported by a different name bs4:

    >>> import bs4
    >>> bs4
    <module 'bs4' from '/home/jackevans/code/example/.venv/lib/python3.12/site-packages/bs4/__init__.py'>
    >>> bs4.__package__
    'bs4'


    I can see the bs4 mapping is available in importlib.metadata.packages_distributions

    >>> importlib.metadata.packages_distributions()['bs4']
    ['beautifulsoup4']


    but no such mapping exists for google.cloud.tasks_v2.

    I'm beginning to think what I'm trying to do isn't possible at all due to the nature of Python packaging, but hoping someone can prove me wrong.

    The motivation here is to be able (via static analysis) determine what packages are used where in my codebase

    Appreciate the downvotes thanks.

    Continue reading...

Compartilhe esta Página