# Updating slicer to work with python 3

**URL:** <https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662>\
**Category:** Development\
**Tags:** build\
**Created:** [November 6, 2018, 7:31pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662 "2018-11-06T19:31:37Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)\
**Post date:** [November 6, 2018, 7:31pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/1 "2018-11-06T19:31:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [November 7, 2018, 5:57am UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/2 "2018-11-07T05:57:21Z")

</div>

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](https://pythonhosted.org/six), 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](https://python-future.org/compatible_idioms.html), 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](https://github.com/Slicer/Slicer/blob/36997169adf0e323015fb368aeb3ea9ecdb2e793/CMake/SlicerMacroBuildModuleQtLibrary.cmake#L239-L260) will be changed internally. And other change will also be specific to Slicer core, and will be related to class like [qSlicerScriptedUtils](https://github.com/Slicer/Slicer/blob/master/Base/QTCore/qSlicerScriptedUtils_p.h). But that should not affect the building and distribution of your project.

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 16, 2019, 8:17pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/3 "2019-03-16T20:17:40Z")

</div>

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](https://discourse.slicer.org/t/c-11-modernization-update-to-itk5-removal-of-obsolete-qt4-and-vtk7-support/6165)

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

> <https://github.com/Slicer/Slicer/issues/1118>
>
> \_This issue was created automatically from an original \[Mantis Issue\](https://ma…ntisarchive.slicer.org/view.php?id=1118). Further discussion may take place here.\_

Cc: @bpaniagua @Andinet_Enquobahrie

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [April 2, 2019, 2:16pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/4 "2019-04-02T14:16:29Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/1/51ef01cb2da456351ae01b397e7b00c10abb2ad9.png)

Jc

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [April 4, 2019, 2:54pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/5 "2019-04-04T14:54:25Z")

</div>

Still not available on nightly 😢

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/1/b15c8967407a3542d70399e5e9446f3c2f56bb91.png)

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [April 4, 2019, 5:09pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/6 "2019-04-04T17:09:27Z")

</div>

> [@Alex\_Vergara](#):
>
> Still not available on nightly :’(

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](https://github.com/Slicer/Slicer/pull/1118)

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [April 4, 2019, 8:21pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/7 "2019-04-04T20:21:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 4, 2019, 9:58pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/8 "2019-04-04T21:58:24Z")

</div>

> [@jcfr](#):
>
> - (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.

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [April 11, 2019, 8:04pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/9 "2019-04-11T20:04:16Z")

</div>

Updates:

- Local testing is completed. Linux, macOS and Windows packages can be generated.
- Migration guide has been updated:
  - [Slicer 5.0: API changes since 4.10 - Python 2 to Python 3](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MigrationGuide#Python_2_to_Python_3)
  - [Slicer Extension updates - Slicer 5.0: Python2 to Python3](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MigrationGuide#Slicer_5.0:_Python2_to_Python3)

- Changes will be integrated this afternoon.

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](https://github.com/fedorov/MultiVolumeExplorer/pull/44), [MultiVolumeImporter](https://github.com/fedorov/MultiVolumeImporter/pull/38) and [SlicerSimpleFilters](https://github.com/SimpleITK/SlicerSimpleFilters/pull/16) are expected to support both Python2 and Python3. More details about the different approach in the migration guide liked above.

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [April 11, 2019, 8:09pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/10 "2019-04-11T20:09:27Z")

</div>

Set of changes concluding the transition to Python3 has been integrated in [r28100](http://viewvc.slicer.org/viewvc.cgi/Slicer4?view=revision&revision=28100) to [r28129](http://viewvc.slicer.org/viewvc.cgi/Slicer4?view=revision&revision=28129)

Notes:

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

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [April 11, 2019, 8:12pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/11 "2019-04-11T20:12:51Z")

</div>

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/](https://pypi.org/))

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 12, 2019, 2:17am UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/12 "2019-04-12T02:17:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 12, 2019, 3:43pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/13 "2019-04-12T15:43:24Z")

</div>

After fixing Python’s [Modules\_io\_iomodule.c](https://github.com/Slicer/Slicer/pull/1118#issuecomment-482436689), 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](https://github.com/lassoan/Slicer/tree/python-startup-hang-in-debug-mode). @jcfr could you please review and merge?

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [April 12, 2019, 4:47pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/14 "2019-04-12T16:47:39Z")

</div>

> After fixing Python’s [Modules\_io\_iomodule.c](https://github.com/Slicer/Slicer/pull/1118#issuecomment-482436689), 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](https://github.com/python-cmake-buildsystem/python-cmake-buildsystem) 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](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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 12, 2019, 6:51pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/15 "2019-04-12T18:51:40Z")

</div>

@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).

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [April 12, 2019, 6:58pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/16 "2019-04-12T18:58:43Z")

</div>

> [@lassoan](#):
>
> Should we add a wiki page

I started to document these type of problems here: [Documentation/Labs/Slicer5-roadmap - Slicer Wiki](https://www.slicer.org/wiki/Documentation/Labs/Slicer5-roadmap#Python3)

---

<div class="post-metadata">

**Author:** ![MARKRONSON](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@MARKRONSON](https://discourse.slicer.org/u/MARKRONSON)\
**Post date:** [April 19, 2019, 6:00pm UTC](https://discourse.slicer.org/t/updating-slicer-to-work-with-python-3/4662/17 "2019-04-19T18:00:26Z")

</div>

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