pypa / setuptools

Official project repository for the Setuptools build system
https://pypi.org/project/setuptools/
MIT License
2.5k stars 1.19k forks source link

[BUG] Importing `setuptools` is slow and takes nearly half a second #3441

Open jiasli opened 2 years ago

jiasli commented 2 years ago

setuptools version

63.1.0

Python version

3.10.5

OS

Windows

Additional environment information

No response

Description

Importing setuptools is slow and takes nearly half a second. This imposes significant performance impact on CLI applications (https://github.com/Azure/azure-cli/issues/23177).

Expected behavior

Importing setuptools should be fast.

How to Reproduce

import setuptools

Output

python -X importtime -c "import setuptools" 2>perf.log ; python -m tuna .\perf.log

image

Without setuptools, importing distutils is pretty fast:

python -X importtime -c "import distutils" 2>perf.log ; python -m tuna .\perf.log

image

yonzhan commented 2 years ago

Good catch!

abravalheri commented 2 years ago

Hi @jiasli thank you very much for the study. Would you like to suggest any PRs tackling this issue?

For Python <= 3.12, _distutils_hack is still necessary, but probably for Python >= 3.12 that can be an easy gain.

I think we can also delay the import of unittest.mock in distutils/_msvccompiler.py, but that would have to be addressed as a PR to the pypa/distutils repository.

Currently the plan is to remove pkg_resources and we have been working continuously on this direction (this should provide some performance improvements). Unfortunately, it is not something we can modify quickly.

jiasli commented 2 years ago

Thanks lot @abravalheri for the analysis.

Would you like to suggest any PRs tackling this issue?

I would certainly like to, but sadly I am not an expert on the delicate implementation of Python infrastructures. 🤣

Currently the plan is to remove pkg_resources and we have been working continuously on this direction (this should provide some performance improvements). Unfortunately, it is not something we can modify quickly.

This will certainly make setuptools significantly faster. We spent lots of effort getting rid of pkg_resources (https://github.com/Azure/azure-cli/pull/14372, https://github.com/Azure/azure-cli/pull/14905), but now it's back via setuptools. 😂

If setuptools can be as fast as the old distutils, that would really be helpful!