Skip to content

Commit

Permalink
Remove pointless references to PYTHONHOME and PYTHONPATH
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
codewarrior0 committed Aug 15, 2015
1 parent 6a8845a commit 968311e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
2 changes: 1 addition & 1 deletion PyInstaller/loader/pyiboot01_bootstrap.py
Expand Up @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion bootloader/src/pyi_archive.h
Expand Up @@ -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];
/*
Expand Down
1 change: 0 additions & 1 deletion bootloader/src/pyi_main.c
Expand Up @@ -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;

Expand Down
52 changes: 23 additions & 29 deletions bootloader/src/pyi_pythonlib.c
Expand Up @@ -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];
Expand Down Expand Up @@ -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));
Expand All @@ -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)) {
Expand All @@ -426,15 +417,15 @@ 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 */
if(!pyi_locale_char2wchar(pyhome_w, status->mainpath, PATH_MAX)) {
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);
};

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions doc/source/Manual.rst
Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit 968311e

Please sign in to comment.