# Using Slicer and Slicer Modules from Command-Line

**URL:** <https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162>\
**Category:** Support\
**Created:** [August 24, 2019, 6:28pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162 "2019-08-24T18:28:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt-warkentin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matt-warkentin/32/4962_2.png) [@matt-warkentin](https://discourse.slicer.org/u/matt-warkentin)\
**Post date:** [August 24, 2019, 6:28pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/1 "2019-08-24T18:28:14Z")

</div>

Hi,

I am fairly expert in `R` and other languages, but I am admittedly relatively novice with `python` (though I am working to improve).

I think I am conceptually unclear on what it means to run `Slicer`/`Slicer` modules from the command line. Should running `Slicer` from the command-line be conceptually viewed as a running code/scripts inside a complete `python` environment (like a `conda` environment), whereby it contains a `python` interpreter and all necessary dependencies/modules? Could someone perhaps provide a high-level conceptual overview that might clarify what it actually means to run `Slicer` modules at the command-line?

I feel like connecting some of these dots may lead to a “eureka” moment for me, and I can begin to further appreciate using `Slicer` from the command-line. If it helps to place this into a more specific context, I am interested in learning how to perform batch `pyradiomics` feature extraction using the `SlicerRadiomicsCLI` module.

I have had no issues running the stand-alone `pyradiomics` CLI-tool, however, learning how to run effectively run `Slicer` modules from the command-line has broader benefit, given the large number of useful modules for other important image processing tasks.

Thanks you.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 26, 2019, 2:45pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/2 "2019-08-26T14:45:27Z")

</div>

Hi Matt -

It sounds like you are on the right track - we try to support many different ways of accessing Slicer functionality but sometimes you do need to investigate a bit to decide what’s the most streamlined way to accomplish what you need. Some of these practices are changing a bit now that we have Python3 support.

For streamlined batch processing and command line use, the python interpreter (`PythonSlicer`) can be used to access all the packages installed in Slicer, but won’t have the application context so many things won’t be available (e.g. rendering etc). You can use `slicer.util.pip_install()` to add custom packages.

From a shell, you can run many of the CLI modules as commands, using Slicer’s launcher (e.g. `Slicer --launch BRAINSFit`), which is a very lightweight way to access the ITK based C++ code.

Or if you need a full environment where you can access any of the code you can use the full Slicer application and pass it whatever startup script you want (the expense is a little extra startup overhead).

Hope that helps,  
Steve

---

<div class="post-metadata">

**Author:** ![matt-warkentin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matt-warkentin/32/4962_2.png) [@matt-warkentin](https://discourse.slicer.org/u/matt-warkentin)\
**Post date:** [August 26, 2019, 4:06pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/3 "2019-08-26T16:06:36Z")

</div>

Hi Steve,

Thanks for the detailed reply. Could you perhaps clarify what you mean when you say I could use the `PythonSlicer` python interpreter? I know what a python interpreter is, in general, is the `PythonSlicer` interpreter just the python interpreter that is available to use inside the Slicer GUI (i.e. View \> Python Interactor)? Or is this something different entirely?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 26, 2019, 7:28pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/4 "2019-08-26T19:28:50Z")

</div>

Hi Matt -

`PythonSlicer` is an executable provided in the bin directory of the Slicer binaries. It’s the same version of python that’s included with the app, but it acts like a normal `python` (or `python3`) executable in terms of running scripts. So you can use it in place of regular python to make use of the Slicer environment for batch / cli operations.

Best,  
Steve

---

<div class="post-metadata">

**Author:** ![matt-warkentin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matt-warkentin/32/4962_2.png) [@matt-warkentin](https://discourse.slicer.org/u/matt-warkentin)\
**Post date:** [August 26, 2019, 8:10pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/5 "2019-08-26T20:10:19Z")

</div>

Oh, okay, I did not know that was provided with the Slicer binary. I was able to locate this `PythonSlicer` executable and run it with success. Thank you very much for clarifying.

Is `PythonSlicer` the interpreter that is used when one uses the Slicer kernel in a Jupyter Notebook? Or is this different yet again?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 26, 2019, 8:49pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/6 "2019-08-26T20:49:59Z")

</div>

It’s basically the same python code and environment, but where `PythonSlicer` is a headless interpreter, SlicerJupyter allows you to control a running Slicer application via the notebook. So Jupyter is basically doing the same thing as the python console in Slicer.

---

<div class="post-metadata">

**Author:** ![matt-warkentin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matt-warkentin/32/4962_2.png) [@matt-warkentin](https://discourse.slicer.org/u/matt-warkentin)\
**Post date:** [August 27, 2019, 7:29pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/7 "2019-08-27T19:29:35Z")

</div>

So, I tried to run `pyradiomics` from a shell using Slicer by locating what I thought was the correct `python` script (`SlicerRadiomics.py`) located in `Slicer > Contents > Extensions > SlicerRadiomics > lib > Slicer-4.10 > qt-scripted-modules` and ran the following code:

```auto
Slicer --no-main-window --python-script SlicerRadiomics.py

```

However, this did not seem to work. Am I on the right track?

**EDIT** : I decided to try using the `pyradiomics` binary located in `Slicer > Contents > Extensions > SlicerRadiomics > bin` and it seems to work…

```auto
python pyradiomics --help

```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 27, 2019, 7:44pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/8 "2019-08-27T19:44:59Z")

</div>

Note that here you might want to launch `PythonSlicer` so that you are sure to have the same python and pyradiomics installation as you have in the Slicer application. (SlicerRadiomics extension is the GUI wrapper that bundles pyradiomics into Slicer).

---

<div class="post-metadata">

**Author:** ![matt-warkentin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matt-warkentin/32/4962_2.png) [@matt-warkentin](https://discourse.slicer.org/u/matt-warkentin)\
**Post date:** [August 27, 2019, 8:08pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/9 "2019-08-27T20:08:14Z")

</div>

I must be doing something wrong but I was able to successfully extract features using

```auto
Slicer --no-main-window --python-script pyradiomics image.nrrd mask.nrrd

```

and

```auto
python pyradiomics image.nrrd mask.nrrd

```

Which uses the python interpreter found in my `$PATH` (`/anaconda3/bin/python`), but not when I do:

```auto
PythonSlicer pyradiomics image.nrrd mask.nrrd

```

> Traceback (most recent call last):  
> File “pyradiomics”, line 7, in   
> from radiomics.scripts. **init** import parse\_args  
> ImportError: No module named radiomics.scripts. **init**

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 27, 2019, 8:22pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/10 "2019-08-27T20:22:10Z")

</div>

Right, that makes sense. You need to run it like: `./Slicer --launch PythonSlicer` so it has the right environment (paths to the python libraries).

The first form you tried uses the `Slicer` application as the interpreter, which is why you need the extra options to turn off the main window and run the script - this uses the same basic python environment, but with the extra overhead of the GUI app. The second form uses your anaconda install, which might work too but also might have different packages or versions than the Slicer python.

---

<div class="post-metadata">

**Author:** ![matt-warkentin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matt-warkentin/32/4962_2.png) [@matt-warkentin](https://discourse.slicer.org/u/matt-warkentin)\
**Post date:** [August 27, 2019, 8:45pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/11 "2019-08-27T20:45:28Z")

</div>

Thank you for your patience. I don’t think I am clear on what exactly `Slicer --launch PythonSlicer` is really doing. When I run that code, a `Slicer` GUI is opened and nothing else appears to happen; so I don’t see the clear difference between using `Slicer --python-script` and `Slicer --launch SlicerPython`.

---

<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:** [August 27, 2019, 8:57pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/12 "2019-08-27T20:57:30Z")

</div>

> [@pieper](#):
>
> Right, that makes sense. You need to run it like: `./Slicer --launch PythonSlicer`

PythonSlicer inherits all the settings from the Slicer launcher, so it should not be necessary to run it using `. /Slicer`. Have you experienced issues running when simply running `PythonSlicer`?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 27, 2019, 9:17pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/13 "2019-08-27T21:17:14Z")

</div>

Yes, I did test that. Running `PythonSlicer` without the launcher I cannot `import radiomics` but with the launcher I can (on linux nightly from 2019-08-22).

---

<div class="post-metadata">

**Author:** ![matt-warkentin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matt-warkentin/32/4962_2.png) [@matt-warkentin](https://discourse.slicer.org/u/matt-warkentin)\
**Post date:** [August 27, 2019, 10:13pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/14 "2019-08-27T22:13:14Z")

</div>

I anticipated when running `Slicer --launch PythonSlicer` that it would just open a python interpreter in the shell; but it seems to also open the Slicer GUI. Is this the expected behaviour?

Because calling `PythonSlicer` alone of course just opens a python interpreter.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 28, 2019, 12:12am UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/15 "2019-08-28T00:12:15Z")

</div>

Ah, are you on mac? The launcher is part of the mac build tree but not the official entrypoint for the app in the binaries now that I think about it. On linux Slicer --launch PythonSlicer does just bring up the interpreter.

---

<div class="post-metadata">

**Author:** ![matt-warkentin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matt-warkentin/32/4962_2.png) [@matt-warkentin](https://discourse.slicer.org/u/matt-warkentin)\
**Post date:** [August 28, 2019, 1:17am UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/16 "2019-08-28T01:17:51Z")

</div>

Yes, sorry. I should’ve provided that context. I am currently running `Slicer 4.10.1` on a Macbook Pro running `Mojave 10.14.4`.

---

<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:** [August 28, 2019, 2:48pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/17 "2019-08-28T14:48:08Z")

</div>

Could you please try the latest stable (4.10.2) and latest preview versions, too?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 28, 2019, 6:39pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/18 "2019-08-28T18:39:02Z")

</div>

> [@matt-warkentin](#):
>
> Yes, sorry. I should’ve provided that context. I am currently running `Slicer 4.10.1` on a Macbook Pro running `Mojave 10.14.4` .

Yes, for that case maybe you need to stick with the `--no-main-window --python-script` approach - it’s a bit more overhead but maybe it doesn’t matter.

---

<div class="post-metadata">

**Author:** ![matt-warkentin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/matt-warkentin/32/4962_2.png) [@matt-warkentin](https://discourse.slicer.org/u/matt-warkentin)\
**Post date:** [August 28, 2019, 6:56pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/19 "2019-08-28T18:56:39Z")

</div>

@lassoan Yes, I can try these more recent versions. I won’t have the chance to test them for maybe two weeks as I am travelling for work. But I promise to test things out when I get back home and I will report back.

@pieper Good to know. Thank you very much for your help.

One last question – I am under the impression that some Slicer functionality/modules rely on using `plastimatch`. Does this mean the `plastimatch` binary is buried somewhere inside the Slicer file tree? I’m wondering if by installing Slicer, one now has the `plastimatch` binary behind-the-scenes which can be exposed at the command-line.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 28, 2019, 7:24pm UTC](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162/20 "2019-08-28T19:24:45Z")

</div>

> [@matt-warkentin](#):
>
> Slicer functionality/modules rely on using `plastimatch` . Does this mean the `plastimatch` binary is buried somewhere inside the Slicer file tree?

I believe you get most if not all `plastimatch` features via [SlicerRT](http://slicerrt.github.io/). (I’m sure someone will correct me if I’m wrong on that!).

[Next page](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162.md?page=2)
