2 /* Return the initial module search path. */
3 /* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */
5 /* ----------------------------------------------------------------
6 PATH RULES FOR WINDOWS:
7 This describes how sys.path is formed on Windows. It describes the
8 functionality, not the implementation (ie, the order in which these
9 are actually fetched is different)
11 * Python always adds an empty entry at the start, which corresponds
12 to the current directory.
14 * If the PYTHONPATH env. var. exists, it's entries are added next.
16 * We look in the registry for "application paths" - that is, sub-keys
17 under the main PythonPath registry key. These are added next (the
18 order of sub-key processing is undefined).
19 HKEY_CURRENT_USER is searched and added first.
20 HKEY_LOCAL_MACHINE is searched and added next.
21 (Note that all known installers only use HKLM, so HKCU is typically
24 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
25 is set, we believe it. Otherwise, we use the path of our host .EXE's
26 to try and locate our "landmark" (lib\\os.py) and deduce our home.
27 - If we DO have a Python Home: The relevant sub-directories (Lib,
28 plat-win, lib-tk, etc) are based on the Python Home
29 - If we DO NOT have a Python Home, the core Python Path is
30 loaded from the registry. This is the main PythonPath key,
31 and both HKLM and HKCU are combined to form the path)
33 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
34 specified, and can't locate any Registry entries (ie, we have _nothing_
35 we can assume is a good path), a default path with relative entries is
36 used (eg. .\Lib;.\plat-win, etc)
39 The end result of all this is:
40 * When running python.exe, or any other .exe in the main Python directory
41 (either an installed version, or directly from the PCbuild directory),
42 the core path is deduced, and the core paths in the registry are
43 ignored. Other "application paths" in the registry are always read.
45 * When Python is hosted in another exe (different directory, embedded via
46 COM, etc), the Python Home will not be deduced, so the core path from
47 the registry is used. Other "application paths" in the registry are
50 * If Python can't find its home and there is no registry (eg, frozen
51 exe, some very strange installation setup) you get a path with
52 some default, but relative, paths.
54 ---------------------------------------------------------------- */
65 #include <sys/types.h>
71 #endif /* HAVE_UNISTD_H */
73 /* Search in some common locations for the associated Python libraries.
75 * Py_GetPath() tries to return a sensible Python module search path.
77 * The approach is an adaptation for Windows of the strategy used in
78 * ../Modules/getpath.c; it uses the Windows Registry as one of its
79 * information sources.
83 #define LANDMARK "lib\\os.py"
86 static char prefix
[MAXPATHLEN
+1];
87 static char progpath
[MAXPATHLEN
+1];
88 static char *module_search_path
= NULL
;
92 is_sep(char ch
) /* determine if "ch" is a separator character */
95 return ch
== SEP
|| ch
== ALTSEP
;
101 /* assumes 'dir' null terminated in bounds. Never writes
102 beyond existing terminator.
107 size_t i
= strlen(dir
);
108 while (i
> 0 && !is_sep(dir
[i
]))
115 exists(char *filename
)
118 return stat(filename
, &buf
) == 0;
121 /* Assumes 'filename' MAXPATHLEN+1 bytes long -
122 may extend 'filename' by one character.
125 ismodule(char *filename
) /* Is module -- check for .pyc/.pyo too */
127 if (exists(filename
))
130 /* Check for the compiled version of prefix. */
131 if (strlen(filename
) < MAXPATHLEN
) {
132 strcat(filename
, Py_OptimizeFlag
? "o" : "c");
133 if (exists(filename
))
139 /* guarantees buffer will never overflow MAXPATHLEN+1 bytes */
141 join(char *buffer
, char *stuff
)
144 if (is_sep(stuff
[0]))
148 if (n
> 0 && !is_sep(buffer
[n
-1]) && n
< MAXPATHLEN
)
152 if (n
+ k
> MAXPATHLEN
)
154 strncpy(buffer
+n
, stuff
, k
);
158 /* gotlandmark only called by search_for_prefix, which ensures
159 'prefix' is null terminated in bounds. join() ensures
160 'landmark' can not overflow prefix if too long.
163 gotlandmark(char *landmark
)
168 join(prefix
, landmark
);
169 ok
= ismodule(prefix
);
174 /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
175 assumption provided by only caller, calculate_path() */
177 search_for_prefix(char *argv0_path
, char *landmark
)
179 /* Search from argv0_path, until landmark is found */
180 strcpy(prefix
, argv0_path
);
182 if (gotlandmark(landmark
))
191 /* a string loaded from the DLL at startup.*/
192 extern const char *PyWin_DLLVersionString
;
195 /* Load a PYTHONPATH value from the registry.
196 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
198 Works in both Unicode and 8bit environments. Only uses the
199 Ex family of functions so it also works with Windows CE.
201 Returns NULL, or a pointer that should be freed.
203 XXX - this code is pretty strange, as it used to also
204 work on Win16, where the buffer sizes werent available
205 in advance. It could be simplied now Win16/Win32s is dead!
209 getpythonregpath(HKEY keyBase
, int skipcore
)
216 TCHAR
*dataBuf
= NULL
;
217 static const TCHAR keyPrefix
[] = _T("Software\\Python\\PythonCore\\");
218 static const TCHAR keySuffix
[] = _T("\\PythonPath");
221 TCHAR
*keyBuf
= NULL
;
223 TCHAR
**ppPaths
= NULL
;
225 /* Tried to use sysget("winver") but here is too early :-( */
226 versionLen
= _tcslen(PyWin_DLLVersionString
);
227 /* Space for all the chars, plus one \0 */
228 keyBuf
= keyBufPtr
= malloc(sizeof(keyPrefix
) +
229 sizeof(TCHAR
)*(versionLen
-1) +
231 if (keyBuf
==NULL
) goto done
;
233 memcpy(keyBufPtr
, keyPrefix
, sizeof(keyPrefix
)-sizeof(TCHAR
));
234 keyBufPtr
+= sizeof(keyPrefix
)/sizeof(TCHAR
) - 1;
235 memcpy(keyBufPtr
, PyWin_DLLVersionString
, versionLen
* sizeof(TCHAR
));
236 keyBufPtr
+= versionLen
;
237 /* NULL comes with this one! */
238 memcpy(keyBufPtr
, keySuffix
, sizeof(keySuffix
));
239 /* Open the root Python key */
240 rc
=RegOpenKeyEx(keyBase
,
245 if (rc
!=ERROR_SUCCESS
) goto done
;
246 /* Find out how big our core buffer is, and how many subkeys we have */
247 rc
= RegQueryInfoKey(newKey
, NULL
, NULL
, NULL
, &numKeys
, NULL
, NULL
,
248 NULL
, NULL
, &dataSize
, NULL
, NULL
);
249 if (rc
!=ERROR_SUCCESS
) goto done
;
250 if (skipcore
) dataSize
= 0; /* Only count core ones if we want them! */
251 /* Allocate a temp array of char buffers, so we only need to loop
252 reading the registry once
254 ppPaths
= malloc( sizeof(TCHAR
*) * numKeys
);
255 if (ppPaths
==NULL
) goto done
;
256 memset(ppPaths
, 0, sizeof(TCHAR
*) * numKeys
);
257 /* Loop over all subkeys, allocating a temp sub-buffer. */
258 for(index
=0;index
<numKeys
;index
++) {
259 TCHAR keyBuf
[MAX_PATH
+1];
261 DWORD reqdSize
= MAX_PATH
+1;
262 /* Get the sub-key name */
263 DWORD rc
= RegEnumKeyEx(newKey
, index
, keyBuf
, &reqdSize
,
264 NULL
, NULL
, NULL
, NULL
);
265 if (rc
!=ERROR_SUCCESS
) goto done
;
266 /* Open the sub-key */
267 rc
=RegOpenKeyEx(newKey
,
272 if (rc
!=ERROR_SUCCESS
) goto done
;
273 /* Find the value of the buffer size, malloc, then read it */
274 RegQueryValueEx(subKey
, NULL
, 0, NULL
, NULL
, &reqdSize
);
276 ppPaths
[index
] = malloc(reqdSize
);
277 if (ppPaths
[index
]) {
278 RegQueryValueEx(subKey
, NULL
, 0, NULL
,
279 (LPBYTE
)ppPaths
[index
],
281 dataSize
+= reqdSize
+ 1; /* 1 for the ";" */
286 /* original datasize from RegQueryInfo doesn't include the \0 */
287 dataBuf
= malloc((dataSize
+1) * sizeof(TCHAR
));
289 TCHAR
*szCur
= dataBuf
;
290 DWORD reqdSize
= dataSize
;
291 /* Copy our collected strings */
292 for (index
=0;index
<numKeys
;index
++) {
294 *(szCur
++) = _T(';');
297 if (ppPaths
[index
]) {
298 int len
= _tcslen(ppPaths
[index
]);
299 _tcsncpy(szCur
, ppPaths
[index
], len
);
307 /* If we have no values, we dont need a ';' */
309 *(szCur
++) = _T(';');
312 /* Now append the core path entries -
313 this will include the NULL
315 rc
= RegQueryValueEx(newKey
, NULL
, 0, NULL
,
316 (LPBYTE
)szCur
, &dataSize
);
318 /* And set the result - caller must free
319 If MBCS, it is fine as is. If Unicode, allocate new
323 retval
= (char *)malloc(reqdSize
+1);
325 WideCharToMultiByte(CP_ACP
, 0,
326 dataBuf
, -1, /* source */
327 retval
, dataSize
+1, /* dest */
335 /* Loop freeing my temp buffers */
337 for(index
=0;index
<numKeys
;index
++)
338 if (ppPaths
[index
]) free(ppPaths
[index
]);
347 #endif /* MS_WIN32 */
352 extern char *Py_GetProgramName(void);
353 char *path
= getenv("PATH");
354 char *prog
= Py_GetProgramName();
358 WCHAR wprogpath
[MAXPATHLEN
+1];
359 /* Windows documents that GetModuleFileName() will "truncate",
360 but makes no mention of the null terminator. Play it safe.
361 PLUS Windows itself defines MAX_PATH as the same, but anyway...
363 wprogpath
[MAXPATHLEN
]=_T('\0')';
364 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
365 WideCharToMultiByte(CP_ACP, 0,
367 progpath, MAXPATHLEN+1,
372 /* static init of progpath ensures final char remains \0 */
373 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
377 if (prog == NULL || *prog == '\
0')
380 /* If there is no slash in the argv0 path, then we have to
381 * assume python is on the user's $PATH, since there's no
382 * other way to find a directory to start the search from. If
383 * $PATH isn't exported, you lose.
386 if (strchr(prog
, SEP
) || strchr(prog
, ALTSEP
))
388 if (strchr(prog
, SEP
))
390 strncpy(progpath
, prog
, MAXPATHLEN
);
393 char *delim
= strchr(path
, DELIM
);
396 size_t len
= delim
- path
;
397 /* ensure we can't overwrite buffer */
398 len
= min(MAXPATHLEN
,len
);
399 strncpy(progpath
, path
, len
);
400 *(progpath
+ len
) = '\0';
403 strncpy(progpath
, path
, MAXPATHLEN
);
405 /* join() is safe for MAXPATHLEN+1 size buffer */
406 join(progpath
, prog
);
407 if (exists(progpath
))
424 char argv0_path
[MAXPATHLEN
+1];
427 char *pythonhome
= Py_GetPythonHome();
428 char *envpath
= Py_GETENV("PYTHONPATH");
431 int skiphome
, skipdefault
;
432 char *machinepath
= NULL
;
433 char *userpath
= NULL
;
437 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
438 strcpy(argv0_path
, progpath
);
440 if (pythonhome
== NULL
|| *pythonhome
== '\0') {
441 if (search_for_prefix(argv0_path
, LANDMARK
))
447 strncpy(prefix
, pythonhome
, MAXPATHLEN
);
449 if (envpath
&& *envpath
== '\0')
454 skiphome
= pythonhome
==NULL
? 0 : 1;
455 machinepath
= getpythonregpath(HKEY_LOCAL_MACHINE
, skiphome
);
456 userpath
= getpythonregpath(HKEY_CURRENT_USER
, skiphome
);
457 /* We only use the default relative PYTHONPATH if we havent
458 anything better to use! */
459 skipdefault
= envpath
!=NULL
|| pythonhome
!=NULL
|| \
460 machinepath
!=NULL
|| userpath
!=NULL
;
463 /* We need to construct a path from the following parts.
464 (1) the PYTHONPATH environment variable, if set;
465 (2) for Win32, the machinepath and userpath, if set;
466 (3) the PYTHONPATH config macro, with the leading "."
467 of each component replaced with pythonhome, if set;
468 (4) the directory containing the executable (argv0_path).
469 The length calculation calculates #3 first.
471 - If PYTHONHOME is set (in any way) item (2) is ignored.
472 - If registry values are used, (3) and (4) are ignored.
475 /* Calculate size of return buffer */
476 if (pythonhome
!= NULL
) {
479 for (p
= PYTHONPATH
; *p
; p
++) {
481 bufsz
++; /* number of DELIM plus one */
483 bufsz
*= strlen(pythonhome
);
487 bufsz
+= strlen(PYTHONPATH
) + 1;
488 bufsz
+= strlen(argv0_path
) + 1;
491 bufsz
+= strlen(userpath
) + 1;
493 bufsz
+= strlen(machinepath
) + 1;
496 bufsz
+= strlen(envpath
) + 1;
498 module_search_path
= buf
= malloc(bufsz
);
500 /* We can't exit, so print a warning and limp along */
501 fprintf(stderr
, "Can't malloc dynamic PYTHONPATH.\n");
503 fprintf(stderr
, "Using environment $PYTHONPATH.\n");
504 module_search_path
= envpath
;
507 fprintf(stderr
, "Using default static path.\n");
508 module_search_path
= PYTHONPATH
;
515 #endif /* MS_WIN32 */
520 strcpy(buf
, envpath
);
521 buf
= strchr(buf
, '\0');
526 strcpy(buf
, userpath
);
527 buf
= strchr(buf
, '\0');
532 strcpy(buf
, machinepath
);
533 buf
= strchr(buf
, '\0');
537 if (pythonhome
== NULL
) {
539 strcpy(buf
, PYTHONPATH
);
540 buf
= strchr(buf
, '\0');
544 if (pythonhome
== NULL
) {
545 strcpy(buf
, PYTHONPATH
);
546 buf
= strchr(buf
, '\0');
548 #endif /* MS_WIN32 */
550 char *p
= PYTHONPATH
;
554 q
= strchr(p
, DELIM
);
559 if (p
[0] == '.' && is_sep(p
[1])) {
560 strcpy(buf
, pythonhome
);
561 buf
= strchr(buf
, '\0');
575 strcpy(buf
, argv0_path
);
576 buf
= strchr(buf
, '\0');
579 /* Now to pull one last hack/trick. If sys.prefix is
580 empty, then try and find it somewhere on the paths
581 we calculated. We scan backwards, as our general policy
582 is that Python core directories are at the *end* of
583 sys.path. We assume that our "lib" directory is
584 on the path, and that our 'prefix' directory is
588 char lookBuf
[MAXPATHLEN
+1];
589 char *look
= buf
- 1; /* 'buf' is at the end of the buffer */
592 char *lookEnd
= look
;
593 /* 'look' will end up one character before the
594 start of the path in question - even if this
595 is one character before the start of the buffer
597 while (*look
!= DELIM
&& look
>= module_search_path
)
599 nchars
= lookEnd
-look
;
600 strncpy(lookBuf
, look
+1, nchars
);
601 lookBuf
[nchars
] = '\0';
602 /* Up one level to the parent */
604 if (search_for_prefix(lookBuf
, LANDMARK
)) {
607 /* If we are out of paths to search - give up */
608 if (look
< module_search_path
)
616 /* External interface */
621 if (!module_search_path
)
623 return module_search_path
;
629 if (!module_search_path
)
635 Py_GetExecPrefix(void)
637 return Py_GetPrefix();
641 Py_GetProgramFullPath(void)
643 if (!module_search_path
)