How to run python script in Slicer?

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](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.

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

1 Like

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

1 Like

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:

>>> 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

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.

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.

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

 ./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 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.

@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

1 Like

You can use the Screen Capture module to take screenshots. See the script repository for examples.

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

1 Like