Setting the PYTHONPATH variable before initializing the python

environment using Py_Initialize() function.

Also, add virtual environment binary path to the PATH environment
variable.
This commit is contained in:
Ashesh Vashi 2016-06-21 15:01:09 +05:30
parent a1f65e981f
commit 4a7607445f

View File

@ -18,7 +18,6 @@
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QMessageBox> #include <QMessageBox>
#include <QProcessEnvironment>
// App headers // App headers
#include "Server.h" #include "Server.h"
@ -65,17 +64,13 @@ Server::Server(quint16 port)
Py_SetProgramName(m_wcAppName); Py_SetProgramName(m_wcAppName);
#endif #endif
Py_Initialize();
// Setup the search path // Setup the search path
QSettings settings; QSettings settings;
QString python_path = settings.value("PythonPath").toString(); QString python_path = settings.value("PythonPath").toString();
// Get the application directory // Get the application directory
QString app_dir = qApp->applicationDirPath(); QString app_dir = qApp->applicationDirPath();
QString path_env = qgetenv("PATH");;
QProcessEnvironment env;
QString path_env = env.value("PATH");
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
// In the case we're running in a release appbundle, we need to ensure the // In the case we're running in a release appbundle, we need to ensure the
@ -128,35 +123,36 @@ Server::Server(quint16 port)
add_to_path(python_path, venvSitePackagesPath.canonicalFilePath()); add_to_path(python_path, venvSitePackagesPath.canonicalFilePath());
#endif #endif
env.insert("PATH", path_env); qputenv("PATH", path_env.toUtf8().data());
if (python_path.length() > 0) if (python_path.length() > 0)
{ {
// Split the path setting into individual entries // Split the path setting into individual entries
QStringList path_list = python_path.split(";", QString::SkipEmptyParts); QStringList path_list = python_path.split(";", QString::SkipEmptyParts);
python_path = QString();
// Get the current path
PyObject* sysPath = PySys_GetObject((char*)"path");
// Add new additional path elements // Add new additional path elements
for (int i = path_list.size() - 1; i >= 0 ; --i) for (int i = path_list.size() - 1; i >= 0 ; --i)
{ {
#ifdef PYTHON2 python_path.append(path_list.at(i));
PyList_Append(sysPath, PyString_FromString(path_list.at(i).toUtf8().data())); if (i > 0)
{
#if Q_OS_WIN
python_path.append(";");
#else #else
#if PY_MINOR_VERSION > 2 python_path.append(":");
PyList_Append(sysPath, PyUnicode_DecodeFSDefault(path_list.at(i).toUtf8().data()));
#else
PyList_Append(sysPath, PyBytes_FromString(path_list.at(i).toUtf8().data()));
#endif
#endif #endif
} }
} }
qputenv("PYTHONPATH", python_path.toUtf8().data());
}
qDebug() << "Full Python path: " << python_path; qDebug() << "Full Python path: " << python_path;
python_path = settings.value("PythonPath").toString(); python_path = settings.value("PythonPath").toString();
qDebug() << "User Python path: " << python_path; qDebug() << "User Python path: " << python_path;
Py_Initialize();
} }
Server::~Server() Server::~Server()