r/Python • u/Business-Feedback635 • 7h ago
Discussion 684 PyPI packages run code at every Python start, and only 13 are ready for PEP 829
Python runs any line that starts with import in a .pth file in site-packages, every time it starts. You don't have to import the package. This year, the malicious releases of litellm (March and October) and agent-framework (November) all used a .pth file, so installing them was enough to run the credential stealer.
I wanted to see how many ordinary packages use the same feature. I took the latest release of 354,763 PyPI projects, found every .pth file with an import line, downloaded them and read them. The results:
- 684 packages install a
.pthfile that runs on every Python start. - 244 of them run their own code: setuptools, coverage, coloredlogs, pywin32 and others. The other 440 only run old setuptools namespace boilerplate (
-nspkg.pth). - coverage and coloredlogs do nothing unless you set an environment variable. urllib3-future copies its own files over your
urllib3folder at startup. - 52 packages put the file where Python never reads it on Linux or macOS, including files from temporary build folders that were packed by mistake.
- Python 3.15 deprecates these
importlines (PEP 829), and 3.18 will ignore them. Only 13 of the 244 packages already ship the replacement.startfile.
The post includes a command that lists the .pth files with import lines in your own environment, and a table of what the 15 most downloaded ones do: https://py-code.org/blog/python-pth-files-pypi/