Prechádzať zdrojové kódy

Ensure module located where python script is executed can be imported

* This is done by "prepending" the script file path to sys.path
when executeString() is called.
Jean-Christophe Fillion-Robin 13 rokov pred
rodič
commit
06e1e79454

+ 3 - 0
Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp

@@ -183,7 +183,10 @@ void ctkAbstractPythonManager::executeFile(const QString& filename)
   PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
   if (main)
     {
+    QString path = QFileInfo(filename).absolutePath();
+    this->executeString(QString("import sys\nsys.path.insert(0, '%1')").arg(path));
     main.evalFile(filename);
+    this->executeString(QString("import sys\nif sys.path[0] == '%1': sys.path.pop(0)").arg(path));
     }
 }