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

Add ability to list sizes of dependencies #391

Open
notexactlyawe opened this issue Jul 8, 2024 · 2 comments
Open

Add ability to list sizes of dependencies #391

notexactlyawe opened this issue Jul 8, 2024 · 2 comments

Comments

@notexactlyawe
Copy link

Describe the feature

Searching for duplicate issues, I found that #108 was closed for lack of a specific use case.

I would like to use pipdeptree to help optimize the size of a pyinstaller executable. It's currently great for seeing what dependencies are present in a project, but it doesn't show the sizes of those dependencies.

Size, to me, is anything that helps me determine the "cost" of a dependency in my final executable. I would like to use it to answer questions like, "Which dependency should I try to remove first to get the size of my executable down?".

A potential metric for this might be wheel size. pip seems to know about this at install time:

...
Collecting virtualenv>=20.25 (from tox->-r .\requirements-dev.txt (line 6))
  Using cached <some pypi>/virtualenv-20.26.3-py3-none-any.whl (5.7 MB)
...
@notexactlyawe notexactlyawe added enhancement tobeconfirmed To be confirmed after more investigation labels Jul 8, 2024
@kemzeb
Copy link
Collaborator

kemzeb commented Jul 11, 2024

This has been requested before and I too believe it would be useful.

One approach I see right now is we can:

  1. Try using the RECORD file in the package metadata to determine the package size (see here for details)
  2. If we fail to find it in the metadata directory (since its optional), try to recurse the package's source directory and snatch the file sizes
  3. If we fail to find the package's source directory, we can't determine the package size so consider the size to be "Unknown"

@kemzeb kemzeb added help-wanted and removed tobeconfirmed To be confirmed after more investigation labels Jul 11, 2024
@kylepollina
Copy link

kylepollina commented Aug 18, 2024

I was playing around and was able to grab the size for the package using the following approach

# in src/pipdeptree/_models/package.py

...
import os
import pkg_resources
import importlib

def get_directory_size(directory) -> int:
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(directory):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size


def get_package_location(package_name) -> Optional[str]:
    try:
        # Try to get the package location using pkg_resources
        package = pkg_resources.get_distribution(package_name)
        return package.location
    except pkg_resources.DistributionNotFound:
        pass

    # Try to get the package location using importlib
    spec = importlib.util.find_spec(package_name)
    if spec is not None:
        return spec.origin

    # If all else fails, return None
    return None

...


class Package(ABC):
    """Abstract class for wrappers around objects that pip returns."""

    UNKNOWN_LICENSE_STR = "(Unknown license)"

    def __init__(self, project_name: str) -> None:
        self.project_name = project_name
        self.key = canonicalize_name(project_name)
        self.location = get_package_location(self.key)
        self.size = get_directory_size(f"{self.location}/{self.key}")

This sets self.size to be the total size in bytes for the package. I don't know how you would approach this, and I wouldn't know how to best render it. Other problems that I would love to have solved is, what is the total memory size of this package and all dependent children? I couldn't figure out how to calculate that

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

No branches or pull requests

3 participants