Updating slicer to work with python 3

I am interested in having slicer work with python 3 as I am already using python 3 in other parts of my workflow, and would be nice to not have make my scripts compatible across both.

I have heard that this is planned but didn’t know if was formerly documented all the pieces that will be affected by the transition?

What would be required to do this, what pieces need to be updated and made compatible?

~Kurt

Thanks for reaching out.

would be nice to not have make my scripts compatible across both.

If you are not using feature specific to python 3, your scripts will be compatible. To ensure this is the case, you could also make sure of the Six: Python 2 and 3 Compatibility Library, it is available in Slicer.

all the pieces that will be affected by the transition?

Assuming your code is forward compatible with python 3 (you could for example look at Cheat Sheet: Writing Python 2-3 compatible code, most of the infrastructure change will be transparent for your project.

Indeed, the Slicer build-system API that will be unaffected, only the implementation of CMake function like SlicerMacroBuildModuleQtLibrary will be changed internally. And other change will also be specific to Slicer core, and will be related to class like qSlicerScriptedUtils. But that should not affect the building and distribution of your project.

This past week, we were busy modernizing the Slicer code base. See C++11 modernization, update to ITK5, removal of obsolete Qt4 and VTK7 support

This delayed the transition to Python 3. The associated effort will be resumed on March 25th.

In the mean time, here is a work-in-progress topic based on some preliminary work from @ihnorton

Cc: @bpaniagua @Andinet_Enquobahrie

Hi,

Slicer can now start with Python 3 ! A little more work to finalize the update of scripted modules but we are getting there.

image

Jc

4 Likes

Still not available on nightly :cry:

image

Sorry for the confusion, the support for Python3 is currently only available through this PR. See Glyph visualization inconsistent across viewers · Issue #1118 · Slicer/Slicer · GitHub

The pull request is now reading for testing.

Next steps (before merging):

  • test packaging
  • update migration guide

Then (after merging):

  • update few extensions

Open questions:

  • (1) should we package dependency (potentially specified in a requirement.txt) in extensions
  • or (2) should we install them when the extension is installed.

Leaner extension packages would be nicer and probably version requirements could be managed more flexibly if python packages would be downloaded dynamically. However, it should be possible to install extensions from file, without network connection.

Updates:

Slicer has been updated to only support building against Python3 and all python scripts available in the main Slicer code base are only compatible with Python3.

On the other hand, extensions (e.g MultiVolumeExplorer, MultiVolumeImporter and SlicerSimpleFilters are expected to support both Python2 and Python3. More details about the different approach in the migration guide liked above.

1 Like

Set of changes concluding the transition to Python3 has been integrated in r28100 to r28129

Notes:

  • Given the extend of the change, developers are expected to perform a clean build.

Next:

  • Fix failing tests
  • Integrate SciPy
  • Submit Pull Requests for updating few more extensions. This will provide additional examples.
  • Finalize plan to support requirements for installing dependencies from the Python Package Index (https://pypi.org/)

You are awesome Jc! I’ve started a build on Windows, I’ll let you know if I run into any issues.

After fixing Python’s Modules_io_iomodule.c, Windows build succeeded in both release and debug mode.

In release mode, everything seems to work fine - great! In debug mode, Slicer hangs at startup. See a fix here: https://github.com/lassoan/Slicer/tree/python-startup-hang-in-debug-mode. @jcfr could you please review and merge?

1 Like

After fixing Python’s Modules_io_iomodule.c, Windows build succeeded in both release and debug mode.

Thanks for looking into this. I will update GitHub - python-cmake-buildsystem/python-cmake-buildsystem: A cmake buildsystem for compiling Python to integrate this.

In debug mode, Slicer hangs at startup. See a fix here: https://github.com/lassoan/Slicer/tree/python-startup-hang-in-debug-mode. @jcfr could you please review and merge?

Thanks for the patch. I will review and integrate shortly.

@jcfr Should we add a wiki page or online spreadsheet where we can quickly add any issues that we find (to make sure we don’t forget about them or work on them in parallel)? Mantis would be too heavyweight for this.

For example, I’ve just found that restart() Python function does not work anymore (has no effect).

I started to document these type of problems here: Documentation/Labs/Slicer5-roadmap - Slicer Wiki

hey @jcfr,
thanks for all your replies…i was having the similar sort of question and your solutions worked for me.
best regards!!

1 Like