# Use Julia with Slicer

**URL:** <https://discourse.slicer.org/t/use-julia-with-slicer/16765>\
**Category:** Support\
**Created:** [March 25, 2021, 7:27pm UTC](https://discourse.slicer.org/t/use-julia-with-slicer/16765 "2021-03-25T19:27:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [March 25, 2021, 7:27pm UTC](https://discourse.slicer.org/t/use-julia-with-slicer/16765/1 "2021-03-25T19:27:35Z")

</div>

Hi,

When building SlicerCAT I’m thinking about installing Julia (+miniconda) instead of Slicer’s Python while keeping Slicer+Python tight interaction.  
If someone has any thoughts please give an advice.

---

<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 14, 2021, 6:05am UTC](https://discourse.slicer.org/t/use-julia-with-slicer/16765/2 "2021-04-14T06:05:52Z")

</div>

I don’t think anybody has tried to interface Slicer with Julia. We are pretty happy with the vast amount of packages available for Python and we can implement all performance-critical code in C++.

A couple of years ago we tried to [build Slicer against miniconda Python libraries and it seemed to work](https://www.slicer.org/wiki/Documentation/Labs/SlicerCondaIntegration), but since we can use `pip` in Slicer now, there is not much motivation in trying to use conda.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [April 28, 2021, 12:17am UTC](https://discourse.slicer.org/t/use-julia-with-slicer/16765/3 "2021-04-28T00:17:47Z")

</div>

I decided not to use miniconda so far but I’m going to use Julia.

To install Julia I’m going to use [crossplatform](https://julialang.org/downloads/platform/#cross-platform_installer) python module [Jill.py](https://github.com/johnnychen94/jill.py). It will install Julia in some dir inside build tree like `C:/C/d/julia-1.6`. Then I need to add Julia to CPack to make installation available for usual user. I can see that Slicer handles CPack modules via `SlicerBlockInstallMyLib.cmake` and `SlicerExtensionCPack.cmake` but there is quite a lot of code and for now I can’t understand how can I apply the same Slicer CPackaging style to Julia (maybe because it is time to go to sleep).

But anyway, if somebody has instruction please give me an advice.

---

<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:** [May 1, 2021, 2:20pm UTC](https://discourse.slicer.org/t/use-julia-with-slicer/16765/4 "2021-05-01T14:20:43Z")

</div>

You actually already have several options to run Julia scripts directly from Slicer as CLI modules.

You can implement CLI modules in any language. For now, you need to create an executable (sh, bat, py, exe,…) launcher the starts your script with Julia. But with a small change in Slicer core, you could implement direct Julia launching capability the same way as Python CLIs. See example here:

> **[lassoan/SlicerPythonCLIExample](https://github.com/lassoan/SlicerPythonCLIExample)**
>
> Example extension for 3D Slicer that demonstrates how to make a Python script available as a CLI module

If Julia.exe startup time is too slow then you can do the same that we did for Matlab. We implemented a very simple TCP server in Matlab that receives commands from Slicer executes them, and sends back to result to Slicer. See details here:

> **[PerkLab/SlicerMatlabBridge](https://github.com/PerkLab/SlicerMatlabBridge)**
>
> MatlabBridge extension for 3D Slicer. Contribute to PerkLab/SlicerMatlabBridge development by creating an account on GitHub.

Slicer also supports the Jupyter kernel protocol, so you can access Slicer features via this interface.

If you want to run Julia inside the Slicer process then it should be possible, too, but much more complicated. Python can be either extended (you load your libraries into Python and let Python execute it) or Python can be embedded (your application instantiates and manages a Python interpreter). We embed Python into Slicer so that we have better control over the application and to keep Python an optional component. Probably the simplest way to integrate Julia in the Slicer application is to embed Julia - if it has this option. You may be able to extend Julia with Slicer, this is how TCL integration worked years ago. For a few years, Slicer extended TCL and embedded Python. You can go back in the version history of Slicer and see how it was implemented.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [May 2, 2021, 11:09am UTC](https://discourse.slicer.org/t/use-julia-with-slicer/16765/5 "2021-05-02T11:09:18Z")

</div>

@lassoan thank you very much for consulting me.  
Even I’m not an experienced Julia user I can see that Julia has tight integration with python. Julia can call python via [PyCall](https://github.com/JuliaPy/PyCall.jl) and python is able to call Julia via [PyJulia](https://github.com/JuliaPy/pyjulia). So for now I mostly rely on this python-julia communication. I think the problem will arise when I start integrating Julia scripts and I will comeback to this thread.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [July 11, 2022, 8:22pm UTC](https://discourse.slicer.org/t/use-julia-with-slicer/16765/6 "2022-07-11T20:22:08Z")

</div>

> [@\`sys.executable\` is different when running from Slicer and from PythonSlicer](https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260/5):
>
> In the case of the Slicer application, you should instead considering using the [Application Startup File](https://slicer.readthedocs.io/en/latest/user_guide/settings.html#application-startup-file)

Didn’t know about this file, thank you.

> [@\`sys.executable\` is different when running from Slicer and from PythonSlicer](https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260/5):
>
> To provide a more targeted answer, could you provide more details ? What are you trying to achieve ?

It seems this is not an issue anymore but here is why started this topic:

I bundle julia programming language along with SlicerCAT and I wasn’t able to build a package ([pyjulia](https://github.com/JuliaPy/pyjulia)) because its dependency ([PyCall](https://github.com/JuliaPy/PyCall.jl)) used to be built against `python.exe` while pyjulia determined that it is invoked from `PythonSlicer.exe` (pyjulia used to raise an exception about this).

But as I figured out later the problem was within Slicer build tree python library is copied to `Slicer-build` and the inconsistency was invoked by that: PyCall links to `python-install/bin/python39.dll` and pyjulia links to `Slicer-build/bin/python39.dll`.

This is not an issue when SlicerCAT is installed (tested).

---

<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:** [July 11, 2022, 8:29pm UTC](https://discourse.slicer.org/t/use-julia-with-slicer/16765/7 "2022-07-11T20:29:30Z")

</div>

> PyCall links to `python-install/bin/python39.dll` and pyjulia links to `Slicer-build/bin/python39.dll`.

Technically, the linking is done at build-time against `python-install/libs/python39.lib`.

If you share a link to your custom application, we should be able to provide hints on how to set the launcher settings to ensure the proper dynamic libraries are resolved (by for example setting relevant launcher settings).

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [July 11, 2022, 8:45pm UTC](https://discourse.slicer.org/t/use-julia-with-slicer/16765/8 "2022-07-11T20:45:33Z")

</div>

I created CTK launcher around julia programming lang and I have set there env variables to resolve mentionned issue.

P.S. yesterday I made the repo private as I’m not sure about the future of the project. It is an enthusiastic project but I spend too much time on this without any interest in the sense of sponsorship so far.
