# Run python script from command line not working

**URL:** <https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453>\
**Category:** Support\
**Created:** [March 11, 2022, 1:05pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453 "2022-03-11T13:05:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [March 11, 2022, 1:05pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/1 "2022-03-11T13:05:16Z")

</div>

Hi all,

I want to do the same processing to several volumes so it would be very useful to use slicer code from  
command line.  
What I want is: open volume → process → export processed volume. I guess I can do it with the same code I’m using in the python interactor one by one.

I’ve been searching in other posts how to do it. I tried:

```auto
C:\Users\Belen\AppData\Local\NA-MIC\"Slicer 4.11.20210226"\Slicer.exe --python-script "C:\Users\Belen\Desktop\pruebaSlicer.py" -no-splash --no-main-window | more

```

and --launch python-real instead --python-script but none of them are working.

`C:\Users\Belen\AppData\Local\NA-MIC\"Slicer 4.11.20210226"\Slicer.exe` opens slicer but `C:\Users\Belen\AppData\Local\NA-MIC\"Slicer 4.11.20210226"\Slicer.exe --help | more` is not showing anything.

I do not know what I am doing wrong.

Thanks

---

<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:** [March 11, 2022, 10:12pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/2 "2022-03-11T22:12:56Z")

</div>

Hi -

This has to do with the way windows handles output. You don’t always see output in the command console the way you would on a linux or mac system (some windows expert can explain the details). But Slicer does run the script and if you need to get output from it you can write to a file.

For example if you run this script you should see the output printed in the python console for a second before the program disappears. (Assumes you had the python console open when you last exited slicer and you don’t use the --no-main-window option).

```auto
print(40+2)
qt.QTimer.singleShot(1000, exit)

```

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [March 11, 2022, 10:20pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/3 "2022-03-11T22:20:56Z")

</div>

I usually do batch processing within the same Slicer session. I process one dataset, close the scene (`slicer.mrmlScene.Clear()`) and can proceed with the next one.

Btw I also think your script should work and it may be an issue with the output the way you do it.

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [March 12, 2022, 11:31am UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/4 "2022-03-12T11:31:54Z")

</div>

Hi,

I runned this script but nothing is happening. It seems like Slicer is not running the code.

(base) C:\Users\Belen\>C:\Users\Belen\AppData\Local\NA-MIC"Slicer 4.11.20210226"\Slicer.exe --python-script C:\Users\Belen\Desktop\pruebaSlicer.py

And pruebaSlicer.py:

````auto
print(40+2)
qt.QTimer.singleShot(1000, exit)```
````

---

<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:** [March 12, 2022, 3:44pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/5 "2022-03-12T15:44:32Z")

</div>

Do you still have the three backtick characters after the second line? (the `````). These would be a python syntax error that might not be reported. Make sure whatever is in your script file works when typed into the python console in Slicer.

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [March 12, 2022, 10:46pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/6 "2022-03-12T22:46:06Z")

</div>

No, I do not have the ``` (it was a typo). When I run the command, slicer did not even open. But it opens if I do not write “–python-script …”

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [March 12, 2022, 11:01pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/7 "2022-03-12T23:01:04Z")

</div>

Oh, I already make it. I changed directory to the slicer.exe path. It’s weird… thank you so much. I will try the processing code.

---

<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:** [March 13, 2022, 3:06am UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/8 "2022-03-13T03:06:11Z")

</div>

The problem was that Slicer executable path was specified with an incorrect syntax (misplaced quotation mark). It seems that Windows can tolerate this, but the command-line argument parser in the Slicer launcher is more strict.

Solution: a path that contain spaces must be quoted by placing a quotation mark at the beginning and at the end.

For example, instead of this (incorrect):

> [@bserrano](#):
>
> `C:\Users\Belen\AppData\Local\NA-MIC\"Slicer 4.11.20210226"\Slicer.exe`

Slicer.exe path must be specified like this:

```
"C:\Users\Belen\AppData\Local\NA-MIC\Slicer 4.11.20210226\Slicer.exe"

```

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [March 15, 2022, 9:49am UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/9 "2022-03-15T09:49:28Z")

</div>

Thanks for the explanation.  
I noticed that when I run the script (no --no-main-window) the code continues running even when I close the program (slicer). How can I stop the execution without having to do it by killing a process?

---

<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:** [March 15, 2022, 12:37pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/10 "2022-03-15T12:37:31Z")

</div>

You can call `exit()` when your processing is finished.

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [March 15, 2022, 1:54pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/11 "2022-03-15T13:54:29Z")

</div>

I meant something similar to ctrl+C for the cmd.

---

<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:** [March 17, 2022, 1:19am UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/12 "2022-03-17T01:19:07Z")

</div>

Slicer launcher (Slicer.exe) is built as a GUI application in the official Slicer installation packages. GUI applications are not killed if you hit Ctrl-C.

There is a trick that you can use if you really must use Ctrl-C instead of the X button. We include a console-mode launcher `PythonSlicer.exe`, you can rename that and replace the original GUI-mode `Slicer.exe` launcher. The console-mode launcher will respond to Ctrl-C on the latest Slicer Preview Release (because there the SlicerApp-real.exe executable is also built as a console application). However killing the process like this will not result in a clean exit, because objects are not destructed so VTK will report memory leaks. Stable releases might be built with error leak reporting disabled, but that will not stop on Ctrl-C (because in that version SlicerApp-real.exe is not a console application yet). If you want a solution for a clean exit from the command prompt using Ctrl-C then you need to build Slicer yourself or wait for the new Slicer Stable Release (expected in a week or so).

What is your overall goal? How would you like to launch and stop Slicer?

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [March 21, 2022, 7:47am UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/13 "2022-03-21T07:47:53Z")

</div>

Ok, many thanks for the explanation.

I am just curious. I have codes that operate on many models and sometimes it is very useful to be able to terminate the execution of the program abruptly, in the same way as with another console program.

---

<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:** [March 21, 2022, 12:30pm UTC](https://discourse.slicer.org/t/run-python-script-from-command-line-not-working/22453/14 "2022-03-21T12:30:03Z")

</div>

Your can use the latest Slicer Preview Release with the console-mode launcher as described above; and ignore the listed memory leaks.

Alternatively, you can terminate the process tree using Processes Explorer (Sysinternals utility); or look up the the SlicerApp-real.exe process and terminate it.
