@@ -124,7 +124,7 @@ def removeduppaths():
124124 # if they only differ in case); turn relative paths into absolute
125125 # paths.
126126 dir , dircase = makepath (dir )
127- if not dircase in known_paths :
127+ if dircase not in known_paths :
128128 L .append (dir )
129129 known_paths .add (dircase )
130130 sys .path [:] = L
@@ -234,6 +234,46 @@ def check_enableusersite():
234234
235235 return True
236236
237+
238+ # NOTE: sysconfig and it's dependencies are relatively large but site module
239+ # needs very limited part of them.
240+ # To speedup startup time, we have copy of them.
241+ #
242+ # See https://bugs.python.org/issue29585
243+
244+ # Copy of sysconfig._getuserbase()
245+ def _getuserbase ():
246+ env_base = os .environ .get ("PYTHONUSERBASE" , None )
247+ if env_base :
248+ return env_base
249+
250+ def joinuser (* args ):
251+ return os .path .expanduser (os .path .join (* args ))
252+
253+ if os .name == "nt" :
254+ base = os .environ .get ("APPDATA" ) or "~"
255+ return joinuser (base , "Python" )
256+
257+ if sys .platform == "darwin" and sys ._framework :
258+ return joinuser ("~" , "Library" , sys ._framework ,
259+ "%d.%d" % sys .version_info [:2 ])
260+
261+ return joinuser ("~" , ".local" )
262+
263+
264+ # Same to sysconfig.get_path('purelib', os.name+'_user')
265+ def _get_path (userbase ):
266+ version = sys .version_info
267+
268+ if os .name == 'nt' :
269+ return f'{ userbase } /Python{ version [0 ]} { version [1 ]} /site-packages'
270+
271+ if sys .platform == 'darwin' and sys ._framework :
272+ return f'{ userbase } /lib/python/site-packages'
273+
274+ return f'{ userbase } /lib/python{ version [0 ]} .{ version [1 ]} /site-packages'
275+
276+
237277def getuserbase ():
238278 """Returns the `user base` directory path.
239279
@@ -242,33 +282,23 @@ def getuserbase():
242282 it.
243283 """
244284 global USER_BASE
245- if USER_BASE is not None :
246- return USER_BASE
247- from sysconfig import get_config_var
248- USER_BASE = get_config_var ('userbase' )
285+ if USER_BASE is None :
286+ USER_BASE = _getuserbase ()
249287 return USER_BASE
250288
289+
251290def getusersitepackages ():
252291 """Returns the user-specific site-packages directory path.
253292
254293 If the global variable ``USER_SITE`` is not initialized yet, this
255294 function will also set it.
256295 """
257296 global USER_SITE
258- user_base = getuserbase () # this will also set USER_BASE
259-
260- if USER_SITE is not None :
261- return USER_SITE
262-
263- from sysconfig import get_path
297+ userbase = getuserbase () # this will also set USER_BASE
264298
265- if sys .platform == 'darwin' :
266- from sysconfig import get_config_var
267- if get_config_var ('PYTHONFRAMEWORK' ):
268- USER_SITE = get_path ('purelib' , 'osx_framework_user' )
269- return USER_SITE
299+ if USER_SITE is None :
300+ USER_SITE = _get_path (userbase )
270301
271- USER_SITE = get_path ('purelib' , '%s_user' % os .name )
272302 return USER_SITE
273303
274304def addusersitepackages (known_paths ):
@@ -310,15 +340,11 @@ def getsitepackages(prefixes=None):
310340 else :
311341 sitepackages .append (prefix )
312342 sitepackages .append (os .path .join (prefix , "lib" , "site-packages" ))
313- if sys .platform == "darwin" :
314- # for framework builds *only* we add the standard Apple
315- # locations.
316- from sysconfig import get_config_var
317- framework = get_config_var ("PYTHONFRAMEWORK" )
318- if framework :
319- sitepackages .append (
320- os .path .join ("/Library" , framework ,
321- '%d.%d' % sys .version_info [:2 ], "site-packages" ))
343+ # for framework builds *only* we add the standard Apple locations.
344+ if sys .platform == "darwin" and sys ._framework :
345+ sitepackages .append (
346+ os .path .join ("/Library" , framework ,
347+ '%d.%d' % sys .version_info [:2 ], "site-packages" ))
322348 return sitepackages
323349
324350def addsitepackages (known_paths , prefixes = None ):
0 commit comments