Skip to content

Problem with (documentation of?) QT gui and mayavi #10384

Open

Description

@mwaskom

I am trying to use Mayavi in IPython (terminal at the moment) and am running into issues that are surprising me in light of the documentation on this issue.

Specifically, following this page: http://ipython.readthedocs.io/en/stable/interactive/reference.html#pyqt-and-pyside

I have done

[mlw-mbair ~]$ QT_API=pyqt ipython --gui qt

but importing mayavi fails:

In [1]: from mayavi import mlab
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-3f0e4b94a8ea> in <module>()
----> 1 from mayavi import mlab

/Users/mwaskom/anaconda/lib/python2.7/site-packages/mayavi/mlab.py in <module>()
     25
     26 # Mayavi imports
---> 27 from mayavi.tools.camera import view, roll, yaw, pitch, move
     28 from mayavi.tools.figure import figure, clf, gcf, savefig, \
     29     draw, sync_camera, close, screenshot

/Users/mwaskom/anaconda/lib/python2.7/site-packages/mayavi/tools/camera.py in <module>()
     23 # We can't use gcf, as it creates a circular import in camera management
     24 # routines.
---> 25 from .engine_manager import get_engine
     26
     27

/Users/mwaskom/anaconda/lib/python2.7/site-packages/mayavi/tools/engine_manager.py in <module>()
     10
     11 # Local imports
---> 12 from mayavi.preferences.api import preference_manager
     13 from mayavi.core.registry import registry
     14 from mayavi.core.engine import Engine

/Users/mwaskom/anaconda/lib/python2.7/site-packages/mayavi/preferences/api.py in <module>()
      2
      3 # The global PreferenceManager instance
----> 4 from .preference_manager import preference_manager
      5 from .bindings import set_scene_preferences, get_scene_preferences

/Users/mwaskom/anaconda/lib/python2.7/site-packages/mayavi/preferences/preference_manager.py in <module>()
     27 from traits.etsconfig.api import ETSConfig
     28 from traits.api import HasTraits, Instance
---> 29 from traitsui.api import View, Group, Item
     30 from apptools.preferences.api import (ScopedPreferences, IPreferences,
     31         PreferencesHelper)

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traitsui/api.py in <module>()
     34
     35 try:
---> 36     from .editors.api import ArrayEditor
     37 except ImportError:
     38     # ArrayEditor depends on numpy, so ignore if numpy is not present.

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traitsui/editors/__init__.py in <module>()
     21
     22 try:
---> 23     from .api import ArrayEditor
     24 except ImportError:
     25     pass

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traitsui/editors/api.py in <module>()
     22 from .button_editor import ButtonEditor
     23 from .check_list_editor import CheckListEditor
---> 24 from .code_editor import CodeEditor
     25 from .color_editor import ColorEditor
     26 from .compound_editor import CompoundEditor

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traitsui/editors/code_editor.py in <module>()
     34 #-------------------------------------------------------------------------------
     35
---> 36 class ToolkitEditorFactory ( EditorFactory ):
     37     """ Editor factory for code editors.
     38     """

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traitsui/editors/code_editor.py in ToolkitEditorFactory()
     46
     47     # Background color for marking lines
---> 48     mark_color = Color( 0xECE9D8 )
     49
     50     # Object trait containing the currently selected line (optional)

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traits/traits.pyc in __call__(self, *args, **metadata)
    520
    521     def __call__ ( self, *args, **metadata ):
--> 522         return self.maker_function( *args, **metadata )
    523
    524 class TraitImportError ( TraitFactory ):

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traits/traits.pyc in Color(*args, **metadata)
   1234     from traitsui.toolkit_traits import ColorTrait
   1235
-> 1236     return ColorTrait( *args, **metadata )
   1237
   1238 Color = TraitFactory( Color )

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traitsui/toolkit_traits.pyc in ColorTrait(*args, **traits)
      5
      6 def ColorTrait ( *args, **traits ):
----> 7     return toolkit().color_trait( *args, **traits )
      8
      9 def RGBColorTrait ( *args, **traits ):

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traitsui/toolkit.pyc in toolkit(*toolkits)
    159             try:
    160                 with provisional_toolkit(toolkit_name):
--> 161                     _toolkit = _import_toolkit(toolkit_name)
    162                     return _toolkit
    163             except (AttributeError, ImportError) as exc:

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traitsui/toolkit.pyc in _import_toolkit(name)
     81
     82 def _import_toolkit ( name ):
---> 83     return __import__( name, globals=globals(), level=1 ).toolkit
     84
     85

/Users/mwaskom/anaconda/lib/python2.7/site-packages/traitsui/qt4/__init__.py in <module>()
     16 # import pyface.qt before anything else is done so the sipapi
     17 # can be set correctly if needed
---> 18 import pyface.qt
     19
     20 #----------------------------------------------------------------------------

/Users/mwaskom/anaconda/lib/python2.7/site-packages/pyface/qt/__init__.py in <module>()
     38
     39 elif qt_api == 'pyqt':
---> 40     prepare_pyqt4()
     41
     42 elif qt_api != 'pyside':

/Users/mwaskom/anaconda/lib/python2.7/site-packages/pyface/qt/__init__.py in prepare_pyqt4()
     15     # Set PySide compatible APIs.
     16     import sip
---> 17     sip.setapi('QDate', 2)
     18     sip.setapi('QDateTime', 2)
     19     sip.setapi('QString', 2)

ValueError: API 'QDate' has already been set to version 1

Based on the docs I thought QT_API would use PyQT API v2 and thus be compatible with mayavi.

I am not, to my knowledge, importing anything else that would be setting the QT API ahead of time; i.e. my ipython startup file is pretty clean:

[mlw-mbair ~]$ grep '^\w' ~/.ipython/profile_default/ipython_config.py
c = get_config()
c.TerminalIPythonApp.display_banner = False
c.TerminalIPythonApp.display_banner = False
c.TerminalInteractiveShell.separate_in = ''
c.TerminalInteractiveShell.colors = 'Linux'

I am using an up-to-date version of IPython:

[mlw-mbair ~]$ python -c "import IPython; print(IPython.sys_info())"

{'commit_hash': u'5a894b9',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/Users/mwaskom/anaconda/lib/python2.7/site-packages/IPython',
 'ipython_version': '5.3.0',
 'os_name': 'posix',
 'platform': 'Darwin-13.4.0-x86_64-i386-64bit',
 'sys_executable': '/Users/mwaskom/anaconda/bin/python',
 'sys_platform': 'darwin',
 'sys_version': '2.7.12 |Anaconda 2.3.0 (x86_64)| (default, Jul  2 2016, 17:43:17) \n[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)]'}

and (as I think this might be relevant) matplotlib:

[mlw-mbair ~]$ python -c "import matplotlib; print(matplotlib.__version__)"
2.0.0

This used to work for me in the past, but various things have been updated since. I'm not sure if something has broken, or that section of the docs have gone stale, or I'm simply doing something wrong.

Activity

  1. mwaskom commented on Mar 8, 2017

    @mwaskom
    Author

    Also maybe relevant(?): activating the QT gui backend after importing mayavi works fine:

    [mlw-mbair ~]$ QT_API=pyqt ipython
    In [1]: from mayavi import mlab
    In [2]: %gui qt
    In [3]: f = mlab.figure()
    
  2. ccordoba12 commented on Mar 8, 2017

    @ccordoba12
    Member

    I think the problem is IPython sets the PyQt API to #1 for some APIs but not others. @patricksnape tried to fix this odd behavior but (I think) his PR was declined.

  3. mwaskom commented on Mar 8, 2017

    @mwaskom
    Author

    This PR I think you mean: #10101

  4. mwaskom commented on Mar 8, 2017

    @mwaskom
    Author

    I think the problem is IPython sets the PyQt API to #1 for some APIs but not others.

    However, unless I am misreading the docs, this shouldn't be the issue because QT_API=pyqt should set things to V2, as stated here:

    if QT_API=pyqt then PyQt4 will be used with the v2 API for QString and QVariant, so ETS codes like MayaVi will also work with IPython.

  5. mwaskom commented on Mar 8, 2017

    @mwaskom
    Author

    It does seem like the IPython gui machinery is setting the QDate API:

    [mlw-mbair ~]$ QT_API=pyqt ipython
    In [1]: import sip
    In [2]: sip.getapi('QDate')
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-2-0235c919091c> in <module>()
    ----> 1 sip.getapi('QDate')
    
    ValueError: unknown API 'QDate'
    In [3]: %gui qt
    In [4]: sip.getapi('QDate')
    Out[4]: 1
    
  6. ccordoba12 commented on Mar 9, 2017

    @ccordoba12
    Member

    So the problem is that %gui qt sets Sip API to #1 and that can't be changed to #2, which is what pyface is trying to do.

    In Spyder we adopted API #2 because #1 was used in PyQt 4.6 and older versions (and that's like 5 years old now). So far we haven't received complaints about it.

  7. mwaskom commented on Mar 9, 2017

    @mwaskom
    Author

    So the problem is that %gui qt sets Sip API to #1 and that can't be changed to #2, which is what pyface is trying to do.

    Then I'm correct at least that the IPython QT documentation is incorrect?

  8. patricksnape commented on Mar 11, 2017

    @patricksnape

    This is exactly the problem I was having and trying to fix in my PR

  9. mwaskom commented on Mar 11, 2017

    @mwaskom
    Author

    In light of my experience with this issue, I feel a little confused about why @patricksnape's PR (https://github.com/ipython/ipython/pull/10101/files) was rejected. @takluyver's position seems to be that IPython should leave un-set QT API components that it is not using. That make sense! However, that does not seem to be to existing behavior. %gui qt is setting QT API components to version 1, even when it shouldn't be (due to usage of the QT_API environment variable as specified in the docs).

    My issue isn't about dealing with headaches from mixing applications that want different QT APIs; it's simply about not being able to use a single application (mayavi) that wants version 2.

  10. Carreau commented on Mar 13, 2017

    @Carreau
    Member

    If this happen to be really problematic, and qt-users agree in majority that v2 should be set on all apis at once, then I think we can revisit #10101.

    had anyone had a look at https://pypi.python.org/pypi/Qt.py ?

  11. ccordoba12 commented on Mar 13, 2017

    @ccordoba12
    Member

    qt-users agree in majority that v2 should be set on all apis at once

    I'm +1 for that move. As I said, v1 is quite old and most probably very few projects are using it at the moment.

    had anyone had a look at https://pypi.python.org/pypi/Qt.py ?

    That package is a shim for Pyside/PyQt4/PyQt5, and there's another one called qtpy developed by us for Spyder.

    If there's a decision to adopt a Qt shim, I'd vote for qtpy because it's part of the community (Glue is also using it).

  12. added this to the 6.0 milestone on Mar 13, 2017
  13. Carreau commented on Mar 14, 2017

    @Carreau
    Member

    I thinks there was a consensus at today's meeting to reopen #10101 which is now done.
    we'll try to get this in soon.

  14. takluyver commented on Mar 14, 2017

    @takluyver
    Member

    The issue is that there are a bunch of separate APIs with different versions, each of which you need to pick one of, but as soon as PyQt is imported, they're all fixed. We're not explicitly setting QDate v1, but because we import PyQt to set up the event loop integration, we're implicitly setting the default PyQt gives us.

    I still suspect that we're likely to run into more issues if we suddenly start setting the QDate API to v2. Remember that anyone for whom it would break is not looking at this issue, so the +1s we see here are not a remotely representative sample.

  15. mwaskom commented on Mar 14, 2017

    @mwaskom
    Author

    That's fine but even if nothing changes in the code, I still think the docs are incorrect :)

    And FWIW, I am almost certain that my usecase worked fine until pretty recently. Not sure exactly what update broke it, though.

  16. 6 remaining items

  17. modified the milestones: 6.1, 6.0 on Apr 19, 2017
  18. mwaskom commented on Apr 24, 2017

    @mwaskom
    Author

    Just came across a somewhat older application of mine that used to work but now doesn't because of this issue: https://github.com/mwaskom/connexplore

    It's not super useful for pinning down exactly what changed, but it demonstrates that the ability to use --gui qt with mayavi has been broken at some point in the past two years.

  19. modified the milestones: 6.1, 6.2 on May 24, 2017
  20. mwaskom commented on Jun 15, 2017

    @mwaskom
    Author

    Not the be annoying but any progress here? My experience with this issue is that it is a serious regression that has made using mayavi-based tools in an IPython environment extremely difficult.

  21. modified the milestones: 6.2, 6.3 on Sep 11, 2017
  22. modified the milestones: 6.3, wishlist on Mar 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions