6161# Enable per user site-packages directory
6262# set it to False to disable the feature or True to force the feature
6363ENABLE_USER_SITE = None
64+
6465# for distutils.commands.install
66+ # These values are initialized by the getuserbase() and getusersitepackages()
67+ # functions, through the main() function when Python starts.
6568USER_SITE = None
6669USER_BASE = None
6770
@@ -205,49 +208,75 @@ def check_enableusersite():
205208
206209 return True
207210
211+ def getuserbase ():
212+ """Returns the `user base` directory path.
208213
209- def addusersitepackages (known_paths ):
210- """Add a per user site-package to sys.path
211-
212- Each user has its own python directory with site-packages in the
213- home directory.
214-
215- USER_BASE is the root directory for all Python versions
216-
217- USER_SITE is the user specific site-packages directory
218-
219- USER_SITE/.. can be used for data.
214+ The `user base` directory can be used to store data. If the global
215+ variable ``USER_BASE`` is not initialized yet, this function will also set
216+ it.
220217 """
221- global USER_BASE , USER_SITE , ENABLE_USER_SITE
218+ global USER_BASE
219+ if USER_BASE is not None :
220+ return USER_BASE
221+
222222 env_base = os .environ .get ("PYTHONUSERBASE" , None )
223223
224224 def joinuser (* args ):
225225 return os .path .expanduser (os .path .join (* args ))
226226
227- #if sys.platform in ('os2emx', 'riscos'):
228- # # Don't know what to put here
229- # USER_BASE = ''
230- # USER_SITE = ''
227+ # what about 'os2emx', 'riscos' ?
231228 if os .name == "nt" :
232229 base = os .environ .get ("APPDATA" ) or "~"
233230 USER_BASE = env_base if env_base else joinuser (base , "Python" )
234- USER_SITE = os .path .join (USER_BASE ,
235- "Python" + sys .version [0 ] + sys .version [2 ],
236- "site-packages" )
237231 else :
238232 USER_BASE = env_base if env_base else joinuser ("~" , ".local" )
239- USER_SITE = os .path .join (USER_BASE , "lib" ,
240- "python" + sys .version [:3 ],
233+
234+ return USER_BASE
235+
236+ def getusersitepackages ():
237+ """Returns the user-specific site-packages directory path.
238+
239+ If the global variable ``USER_SITE`` is not initialized yet, this
240+ function will also set it.
241+ """
242+ global USER_SITE
243+ user_base = getuserbase () # this will also set USER_BASE
244+
245+ if USER_SITE is not None :
246+ return USER_SITE
247+
248+ if os .name == "nt" :
249+ USER_SITE = os .path .join (user_base , "Python" + sys .version [0 ] +
250+ sys .version [2 ], "site-packages" )
251+ else :
252+ USER_SITE = os .path .join (user_base , "lib" , "python" + sys .version [:3 ],
241253 "site-packages" )
242254
243- if ENABLE_USER_SITE and os .path .isdir (USER_SITE ):
244- addsitedir (USER_SITE , known_paths )
255+ return USER_SITE
256+
257+ def addusersitepackages (known_paths ):
258+ """Add a per user site-package to sys.path
259+
260+ Each user has its own python directory with site-packages in the
261+ home directory.
262+ """
263+ # get the per user site-package path
264+ # this call will also make sure USER_BASE and USER_SITE are set
265+ user_site = getusersitepackages ()
266+
267+ if ENABLE_USER_SITE and os .path .isdir (user_site ):
268+ addsitedir (user_site , known_paths )
245269 return known_paths
246270
271+ def getsitepackages ():
272+ """Returns a list containing all global site-packages directories
273+ (and possibly site-python).
247274
248- def addsitepackages (known_paths ):
249- """Add site-packages (and possibly site-python) to sys.path"""
250- sitedirs = []
275+ For each directory present in the global ``PREFIXES``, this function
276+ will find its `site-packages` subdirectory depending on the system
277+ environment, and will return a list of full paths.
278+ """
279+ sitepackages = []
251280 seen = []
252281
253282 for prefix in PREFIXES :
@@ -256,35 +285,36 @@ def addsitepackages(known_paths):
256285 seen .append (prefix )
257286
258287 if sys .platform in ('os2emx' , 'riscos' ):
259- sitedirs .append (os .path .join (prefix , "Lib" , "site-packages" ))
288+ sitepackages .append (os .path .join (prefix , "Lib" , "site-packages" ))
260289 elif os .sep == '/' :
261- sitedirs .append (os .path .join (prefix , "lib" ,
290+ sitepackages .append (os .path .join (prefix , "lib" ,
262291 "python" + sys .version [:3 ],
263292 "site-packages" ))
264- sitedirs .append (os .path .join (prefix , "lib" , "site-python" ))
293+ sitepackages .append (os .path .join (prefix , "lib" , "site-python" ))
265294 else :
266- sitedirs .append (prefix )
267- sitedirs .append (os .path .join (prefix , "lib" , "site-packages" ))
268-
295+ sitepackages .append (prefix )
296+ sitepackages .append (os .path .join (prefix , "lib" , "site-packages" ))
269297 if sys .platform == "darwin" :
270298 # for framework builds *only* we add the standard Apple
271299 # locations.
272300 if 'Python.framework' in prefix :
273- sitedirs .append (
301+ sitepackages .append (
274302 os .path .expanduser (
275303 os .path .join ("~" , "Library" , "Python" ,
276304 sys .version [:3 ], "site-packages" )))
277- sitedirs .append (
305+ sitepackages .append (
278306 os .path .join ("/Library" , "Python" ,
279307 sys .version [:3 ], "site-packages" ))
308+ return sitepackages
280309
281- for sitedir in sitedirs :
310+ def addsitepackages (known_paths ):
311+ """Add site-packages (and possibly site-python) to sys.path"""
312+ for sitedir in getsitepackages ():
282313 if os .path .isdir (sitedir ):
283314 addsitedir (sitedir , known_paths )
284315
285316 return known_paths
286317
287-
288318def setBEGINLIBPATH ():
289319 """The OS/2 EMX port has optional extension modules that do double duty
290320 as DLLs (and must use the .DLL file extension) for other extensions.
0 commit comments