Skip to content

Don't import the world when pip starts up #6692

@cjerdonek

Description

@cjerdonek

Currently, when pip starts up, it basically imports its entire code base and all vendored libraries and modules it depends on. This issue is for pip only to import what it needs.

It looks like a large part of this can be done pretty simply by changing commands/__init__.py not to import all the command classes:

commands_order = [
InstallCommand,
DownloadCommand,
UninstallCommand,
FreezeCommand,
ListCommand,
ShowCommand,
CheckCommand,
ConfigurationCommand,
SearchCommand,
WheelCommand,
HashCommand,
CompletionCommand,
DebugCommand,
HelpCommand,
] # type: List[Type[Command]]

And then import only the command class it needs after parsing the command name:

command = commands_dict[cmd_name](isolated=("--isolated" in cmd_args))

This will require storing a mapping of command name to command class location and command "summary."

It looks like this would reduce the startup time by ~20% when I tried a quick test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    auto-lockedOutdated issues that have been locked by automationtype: enhancementImprovements to functionalitytype: refactorRefactoring code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions