Dear Peter,
I've just entered the issue into your bug tracking system. Here is the
exerpt from the issue text:
My environment:
Windows 7 64bit ; VS 2017 Professional, Qt 5.12.3, Python 3.5.2 (Anaconda
4.2.0 bundle)
Short technical description of the problem:
The reason for the crash lies in the fact that the first attempt to use
the mitk::PythonService happens within the mitk::PythonService()
constructor (within mitk::PythonActivator::Load() method) as part of Python
static initialization and BEFORE the service gets registed thorugh the
m_PythonServiceRegistration =
context->RegisterService<mitk::IPythonService>(m_PythonService.GetPointer(),
_PythonServiceProps); call within the same
mitk::PythonActivator::Load() method.
Short technical description of the fix:
The idea of the fix is to defer the first query for the PythonService until
the service is register by moving all python initialization related the
mitk::PythonService() constructor functionality into new separate
Initialize() method (also defining it within the IPythonService abstrct
class) and calling this Initialize() method within the mitk::
QmitkPythonVariableStackTableModel() constructor.
Debugging the modified code has shown that now the first PythonService
query indeed happens after it was actually registered which resolved the
problem.
I am also attaching the diff file (PythoPluginDiff.txt) of all
modifications I've made to resolve the issue which can be used as a patch
(did not find how I can attach it to the bug report)
Regarding my second question (fixing the colors for Dark Theme), I am
attaching the second diff file ()containing modification to
QmitkCtkPythonShell() constructor which made the Python console text pretty
readable.
On Wed, Jun 19, 2019 at 6:09 AM Neher, Peter <[email protected]>
wrote:
> Dear Alex,
>
>
>
> If I recall correctly, this feature worked for me when I last tried it a
> couple of weeks ago. Maybe it’s a Qt issue. Which Qt version are you using?
> Also could you enter this issue in our bug tracking system including your
> quick fix? https://phabricator.mitk.org/
>
>
>
> I am currently out of office, but I can take a look at it when I am back.
>
>
>
> Cheers,
>
> Peter
>
>
>
> *Von:* Alex Lisovich <[email protected]>
> *Gesendet:* Dienstag, 18. Juni 2019 00:44
> *An:* [email protected]
> *Betreff:* Re: [mitk-users] org.mitk.gui.qt.python plugin fails to run on
> Windows
>
>
>
> Dear All,
>
>
>
> Sorry for a long post, but here it goes:
>
>
>
> For the last few days I was trying to compile/run
> the org.mitk.gui.qt.python Python pligin.
>
> Basically, it compiles just fine, but when trying to load (both in Release
> and Debug mode) it generates (and displays instead of Python console) the
> following message: "Part Initialization Error: Default constructed
> ServiceReference is not valid input to GetService()".
>
>
>
> Running it in Debug mode revealed that this is the result of throwing the
> exception within the ModuleContext::GetService(const ServiceReferenceBase&
> reference) as a result of PythonService not being registered at the moment
> of call which in turn happens within the mitk::PythonActivator::Load()
> during the call to PythonService constructor and BEFORE the PythonService
> is actually get registered.
>
>
>
> Further digging in MITK forum archives, I came along the thread *[mitk-users]
> Can't run Python plugin
> <https://sourceforge.net/p/mitk/mailman/message/34810888/> *initiated by
> Matt Clarkson describing exactly the same problem and dated back to 2016.
> During the discussion, Miklos Espak provided the explanation for this
> phenomenon (bug in PythonQT related to static variables initialization) and
> outlined 3 ways to fix the problem, second of them being moving all
> PythonQT initialization related code from the static initialization.
>
>
>
> Using his second suggestion I was able to successfully implement "quick
> and dirty" fix allowing to plugin load and run just fine (including
> SimpleITK import, images drag and drop, simple processing etc).
>
>
>
> So after all this hassle, I basically have two questions:
>
>
>
> (1) It looks the Python plugin has been broken on Windows since 2016 and
> likely since MITK 2015 release (that's what Miklos is saying in thread
> above). Was anybody able to successfully run it on Windows since then, and
> if yes, was it fixed somehow outside of the master branch or I am missing
> something?
>
>
>
> (2) The Python Console window text colors are such that it is practically
> impossible to see anything you type (in "Light" theme it is fine). Does
> anybody know if there is some qml (or xml) file with default colors I can
> modify (the colorare defined as Q_PROPERTY's within the code)?
>
>
>
>
> P.S. I am running Windows 7 64 bit, the MITK was configured using CMake
> 3.14.3 and compiled on VS 2017 Professional.
>
>
>
>
>
> Best Regards, and sorry again for a supre-long post,
>
>
>
> Alex
>
>
>
>
>
>
>
>
>
diff --git a/Modules/QtPython/QmitkCtkPythonShell.cpp
b/Modules/QtPython/QmitkCtkPythonShell.cpp
index 576b817..07b326c 100644
--- a/Modules/QtPython/QmitkCtkPythonShell.cpp
+++ b/Modules/QtPython/QmitkCtkPythonShell.cpp
@@ -35,6 +35,15 @@ struct QmitkCtkPythonShellData
QmitkCtkPythonShell::QmitkCtkPythonShell(QWidget* parent)
: ctkPythonConsole(parent), d( new QmitkCtkPythonShellData )
{
+ QFont font=this->shellFont();
+
+ font.setPixelSize(font.pixelSize()-1);
+ this->setShellFont(font);
+
+ this->setPromptColor(Qt::darkCyan);
+ this->setCommandTextColor(Qt::magenta);
+
+
MITK_DEBUG("QmitkCtkPythonShell") << "retrieving IPythonService";
us::ModuleContext* context = us::GetModuleContext();
d->m_PythonServiceRef = context->GetServiceReference<mitk::IPythonService>();
Modules/Python/mitkIPythonService.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/Modules/Python/mitkIPythonService.h
b/Modules/Python/mitkIPythonService.h
index 935c8cd..3fda30d 100644
--- a/Modules/Python/mitkIPythonService.h
+++ b/Modules/Python/mitkIPythonService.h
@@ -149,6 +149,8 @@ namespace mitk
virtual void AddRelativeSearchDirs(std::vector< std::string > dirs) =
0;
virtual void AddAbsoluteSearchDirs(std::vector< std::string > dirs) =
0;
+
+ virtual void Initialize() = 0;
};
}
Modules/Python/autoload/PythonService/mitkPythonService.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/Modules/Python/autoload/PythonService/mitkPythonService.h
b/Modules/Python/autoload/PythonService/mitkPythonService.h
index 3b99edb..9430be9 100644
--- a/Modules/Python/autoload/PythonService/mitkPythonService.h
+++ b/Modules/Python/autoload/PythonService/mitkPythonService.h
@@ -33,6 +33,9 @@ namespace mitk
/// instantiate python manager here
PythonService();
///
+ /// initialize service here
+ void Initialize();
+ ///
/// empty implementation...
~PythonService();
///
Modules/QtPython/QmitkPythonVariableStackTableModel.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/Modules/QtPython/QmitkPythonVariableStackTableModel.cpp
b/Modules/QtPython/QmitkPythonVariableStackTableModel.cpp
index 04c810e..36c9bbe 100644
--- a/Modules/QtPython/QmitkPythonVariableStackTableModel.cpp
+++ b/Modules/QtPython/QmitkPythonVariableStackTableModel.cpp
@@ -33,6 +33,7 @@
QmitkPythonVariableStackTableModel::QmitkPythonVariableStackTableModel(QObject *
m_PythonServiceRef = context->GetServiceReference<mitk::IPythonService>();
m_PythonService =
context->GetService<mitk::IPythonService>(m_PythonServiceRef);
m_PythonService->AddPythonCommandObserver( this );
+ m_PythonService->Initialize();
}
QmitkPythonVariableStackTableModel::~QmitkPythonVariableStackTableModel()
.../autoload/PythonService/mitkPythonService.cpp | 51 +++++++++++++---------
1 file changed, 30 insertions(+), 21 deletions(-)
diff --git a/Modules/Python/autoload/PythonService/mitkPythonService.cpp
b/Modules/Python/autoload/PythonService/mitkPythonService.cpp
index 367e07f..2b94037 100644
--- a/Modules/Python/autoload/PythonService/mitkPythonService.cpp
+++ b/Modules/Python/autoload/PythonService/mitkPythonService.cpp
@@ -41,22 +41,23 @@ See LICENSE.txt or http://www.mitk.org for details.
typedef itksys::SystemTools ist;
mitk::PythonService::PythonService()
- : m_ItkWrappingAvailable( true )
- , m_OpenCVWrappingAvailable( true )
- , m_VtkWrappingAvailable( true )
- , m_ErrorOccured( false )
+ : m_ItkWrappingAvailable(true), m_OpenCVWrappingAvailable(true),
m_VtkWrappingAvailable(true), m_ErrorOccured(false)
{
- bool pythonInitialized = static_cast<bool>( Py_IsInitialized() );
//m_PythonManager.isPythonInitialized() );
+}
+
+void mitk::PythonService::Initialize()
+{
+ bool pythonInitialized = static_cast<bool>(Py_IsInitialized()); //
m_PythonManager.isPythonInitialized() );
// due to strange static var behaviour on windows Py_IsInitialized() returns
correct value while
// m_PythonManager.isPythonInitialized() does not because it has been
constructed and destructed again
- if( !pythonInitialized )
+ if (!pythonInitialized)
{
MITK_INFO << "Initializing python service";
- //TODO a better way to do this
+ // TODO a better way to do this
#ifndef WIN32
dlerror();
- if(dlopen(PYTHON_LIBRARY, RTLD_NOW | RTLD_GLOBAL) == nullptr )
+ if (dlopen(PYTHON_LIBRARY, RTLD_NOW | RTLD_GLOBAL) == nullptr)
{
mitkThrow() << "Python runtime could not be loaded: " << dlerror();
}
@@ -65,26 +66,34 @@ mitk::PythonService::PythonService()
std::string programPath =
QCoreApplication::applicationDirPath().toStdString() + "/";
QString pythonCommand;
- pythonCommand.append( QString("import site, sys\n") );
- pythonCommand.append( QString("import SimpleITK as sitk\n") );
- pythonCommand.append( QString("import SimpleITK._SimpleITK as
_SimpleITK\n") );
- pythonCommand.append( QString("import numpy\n") );
-
- pythonCommand.append( QString("sys.path.append('')\n") );
- pythonCommand.append(
QString("sys.path.append('%1')\n").arg(programPath.c_str()) );
- pythonCommand.append(
QString("sys.path.append('%1')\n").arg(EXTERNAL_DIST_PACKAGES) );
- pythonCommand.append(
QString("\nsite.addsitedir('%1')").arg(EXTERNAL_SITE_PACKAGES) );
-
- if( pythonInitialized )
-
m_PythonManager.setInitializationFlags(PythonQt::RedirectStdOut|PythonQt::PythonAlreadyInitialized);
+
+ pythonCommand.append(QString("import site, sys\n"));
+ pythonCommand.append(QString("import SimpleITK as sitk\n"));
+ pythonCommand.append(QString("import SimpleITK._SimpleITK as
_SimpleITK\n"));
+ pythonCommand.append(QString("import numpy\n"));
+ pythonCommand.append(QString("import vtk\n"));
+
pythonCommand.append(QString("sys.path.append('%1')\n").arg(programPath.c_str()));
+
+ /*
+ // was in original constructor as part of setup
+ // commented out because the variables EXTERNAL_DIST_PACKAGES and
EXTERNAL_SITE_PACKAGES
+ // are valid only for computer on which MITK was built
+
pythonCommand.append(QString("sys.path.append('%1')\n").arg(EXTERNAL_DIST_PACKAGES));
+
pythonCommand.append(QString("\nsite.addsitedir('%1')").arg(EXTERNAL_SITE_PACKAGES));
+ */
+
+ if (pythonInitialized)
+ m_PythonManager.setInitializationFlags(PythonQt::RedirectStdOut |
PythonQt::PythonAlreadyInitialized);
else
m_PythonManager.setInitializationFlags(PythonQt::RedirectStdOut);
+
m_PythonManager.initialize();
- m_PythonManager.executeString( pythonCommand,
ctkAbstractPythonManager::FileInput );
+ m_PythonManager.executeString(pythonCommand,
ctkAbstractPythonManager::FileInput);
}
}
+
mitk::PythonService::~PythonService()
{
MITK_DEBUG("mitk::PythonService") << "destructing PythonService";
_______________________________________________
mitk-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mitk-users