From 7767c085c33d6f230b2ad3e22760a191125ef25d Mon Sep 17 00:00:00 2001 From: Ashesh Vashi Date: Fri, 10 Mar 2017 21:16:13 +0530 Subject: [PATCH] Resolved an issue finding the python interpreter on *nix systems, and Windows 2008 R2 (32 bit), while running the pgAdmin 4 as runtime for the PostgreSQL one click installers. - Found a typo in runtime code, we were appending the path using ';' on *nix systems too. We should have used ':', and that did not allow the os.environ['PATH'] to identify the correct path of the python interpreter under the 'venv' directory. - On Windows 2008, it was not honouring the environment variables, set under the Qt application (e.g. pgAdmin4.exe runtime), in the python application. (e.g. pgAdmin4.py). We will need to assume that - the python interpreter resides under the 'venv' directory outside the 'bin' directory. - Also, on windows 2008, it was setting PYTHONHOME environment variable to the full path of the pgAdmin4.exe, we need to reset it to 'venv' directory, if we find the python interpreter under it. Thanks Murtuza Zabuawala for tips, and help. --- runtime/Server.cpp | 10 +++++++ web/pgadmin/misc/bgprocess/processes.py | 39 ++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/runtime/Server.cpp b/runtime/Server.cpp index d655119cb..2371d7f5d 100644 --- a/runtime/Server.cpp +++ b/runtime/Server.cpp @@ -28,15 +28,25 @@ static void add_to_path(QString &python_path, QString path, bool prepend=false) { if (!prepend) { +#if defined(Q_OS_WIN) if (!python_path.isEmpty() && !python_path.endsWith(";")) python_path.append(";"); +#else + if (!python_path.isEmpty() && !python_path.endsWith(":")) + python_path.append(":"); +#endif python_path.append(path); } else { +#if defined(Q_OS_WIN) if (!python_path.isEmpty() && !python_path.startsWith(";")) python_path.prepend(";"); +#else + if (!python_path.isEmpty() && !python_path.startsWith(":")) + python_path.prepend(":"); +#endif python_path.prepend(path); } diff --git a/web/pgadmin/misc/bgprocess/processes.py b/web/pgadmin/misc/bgprocess/processes.py index 457e6d343..ad6d0ff50 100644 --- a/web/pgadmin/misc/bgprocess/processes.py +++ b/web/pgadmin/misc/bgprocess/processes.py @@ -227,8 +227,45 @@ class BatchProcess(object): interpreter = which(u'pythonw.exe', paths) if interpreter is None: interpreter = which(u'python.exe', paths) + + if interpreter is None and current_app.PGADMIN_RUNTIME: + # We've faced an issue with Windows 2008 R2 (x86) regarding, + # not honouring the environment variables set under the Qt + # (e.g. runtime), and also setting PYTHONHOME same as + # sys.executable (i.e. pgAdmin4.exe). + # + # As we know, we're running it under the runtime, we can assume + # that 'venv' directory will be available outside of 'bin' + # directory. + # + # We would try out luck to find python executable based on that + # assumptions. + bin_path = os.path.dirname(sys.executable) + + venv = os.path.realpath( + os.path.join(bin_path, u'..\\venv') + ) + + interpreter = which(u'pythonw.exe', [venv]) + if interpreter is None: + interpreter = which(u'pythonw.exe', [venv]) + + if interpreter is not None: + # Our assumptions are proven right. + # Let's append the 'bin' directory to the PATH environment + # variable. And, also set PYTHONHOME environment variable + # to 'venv' directory. + os.environ['PATH'] = bin_path + ';' + os.environ['PATH'] + os.environ['PYTHONHOME'] = venv else: - paths.insert(0, os.path.join(u(sys.prefix), u'bin')) + # Let's not use sys.prefix in runtime. + # 'sys.prefix' is not identified on *nix systems for some unknown + # reason, while running under the runtime. + # We're already adding '/pgAdmin 4/venv/bin' + # directory in the PATH environment variable. Hence - it will + # anyway be the redundant value in paths. + if not current_app.PGADMIN_RUNTIME: + paths.insert(0, os.path.join(u(sys.prefix), u'bin')) interpreter = which(u'python', paths) p = None