import slicer problem in using python in the shell launched using "slicer --xterm &"

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

import slicer problem in using python in the shell launched using "slicer --xterm &"

bucslirui
I'm trying out the example Steve put on
I have a Ubuntu 11.10 (oneiric).

   To get the matplotlib stalled on my current linux version, I added
"linux3" in the setupext.py and added
the python path to basedirlist "basedirlist = /usr ../python-build" in the
setup.cfg. After the matplotlib installed
successfully, I removed the source directory.

   I launched a xterm using "./Slicer-build/Slicer --xterm &", and in the
xterm I started python using ./python-build/bin/python. In python shell, I
typed in:

   import vtk
   import slicer

   Then the dreaded "segmentation fault" was reported and the python
session was closed. The gdb session information points to the pythonQt
problem.

   I added print information at the __init.py__ in
./Slicer-build/bin/Python/slicer and noticed that the
import failed at trying to import qMRMLWidgetsPythonQt. For some reason, it
could not find the class file
to import. To get around this problem, I added qMRMLWidgetsPythonQt.py in
./Slicer-build/bin/Python/slicer.
Here is how my qMRMLWidgetsPythonQt.py:

   from qMRMLWidgetsPythonQt import *

   Then I tried to "import slicer" again in the python shell, now the
import failed at trying to import qSlicerBaseQTCorePython,
qSlicerBaseQTGUIPythonQt and qSlicerBaseQTCLIPythonQt.py. So I did
the same thing by creating the corresponding .py files in the directory
./Slicer-build/bin/Python/slicer.

   Now in the python shell, I have:

   >> import vtk
   >> import slicer
   from freesurfer import *
   from mrml import *
   from mrmlLogic import *
   from remoteio import *
   from teem import *
   from qMRMLWidgetsPythonQt import *
   from logic import *
   from qSlicerBaseQTCorePythonQt import *
   from qSlicerBaseQTGUIPythonQt import *
   from modulelogic import *
   cannot import name _qSlicerCoreApplicationInstance
   from modulemrml import *
   cannot import name _qSlicerCoreApplicationInstance
   from modulewidget import *
   cannot import name _qSlicerCoreApplicationInstance
   from qSlicerBaseQTCLIPythonQt import *

This time round I don't get segmentation fault, but there are some classes
cannot be imported. I looked at the __init__.py file under 
./Slicer-build/bin/Python/slicer/modulelogic. It seems that the
_qSlicerCoreApplicationInstance is used to figure out the path to
load the vtk classes. My question is that the _qSlicerCoreApplicationInstance 
is never created as the python shell is running outside Slicer GUI, is there any 
way to get around it? Or am I doing something that is ridiculously wrong here??

I have already checked the system path and library path, everything seems
being set properly since the xterm is launched through slicer. Here is the
printout:

os.environ['PATH']
'/home/lir/projs/namic/Slicer4-build/Slicer-build/bin/.:/home/lir/projs/namic/Slicer4-build/teem-build/bin/.:/home/lir/QtSDK/Desktop/Qt/474/gcc/bin:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/cli-modules/.:/home/lir/projs/namic/Slicer4-build/tcl-build/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'

os.environ['LD_LIBRARY_PATH']
'/home/lir/projs/namic/Slicer4-build/VTK-build/bin/.:/home/lir/projs/namic/Slicer4-build/CTK-build/CTK-build/bin/.:/home/lir/QtSDK/Desktop/Qt/474/gcc/lib:/home/lir/projs/namic/Slicer4-build/ITKv3-build/bin/.:/home/lir/projs/namic/Slicer4-build/teem-build/bin/.:/home/lir/projs/namic/Slicer4-build/LibArchive-install/lib:/home/lir/projs/namic/Slicer4-build/Slicer-build/bin/.:/home/lir/projs/namic/Slicer4-build/SlicerExecutionModel-build/ModuleDescriptionParser/bin/.:../lib/Slicer-4.1/qt-loadable-modules:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/cli-modules/.:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/qt-loadable-modules/.:/home/lir/projs/namic/Slicer4-build/tcl-build/lib:/home/lir/projs/namic/Slicer4-build/OpenIGTLink-build:/home/lir/projs/namic/Slicer4-build/OpenIGTLink-build/bin/.:/home/lir/projs/namic/Slicer4-build/CTK-build/PythonQt-build/.:/home/lir/projs/namic/Slicer4-build/python-build/lib:/home/lir/projs/namic/Slicer4-build/CTK-build/QtTesting-build/.:/home/lir/projs/namic/Slicer4-build/python-build/lib/python2.6/site-packages/numpy/core:/home/lir/projs/namic/Slicer4-build/python-build/lib/python2.6/site-packages/numpy/lib:'

>>> os.environ['SLICER_HOME']
'/home/lir/projs/namic/Slicer4-build/Slicer-build'

>>> os.environ['PYTHONPATH']
'/home/lir/projs/namic/Slicer4-build/Slicer-build/bin/.:/home/lir/projs/namic/Slicer4-build/Slicer-build/bin/Python:/home/lir/projs/namic/Slicer4-build/python-build/lib/python2.6/site-packages:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/qt-loadable-modules/.:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/qt-loadable-modules/Python'

Thank you very much for your help and thanks for the patience for reading this long post.



Best,

Rui

_______________________________________________
slicer-devel mailing list
[hidden email]
http://massmail.spl.harvard.edu/mailman/listinfo/slicer-devel
To unsubscribe: send email to [hidden email] with unsubscribe as the subject
Reply | Threaded
Open this post in threaded view
|

Re: import slicer problem in using python in the shell launched using "slicer --xterm &"

Jean-Christophe Fillion-Robin
Hi Rui

The "slicer" python module and related modules are NOT designed to be imported outside of Slicer python interpreter. A running instance of slicer is expected.

The examples reported on the page [1] should be executed within a Slicer interpreter.

You can access slicer python console using different approaches:

  1)  After Slicer is started:  "View -> Python interactor' or "CTRL+3"

  2) Start Slicer using the "--show-python-interactor" option
       For more details "./Slicer --help"

Hth
Jc

On Mon, May 7, 2012 at 11:41 AM, Rui Li (李睿) <[hidden email]> wrote:
I'm trying out the example Steve put on
I have a Ubuntu 11.10 (oneiric).

   To get the matplotlib stalled on my current linux version, I added
"linux3" in the setupext.py and added
the python path to basedirlist "basedirlist = /usr ../python-build" in the
setup.cfg. After the matplotlib installed
successfully, I removed the source directory.

   I launched a xterm using "./Slicer-build/Slicer --xterm &", and in the
xterm I started python using ./python-build/bin/python. In python shell, I
typed in:

   import vtk
   import slicer

   Then the dreaded "segmentation fault" was reported and the python
session was closed. The gdb session information points to the pythonQt
problem.

   I added print information at the __init.py__ in
./Slicer-build/bin/Python/slicer and noticed that the
import failed at trying to import qMRMLWidgetsPythonQt. For some reason, it
could not find the class file
to import. To get around this problem, I added qMRMLWidgetsPythonQt.py in
./Slicer-build/bin/Python/slicer.
Here is how my qMRMLWidgetsPythonQt.py:

   from qMRMLWidgetsPythonQt import *

   Then I tried to "import slicer" again in the python shell, now the
import failed at trying to import qSlicerBaseQTCorePython,
qSlicerBaseQTGUIPythonQt and qSlicerBaseQTCLIPythonQt.py. So I did
the same thing by creating the corresponding .py files in the directory
./Slicer-build/bin/Python/slicer.

   Now in the python shell, I have:

   >> import vtk
   >> import slicer
   from freesurfer import *
   from mrml import *
   from mrmlLogic import *
   from remoteio import *
   from teem import *
   from qMRMLWidgetsPythonQt import *
   from logic import *
   from qSlicerBaseQTCorePythonQt import *
   from qSlicerBaseQTGUIPythonQt import *
   from modulelogic import *
   cannot import name _qSlicerCoreApplicationInstance
   from modulemrml import *
   cannot import name _qSlicerCoreApplicationInstance
   from modulewidget import *
   cannot import name _qSlicerCoreApplicationInstance
   from qSlicerBaseQTCLIPythonQt import *

This time round I don't get segmentation fault, but there are some classes
cannot be imported. I looked at the __init__.py file under 
./Slicer-build/bin/Python/slicer/modulelogic. It seems that the
_qSlicerCoreApplicationInstance is used to figure out the path to
load the vtk classes. My question is that the _qSlicerCoreApplicationInstance 
is never created as the python shell is running outside Slicer GUI, is there any 
way to get around it? Or am I doing something that is ridiculously wrong here??

I have already checked the system path and library path, everything seems
being set properly since the xterm is launched through slicer. Here is the
printout:

os.environ['PATH']
'/home/lir/projs/namic/Slicer4-build/Slicer-build/bin/.:/home/lir/projs/namic/Slicer4-build/teem-build/bin/.:/home/lir/QtSDK/Desktop/Qt/474/gcc/bin:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/cli-modules/.:/home/lir/projs/namic/Slicer4-build/tcl-build/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'

os.environ['LD_LIBRARY_PATH']
'/home/lir/projs/namic/Slicer4-build/VTK-build/bin/.:/home/lir/projs/namic/Slicer4-build/CTK-build/CTK-build/bin/.:/home/lir/QtSDK/Desktop/Qt/474/gcc/lib:/home/lir/projs/namic/Slicer4-build/ITKv3-build/bin/.:/home/lir/projs/namic/Slicer4-build/teem-build/bin/.:/home/lir/projs/namic/Slicer4-build/LibArchive-install/lib:/home/lir/projs/namic/Slicer4-build/Slicer-build/bin/.:/home/lir/projs/namic/Slicer4-build/SlicerExecutionModel-build/ModuleDescriptionParser/bin/.:../lib/Slicer-4.1/qt-loadable-modules:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/cli-modules/.:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/qt-loadable-modules/.:/home/lir/projs/namic/Slicer4-build/tcl-build/lib:/home/lir/projs/namic/Slicer4-build/OpenIGTLink-build:/home/lir/projs/namic/Slicer4-build/OpenIGTLink-build/bin/.:/home/lir/projs/namic/Slicer4-build/CTK-build/PythonQt-build/.:/home/lir/projs/namic/Slicer4-build/python-build/lib:/home/lir/projs/namic/Slicer4-build/CTK-build/QtTesting-build/.:/home/lir/projs/namic/Slicer4-build/python-build/lib/python2.6/site-packages/numpy/core:/home/lir/projs/namic/Slicer4-build/python-build/lib/python2.6/site-packages/numpy/lib:'

>>> os.environ['SLICER_HOME']
'/home/lir/projs/namic/Slicer4-build/Slicer-build'

>>> os.environ['PYTHONPATH']
'/home/lir/projs/namic/Slicer4-build/Slicer-build/bin/.:/home/lir/projs/namic/Slicer4-build/Slicer-build/bin/Python:/home/lir/projs/namic/Slicer4-build/python-build/lib/python2.6/site-packages:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/qt-loadable-modules/.:/home/lir/projs/namic/Slicer4-build/Slicer-build/lib/Slicer-4.1/qt-loadable-modules/Python'

Thank you very much for your help and thanks for the patience for reading this long post.



Best,

Rui

_______________________________________________
slicer-devel mailing list
[hidden email]
http://massmail.spl.harvard.edu/mailman/listinfo/slicer-devel
To unsubscribe: send email to [hidden email] with unsubscribe as the subject



--
+1 919 869 8849


_______________________________________________
slicer-devel mailing list
[hidden email]
http://massmail.spl.harvard.edu/mailman/listinfo/slicer-devel
To unsubscribe: send email to [hidden email] with unsubscribe as the subject
Reply | Threaded
Open this post in threaded view
|

Re: import slicer problem in using python in the shell launched using "slicer --xterm &"

bucslirui
This post was updated on .
Hi, Jc,

     Thanks for the quick reply. I was ambiguous in the previous posting. I was actually trying out
the ipython example and encountered the problem. So Steve's example is targeted for using ipython
outside Slicer GUI. Thus the command "./Slicer-build/Slicer --xterm &" and followed by launch ipython
in the xterm.

     I got the import slicer segmentation fault problem in ipython. But I think the cause is the same. I have exchanged a couple of emails with Steve and Jim regarding this issue. We thought that it could be just a path setting problem. Since the _qSlicerCoreApplicationInstance is only used to figure out the path for loading corresponding vtk classes, I think it is doable to write a script to load these classes, but I am not sure it is the right way to solve the problem.

      Thanks.


Best,

Rui

     
Reply | Threaded
Open this post in threaded view
|

Re: import slicer problem in using python in the shell launched using "slicer --xterm &"

Jean-Christophe Fillion-Robin
Hi Rui, 

Integrating IPython would be very nice.

Few weeks ago, I attended the PyCon in SantaClara and got the opportunity to discuss and hack on Slicer with the developer of IPython (Fernando Perez).

The plan would be to support IPython (single process) directly within Slicer interpreter. I would recommend you to work in that direction.

That said, this is not a priority for the moment. Nevertheless, this is a topic I would enjoy tackling during the coming Project week happening at the MIT in Boston. Would you be joining the project week [1] ? 

Remarks regarding "_qSlicerCoreApplicationInstance":  This is a pointer to the instance of Slicer application allowing to access the mrmlscene, the module manager etc ... it's not just used for path.

From what I understand, IPython is gaining more and more momentum and is definitively something useful to a lot of people, I believe we should think about an "integrated and tested solution" :)

Finally, note also that we are in the process of defining a road map for Slicer so that people could get a better understanding of the short, medium and long term goals.

Stay tuned.

Thanks
Jc

[1] http://www.na-mic.org/Wiki/index.php/2012_Summer_Project_Week

On Mon, May 7, 2012 at 12:55 PM, bucslirui <[hidden email]> wrote:
Hi, Jc,

    Thanks for the quick reply. I was ambiguous in the previous posting. I
was actually trying out
the ipython example and encountered the problem. So Steve's example is
targeted for using ipython
outside Slicer GUI. Thus the command "./Slicer-build/Slicer --xterm &" and
followed by launch ipython
in the xterm.

    I got the import slicer segmentation fault problem in ipython. But I
think the cause is the same. I have exchanged a couple of emails with Steve
and Jim regarding this issue. We thought that it could be just a path
setting problem. Since the _qSlicerCoreApplicationInstance is only used to
figure out the path for loading corresponding vtk classes, I think it is
doable to write a script to load these classes, but I am not it is the right
way to solve the problem.

     Thanks.


Best,

Rui



--
View this message in context: http://slicer-devel.65872.n3.nabble.com/import-slicer-problem-in-using-python-in-the-shell-launched-using-slicer-xterm-tp3968880p3969047.html
Sent from the slicer-devel mailing list archive at Nabble.com.
_______________________________________________
slicer-devel mailing list
[hidden email]
http://massmail.spl.harvard.edu/mailman/listinfo/slicer-devel
To unsubscribe: send email to [hidden email] with unsubscribe as the subject



--
+1 919 869 8849


_______________________________________________
slicer-devel mailing list
[hidden email]
http://massmail.spl.harvard.edu/mailman/listinfo/slicer-devel
To unsubscribe: send email to [hidden email] with unsubscribe as the subject
Reply | Threaded
Open this post in threaded view
|

Re: import slicer problem in using python in the shell launched using "slicer --xterm &"

Steve Pieper
Hi Rui -

Sorry for the difficulties with this - I hadn't realized that the slicer4 ipython example wasn't up to date with the later versions of the slicer python module.  I added a note to the wiki on this:

http://www.slicer.org/slicerWiki/index.php?title=Slicer4%3APython&diff=26116&oldid=26115

The goal of that section is still important though: being able to use the slicer logic and mrml python code independent of the PythonQt gui.  For example, to support running slicer code on headless clusters like the scenario John just raised on the list.

I took a closer look at why it was segfaulting and found as you did that the 'import slicer' call brought in code that assumes qt will be available.  As Jc noted, we plan to improve the integration of ipython so we will keep the various use cases in mind.  IMHO it would be ideal in the long term if we could start with a generic python interpreter and import the parts of slicer we need for various scenarios, but right now PythonQt is not setup to support that so instead we use the specially configured python interpreter  embedded in the slicer core.

As an experiment  on the mrml/logic side, I did create this topic branch that allows me to run the ipython/slicer/matplotlib example from the wiki page:

https://github.com/pieper/Slicer/compare/import-slicer-to-standalone-python

note that for testing purposes I use a hard-coded replacement for the _qSlicerCoreApplicationInstance but this could be fixed (version info from cmake and slicerHome discovered via python at runtime -- like you did in your version you emailed about).  Also note that I didn't have to change any other paths or configs on my build for ubuntu 10.10.

Best,
Steve


On Mon, May 7, 2012 at 1:53 PM, Jean-Christophe Fillion-Robin <[hidden email]> wrote:
Hi Rui, 

Integrating IPython would be very nice.

Few weeks ago, I attended the PyCon in SantaClara and got the opportunity to discuss and hack on Slicer with the developer of IPython (Fernando Perez).

The plan would be to support IPython (single process) directly within Slicer interpreter. I would recommend you to work in that direction.

That said, this is not a priority for the moment. Nevertheless, this is a topic I would enjoy tackling during the coming Project week happening at the MIT in Boston. Would you be joining the project week [1] ? 

Remarks regarding "_qSlicerCoreApplicationInstance":  This is a pointer to the instance of Slicer application allowing to access the mrmlscene, the module manager etc ... it's not just used for path.

From what I understand, IPython is gaining more and more momentum and is definitively something useful to a lot of people, I believe we should think about an "integrated and tested solution" :)

Finally, note also that we are in the process of defining a road map for Slicer so that people could get a better understanding of the short, medium and long term goals.

Stay tuned.

Thanks
Jc

[1] http://www.na-mic.org/Wiki/index.php/2012_Summer_Project_Week


On Mon, May 7, 2012 at 12:55 PM, bucslirui <[hidden email]> wrote:
Hi, Jc,

    Thanks for the quick reply. I was ambiguous in the previous posting. I
was actually trying out
the ipython example and encountered the problem. So Steve's example is
targeted for using ipython
outside Slicer GUI. Thus the command "./Slicer-build/Slicer --xterm &" and
followed by launch ipython
in the xterm.

    I got the import slicer segmentation fault problem in ipython. But I
think the cause is the same. I have exchanged a couple of emails with Steve
and Jim regarding this issue. We thought that it could be just a path
setting problem. Since the _qSlicerCoreApplicationInstance is only used to
figure out the path for loading corresponding vtk classes, I think it is
doable to write a script to load these classes, but I am not it is the right
way to solve the problem.

     Thanks.


Best,

Rui



--
View this message in context: http://slicer-devel.65872.n3.nabble.com/import-slicer-problem-in-using-python-in-the-shell-launched-using-slicer-xterm-tp3968880p3969047.html
Sent from the slicer-devel mailing list archive at Nabble.com.
_______________________________________________
slicer-devel mailing list
[hidden email]
http://massmail.spl.harvard.edu/mailman/listinfo/slicer-devel
To unsubscribe: send email to [hidden email] with unsubscribe as the subject



--
<a href="tel:%2B1%20919%20869%208849" value="+19198698849" target="_blank">+1 919 869 8849


_______________________________________________
slicer-devel mailing list
[hidden email]
http://massmail.spl.harvard.edu/mailman/listinfo/slicer-devel
To unsubscribe: send email to [hidden email] with unsubscribe as the subject


_______________________________________________
slicer-devel mailing list
[hidden email]
http://massmail.spl.harvard.edu/mailman/listinfo/slicer-devel
To unsubscribe: send email to [hidden email] with unsubscribe as the subject
Reply | Threaded
Open this post in threaded view
|

Re: import slicer problem in using python in the shell launched using "slicer --xterm &"

bucslirui
Hi, Steve and Jc,

    Thanks for the detailed reply. It's really helpful.

    I am working on providing a python interface for dispatching some of the slicer code to
the cluster computing nodes and providing a report mechanism, e.g. job in progress, or job
completed. So having ipython running outside slicer interpreter might be easier to start with
in this case.

    Btw, the fix Steve put on his topic branch works. Thanks again.


Best,

Rui