From 968311ea6fb3f5731c4a314bb8bf0c9603d79011 Mon Sep 17 00:00:00 2001 From: David Vierra Date: Sat, 15 Aug 2015 10:06:14 -1000 Subject: [PATCH] Remove pointless references to PYTHONHOME and PYTHONPATH PyInstaller built apps ignore these environment variables. Do not try to set them in pyi_pylib_start_python, as this is pointless. Rename references to the environs in comments and documentation - mention sys.path, sys.prefix, and sys.executable instead. --- PyInstaller/loader/pyiboot01_bootstrap.py | 2 +- bootloader/src/pyi_archive.h | 2 +- bootloader/src/pyi_main.c | 1 - bootloader/src/pyi_pythonlib.c | 52 ++++++++++------------- doc/source/Manual.rst | 4 +- 5 files changed, 27 insertions(+), 34 deletions(-) diff --git a/PyInstaller/loader/pyiboot01_bootstrap.py b/PyInstaller/loader/pyiboot01_bootstrap.py index 881e8c27d9..d5a745eea2 100644 --- a/PyInstaller/loader/pyiboot01_bootstrap.py +++ b/PyInstaller/loader/pyiboot01_bootstrap.py @@ -55,7 +55,7 @@ del os.environ[VIRTENV] -# Ensure PYTHONPATH contains absolute paths. Otherwise import of other python +# Ensure sys.path contains absolute paths. Otherwise import of other python # modules will fail when current working directory is changed by frozen # application. python_path = [] diff --git a/bootloader/src/pyi_archive.h b/bootloader/src/pyi_archive.h index dd24506cc2..916b332dd1 100644 --- a/bootloader/src/pyi_archive.h +++ b/bootloader/src/pyi_archive.h @@ -75,7 +75,7 @@ typedef struct _archive_status { * Main path could be homepath or temppath. It will be temppath * if temppath is available. Sometimes we do not need to know if temppath * or homepath should be used. We only need to know the path. This variable - * is used for example to set PYTHONPATH or PYTHONHOME, and to set sys._MEIPASS. + * is used for example to set sys.path, sys.prefix, and sys._MEIPASS. */ char mainpath[PATH_MAX]; /* diff --git a/bootloader/src/pyi_main.c b/bootloader/src/pyi_main.c index 742fc4b237..2ed6dfdf4c 100644 --- a/bootloader/src/pyi_main.c +++ b/bootloader/src/pyi_main.c @@ -44,7 +44,6 @@ int pyi_main(int argc, char * argv[]) char executable[PATH_MAX]; char homepath[PATH_MAX]; char archivefile[PATH_MAX]; - char MEIPASS2[PATH_MAX]; int rc = 0; char *extractionpath = NULL; diff --git a/bootloader/src/pyi_pythonlib.c b/bootloader/src/pyi_pythonlib.c index 9a8af25017..f9dd229092 100644 --- a/bootloader/src/pyi_pythonlib.c +++ b/bootloader/src/pyi_pythonlib.c @@ -340,10 +340,12 @@ wchar_t * pyi_locale_char2wchar(wchar_t * dst, char * src, size_t len) { */ int pyi_pylib_start_python(ARCHIVE_STATUS *status) { - /* Set PYTHONPATH so dynamic libs will load. - * PYTHONHOME for function Py_SetPythonHome() should point - * to a zero-terminated character string in static storage, same for - * Py_SetProgramName. + /* Set sys.path, sys.prefix, and sys.executable so dynamic libs will load. + * + * The Python APIs we use here (Py_SetProgramName, Py_SetPythonHome) + * specify their argument should be a "string in static storage". + * That is, the APIs use the string pointer as given and will neither copy + * its contents nor free its memory. * * NOTE: Statics are zero-initialized. */ static char pypath[2*PATH_MAX + 14]; @@ -378,13 +380,8 @@ int pyi_pylib_start_python(ARCHIVE_STATUS *status) PI_Py_SetProgramName(progname_w); }; - /* Set the PYTHONPATH - * On Python 3, we must set PYTHONPATH to have base_library.zip before - * calling Py_Initialize as it needs the codecs and other modules. - * mainpath must be last because _pyi_bootstrap uses sys.path[-1] as SYS_PREFIX - */ - // TODO: set _MEIPASS earlier. PySys_SetObject? - VS("LOADER: Manipulating environment (PYTHONPATH, PYTHONHOME)\n"); + /* Set sys.path */ + VS("LOADER: Manipulating environment (sys.path, sys.prefix)\n"); if(is_py2) { /* sys.path = [mainpath] */ strncpy(pypath, status->mainpath, strlen(status->mainpath)); @@ -396,27 +393,21 @@ int pyi_pylib_start_python(ARCHIVE_STATUS *status) strncat(pypath, PYI_PATHSEPSTR, strlen(PYI_PATHSEPSTR)); strncat(pypath, status->mainpath, strlen(status->mainpath)); }; - - VS("LOADER: Pre-init PYTHONPATH is %s\n", pypath); - if (is_py2) { -#ifdef _WIN32 - if(!pyi_win32_utf8_to_mbs_sfn(pypath_sfn, pypath, PATH_MAX)) { - FATALERROR("Failed to convert pypath to ANSI (invalid multibyte string)\n"); - } - pyi_setenv("PYTHONPATH", pypath_sfn); -#else - pyi_setenv("PYTHONPATH", pypath); -#endif - } else { + /* + * On Python 3, we must set sys.path to have base_library.zip before + * calling Py_Initialize as it needs `encodings` and other modules. + */ + if (!is_py2) { /* Decode using current locale */ if(!pyi_locale_char2wchar(pypath_w, pypath, PATH_MAX)) { FATALERROR("Failed to convert pypath to wchar_t\n"); return -1; } + VS("LOADER: Pre-init sys.path is %s\n", pypath); PI_Py_SetPath(pypath_w); }; - /* Set PYTHONHOME by using function from Python C API. */ + /* Set sys.prefix and sys.exec_prefix using Py_SetPythonHome */ if (is_py2) { #ifdef _WIN32 if(!pyi_win32_utf8_to_mbs_sfn(pyhome, status->mainpath, PATH_MAX)) { @@ -426,7 +417,7 @@ int pyi_pylib_start_python(ARCHIVE_STATUS *status) #else strcpy(pyhome, status->mainpath); #endif - VS("LOADER: PYTHONHOME is %s\n", pyhome); + VS("LOADER: sys.prefix is %s\n", pyhome); PI_Py2_SetPythonHome(pyhome); } else { /* Decode using current locale */ @@ -434,7 +425,7 @@ int pyi_pylib_start_python(ARCHIVE_STATUS *status) FATALERROR("Failed to convert pypath to wchar_t\n"); return -1; } - VS("LOADER: PYTHONHOME is %s\n", status->mainpath); + VS("LOADER: sys.prefix is %s\n", status->mainpath); PI_Py_SetPythonHome(pyhome_w); }; @@ -470,12 +461,15 @@ int pyi_pylib_start_python(ARCHIVE_STATUS *status) * the paths we want. */ VS("LOADER: Overriding Python's sys.path\n"); - VS("LOADER: Post-init PYTHONPATH is %s\n", pypath); + VS("LOADER: Post-init sys.path is %s\n", pypath); if (is_py2) { #ifdef _WIN32 - PI_Py2Sys_SetPath(pypath_sfn); + if(!pyi_win32_utf8_to_mbs_sfn(pypath_sfn, pypath, PATH_MAX)) { + FATALERROR("Failed to convert pypath to ANSI (invalid multibyte string)\n"); + } + PI_Py2Sys_SetPath(pypath_sfn); #else - PI_Py2Sys_SetPath(pypath); + PI_Py2Sys_SetPath(pypath); #endif } else { PI_PySys_SetPath(pypath_w); diff --git a/doc/source/Manual.rst b/doc/source/Manual.rst index aedfc3b3ef..b79f8662ac 100644 --- a/doc/source/Manual.rst +++ b/doc/source/Manual.rst @@ -1417,7 +1417,7 @@ B. Second process: bootloader itself started as a child process. The name of the dynamic library is embedded in the executable file. - 3. Initialize Python interpreter: set PYTHONPATH, PYTHONHOME. + 3. Initialize Python interpreter: set sys.path, sys.prefix, sys.executable. 4. Run python code. @@ -1489,7 +1489,7 @@ in a bundled app: *package.subpackage.module*\ ``.pyd`` or *package.subpackage.module*\ ``.so`` -4. Next examine paths in the ``sys.path`` (PYTHONPATH). +4. Next examine paths in the ``sys.path``. There could be any additional location with python modules or ``.egg`` filenames.