1212# but the site module sorts the files before processing them,
1313# and that hasn't changed recently.)
1414
15- import pkgutil
15+ import importlib
1616import sys
1717
1818logged = False
19+ BOLD = "\033 [1m"
20+ RESET = "\033 [0m"
1921
2022
2123def apply ():
@@ -25,37 +27,26 @@ def apply():
2527 global logged
2628 if not logged :
2729 print (
28- "🐎 This Python uses horse-with-no-namespace "
29- "to make pkg_resources namespace packages compatible "
30- "with PEP 420 namespace packages." ,
30+ f "🐎 This Python ( { BOLD } { sys . executable } { RESET } ) uses "
31+ "horse-with-no-namespace to make pkg_resources namespace "
32+ "packages compatible with PEP 420 namespace packages." ,
3133 file = sys .stderr ,
3234 )
3335 logged = True
3436
35- # Only patch pkg_resources if it is installed...
36- # (To do: at some point pkg_resources will be removed from setuptools.
37- # Then we might have to add our own fake module.)
38- try :
39- import pkg_resources
40- except ImportError :
41- pass
42- else :
43- # Patch pkg_resources.declare_namespace
44- # to update __path__ using pkgutil.extend_path instead
45- def declare_namespace (packageName ):
46- parent_locals = sys ._getframe (1 ).f_locals
47- # Sometimes declare_namespace is called from pkg_resources itself;
48- # then there is no __path__ which needs to be updated.
49- if "__path__" in parent_locals :
50- parent_locals ["__path__" ] = pkgutil .extend_path (
51- parent_locals ["__path__" ], packageName
52- )
53-
54- pkg_resources .declare_namespace = declare_namespace
55-
5637 # Remove existing namespace package modules that were already created
5738 # by other .pth files, possibly with an incomplete __path__
5839 for name , module in list (sys .modules .items ()):
5940 loader = getattr (module , "__loader__" , None )
6041 if loader and loader .__class__ .__name__ == "NamespaceLoader" :
6142 del sys .modules [name ]
43+
44+ # We want to patch pkg_resources.declare_namespace,
45+ # but we don't want to import it too early,
46+ # because that would initialize the pkg_resources working set
47+ # before sys.path is finalized.
48+ # So, let's put a fake pkg_resources module is sys.modules,
49+ # which will replace itself once it is accessed.
50+ sys .modules ["pkg_resources" ] = importlib .import_module (
51+ "horse_with_no_namespace.pkg_resources"
52+ )
0 commit comments