diff --git a/news/13165.feature.rst b/news/13165.feature.rst new file mode 100644 index 00000000000..e21ddac50ad --- /dev/null +++ b/news/13165.feature.rst @@ -0,0 +1 @@ +Speed up small CLI tools by removing ``import re`` from the executable template. diff --git a/src/pip/_internal/operations/install/wheel.py b/src/pip/_internal/operations/install/wheel.py index aef42aa9eef..290f5e72f51 100644 --- a/src/pip/_internal/operations/install/wheel.py +++ b/src/pip/_internal/operations/install/wheel.py @@ -11,6 +11,7 @@ import re import shutil import sys +import textwrap import warnings from base64 import urlsafe_b64encode from email.message import Message @@ -412,6 +413,19 @@ def _raise_for_invalid_entrypoint(specification: str) -> None: class PipScriptMaker(ScriptMaker): + # Override distlib's default script template with one that + # doesn't import `re` module, allowing scripts to load faster. + script_template = textwrap.dedent( + """\ + import sys + from %(module)s import %(import_name)s + if __name__ == '__main__': + if sys.argv[0].endswith('.exe'): + sys.argv[0] = sys.argv[0][:-4] + sys.exit(%(func)s()) +""" + ) + def make( self, specification: str, options: Optional[Dict[str, Any]] = None ) -> List[str]: