3 ms·
Python .pth files are horrific. Here's an actual .pth file I was dealing with the other day (from Google Cloud Storage) which completely prevents you from over
by rwmj 2y ago
Python .pth files are horrific. Here's an actual .pth file I was dealing with the other day (from Google Cloud Storage) which completely prevents you from overriding the module using PYTHONPATH:
import sys, types, os;has_mfs = sys.version_info > (3, 5);p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('google',));importlib = has_mfs and __import__('importlib.util');has_mfs and __import__('importlib.machinery');m = has_mfs and sys.modules.setdefault('google', importlib.util.module_from_spec(importlib.machinery.PathFinder.find_spec('google', [os.path.dirname(p)])));m = m or sys.modules.setdefault('google', types.ModuleType('google'));mp = (m or []) and m.__dict__.setdefault('__path__',[]);(p not in mp) and mp.append(p)
- talideon 2y agoIf .pth files are the worst thing you can find to complain about, Python's doing pretty well. That horrific .pth file in question is better placed as the feet of its creators than the mechanism itself.
- rwmj 2y agoThe fact they considered allowing executable code in path lookups shows a certain attitude.
- oefrha 2y agoIt shows that the language is highly dynamic and you can patch anything? The .pth mechanism allows the party controlling the Python installation (site) to run some init code before any user code, basically an rc mechanism. Nothing more, nothing radical. Maybe you’re unhappy with the dynamism, in which case your complaint is misplaced.
- talideon 2y agoI searched all my machines and could only find simple ones, all related to some legacy crud for setuptools, one of which was a way to get eggs (which nobody should be using these days) to work. They're barely a thing these days and mostly a relic from before the end of the Great Setuptools Stagnation and the site package all but discourages their use.
- talideon 2y agoYou'll also be happy to learn that there's an issue open to deprecate them: https://github.com/python/cpython/issues/78125 https://github.com/python/cpython/issues/78125 It's been open since 2018 though. You might find it worth a read.
- rwmj 2y agoYup, subscribed to that one already!
- est 2y agoI agree those particular .pth files were horrific. But python package made by Google were noturously bad. Its awefulness dates back to the GAE days.