# How to run python script in Slicer?

**URL:** https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352
**Category:** Development
**Created:** [July 3, 2020, 8:13am UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352 "2020-07-03T08:13:26Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![SummerZ2020](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/summerz2020/32/7137_2.png) [@SummerZ2020](https://discourse.slicer.org/u/SummerZ2020)
#### Post date: [July 3, 2020, 8:13am UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/1 "2020-07-03T08:13:26Z")

</div>

Operating system:Win10  
Slicer version:4.11.0(2020.6.15)  
Expected behavior:The saveLabel.py should be run when I run `C:\ProgramData\NA-MIC\Slicer 4.11.0-2020-06-15>Slicer.exe --python--script C:\Users\310091560\Desktop\saveLabel.py` in the command line window.  
Actual behavior: nothing happened except the Slicer main window(python interactor) openning.

Is this command line `C:\ProgramData\NA-MIC\Slicer 4.11.0-2020-06-15>Slicer.exe --python--script C:\Users\310091560\Desktop\saveLabel.py` right to run a python script?  
Thanks in advance.

BTW, is the tutorial “[[Documentation/Nightly/Extensions/DebuggingTools - Slicer Wiki](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/DebuggingTools)]([http://Visual](http://Visual) Python debugging in PyCharm)” updated according to the 4.11 version? I cannot find the Python debugger extension and the “Python debugger” module (in “Developer Tools” category).  
Thanks again.

---

<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: [July 3, 2020, 3:55pm UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/2 "2020-07-03T15:55:40Z")

</div>

> [@SummerZ2020](#):
>
> C:\ProgramData\NA-MIC\Slicer 4.11.0-2020-06-15\>Slicer.exe --python–script C:\Users\310091560\Desktop\saveLabel.py

The correct argument name is `--python-script` and not `--python--script`.

---

<div class="post-metadata">

### Author: ![SummerZ2020](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/summerz2020/32/7137_2.png) [@SummerZ2020](https://discourse.slicer.org/u/SummerZ2020)
#### Post date: [July 8, 2020, 1:39am UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/3 "2020-07-08T01:39:49Z")

</div>

So simple mistake, I should be more careful. Thanks a lot.

---

<div class="post-metadata">

### Author: ![Gokce\_Guven](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gokce_guven/32/66833_2.png) [@Gokce\_Guven](https://discourse.slicer.org/u/Gokce_Guven)
#### Post date: [July 30, 2023, 8:18pm UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/4 "2023-07-30T20:18:14Z")

</div>

Hi Mr. Lasso,

I am trying to run a python script from Slicer 5.3.0 version on Ubuntu 22.04 OS. When I run it from the terminal like this:

./Slicer --python-script /home/username/ls.py

it gives this error eventhough I have tkinter on my system:  
/lib/python3.9/ls.py  
Switch to module: “Welcome”  
Loading Slicer RC file [/home/gokce/.slicerrc.py]  
:1: DeprecationWarning: The symbol module is deprecated and will be removed in future versions of Python  
NOTE: You must install tkinter on Linux to use MouseInfo. Run the following: sudo apt-get install python3-tk python3-dev  
Switch to module: “”  
Switch to module: “”

When I put the ls.py script inside the folder: /home/username/Slicer-5.3.0-2023-07-29-linux-amd64/lib/Python/lib/python3.9/ folder and start the slicer and import the ls script from the python console and call a function from the ls script it gives this error:

```auto
>>> import ls
>>> ls.start()
  File "<console>", line 1
    import ls
             ^
SyntaxError: multiple statements found while compiling a single statement
>>> 

```

How should I proceed?

Thanks in advance

---

<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: [July 31, 2023, 1:11am UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/5 "2023-07-31T01:11:29Z")

</div>

Slicer uses its own virtual Python environment (you don’t need to install Python in your system). If you want to install a package in Sicer’s virtual Python environment then you can use the `pip_install` convenience function in the Slicer Python console.

---

<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: [July 31, 2023, 2:27am UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/6 "2023-07-31T02:27:07Z")

</div>

`SyntaxError: multiple statements found while compiling a single statement` error means that statements are not separated by newline or semicolon (`;`). Maybe during copy-paste, the newline character gets replaced by a space? You may try to copy-paste from a different software or add semicolon after each statement (`import ls; ls.start()`).

I would not recommend to put any files into the Python library folder (`/home/username/Slicer-5.3.0-2023-07-29-linux-amd64/lib/Python/lib/python3.9/`). Instead you can put it anywhere and run it by calling as you did (`./Slicer --python-script /home/username/ls.py`). You can ignore the deprecation warning. When Slicer will upgrade to a Python version that does not contain tkinter anymore (or even before that) you can switch to Qt, which is already bundled with Slicer.

---

<div class="post-metadata">

### Author: ![Gokce\_Guven](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gokce_guven/32/66833_2.png) [@Gokce\_Guven](https://discourse.slicer.org/u/Gokce_Guven)
#### Post date: [July 31, 2023, 8:00am UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/7 "2023-07-31T08:00:55Z")

</div>

Dear Mr. Lasso, thanks a lot for the detailed and fast response. When I run it from the terminal like this:

```auto
 ./Slicer --python-script ../ls.py 
Switch to module: "Welcome"
Loading Slicer RC file [/home/gokce/.slicerrc.py]
<string>:1: DeprecationWarning: The symbol module is deprecated and will be removed in future versions of Python
NOTE: You must install tkinter on Linux to use MouseInfo. Run the following: sudo apt-get install python3-tk python3-dev
Switch to module: ""
Switch to module: ""

```

I forgot to add that the slicer is opening for a second and closing itself. The ls.py script is using lung segmenter plugin in slicer and the same code is working on Windows. I want to run it on Ubuntu 22.04.2, I do not know what is wrong. I have tried many possible solutions that I have found on web. When I call the code from the python console without space like this:

```python
Python 3.9.10 (main, Jul 30 2023, 03:07:06)

[GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] on linux2

>>>

Loading Slicer RC file [/home/username/.slicerrc.py]

>>> import ls;ls.start()
>>>

```

it is not giving any output, I cannot understand what may be possibly wrong.

---

<div class="post-metadata">

### Author: ![Gokce\_Guven](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gokce_guven/32/66833_2.png) [@Gokce\_Guven](https://discourse.slicer.org/u/Gokce_Guven)
#### Post date: [July 31, 2023, 10:45am UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/8 "2023-07-31T10:45:56Z")

</div>

@lassoan Hi Mr. Lasso,

I have tried to comment out one of my python package “pyautogui” and uninstall it from the Slicer, then my code worked. I wanted to take screenshots of the segmentation for each data with pyautogui. Do you have any suggestion for collecting the screenshots for each data?

Thanks in advance

---

<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: [July 31, 2023, 11:46am UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/9 "2023-07-31T11:46:46Z")

</div>

You can use the Screen Capture module to take screenshots. See the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#screen-capture) for examples.

---

<div class="post-metadata">

### Author: ![Gokce\_Guven](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gokce_guven/32/66833_2.png) [@Gokce\_Guven](https://discourse.slicer.org/u/Gokce_Guven)
#### Post date: [July 31, 2023, 12:36pm UTC](https://discourse.slicer.org/t/how-to-run-python-script-in-slicer/12352/10 "2023-07-31T12:36:06Z")

</div>

thanks a lot Mr. Lasso, that solved my problem  
Have a great day
