Slicer --no-main-window --python-script in windows command prompt not working

I am trying to submit a python code to slicer python interactor via the windows command line. I can run my slicer using --python-script mypath. When I add --no-main-window before the --python-script mypath, the code does not run.

Thanks!
AC

Maybe it helps if you look at an example for a script that does run without main window:

My .py file runs when using --python-script, without --no-main-window prior.

Is any error displayed? Is any error logged in the application log? Which Slicer version do you use?

Currently using Slicer 4.8.1 , there is no error it seems to be running in the background when I look at the task manager however the that I request to save in the python code are not being saved.

Let us know if the problem persists with latest stable version (4.10.1).

Hi @lassoan, same problem on Win10 PowerShell, Slicer-4.10.2:

.\Slicer.exe --help
.\Slicer.exe --launch python-real

Nothing happens, no errors are displayed either.

It works as intended but I can see how this may be confusing.

In official Slicer releases on Windows, Slicer.exe is not a console application, so you just don’t see the output. It is still there, for example run this instead (more is a console application, so it can display text):

.\Slicer.exe --help | more

For Python, we included a launcher that is built as a console application. Use that for running Python interactively:

.\SlicerPython.exe

@lassoan, I am aware of SlicerPython.exe . But that won’t have knowledge of additional settings like Slicer.exe --launch python-real has.

See my use case below:

C:\Users\tashr\Downloads\“Slicer 4.10.2”\Slicer.exe --launch python-real .\Test\run_test.py

The above command is the Windows variant of a Linux command minus the .exe, that I would like to run on PowerShell. Let me know if there is a way of doing that.

By the way, I followed your | more idea and the following works fine:

C:\Users\tashr\Downloads\“Slicer 4.10.2”\Slicer.exe --launch python-real .\Test\run_test.py | more

PythonSlicer.exe inherits all launcher settings from Slicer.exe (as you can see it from additionalSettingsFilePath=<APPLAUNCHER_SETTINGS_DIR>/SlicerLauncherSettings.ini line in PythonSlicerLauncherSettings.ini), and it runs python-real.exe. Therefore, Slicer.exe --launch python-real.exe ...args... does exactly the same as PythonSlicer.exe ...args..., the only difference is that one is a console application, while the other is not.

By the way, you can capture output of Slicer.exe by redirecting it to an output file:

Slicer.exe --help >output.txt

Sorry, I should have mentioned, SlicerPython.exe .\Test\run_test.py did not find one of the SlicerDMRI modules I installed.

I do confirm that the module is found by:

  • Slicer GUI
  • C:\Users\tashr\Downloads\“Slicer 4.10.2”\Slicer.exe --launch python-real .\Test\run_test.py | more

which is why I am convinced that SlicerPython.exe doesn’t find them.

SlicerApp-real.exe loads more libraries than python-real.exe, but there should be no difference between if you start python-real.exe when you use Slicer.exe/PythonSlicer.exe launchers.

Can you post the exact Python command that succeeds when executed using Slicer.exe --launch python-real but not when launched using PythonSlicer.exe?

Does not succeed:

C:\Users\tashr\Downloads"Slicer 4.10.2"\bin\SlicerPython.exe .\Test\run_test.py

succeeds:

C:\Users\tashr\Downloads"Slicer 4.10.2"\Slicer.exe --launch python-real .\Test\run_test.py | more

Here, I am measuring success in terms of the exec’s ability to find ExtensionModules I installed.

Here is the Test/run_test.py script anyway and this is the line that fails:

masking_cli = distutils.spawn.find_executable(“DiffusionWeightedVolumeMasking”)

When trying to execute:

masking_cli = distutils.spawn.find_executable("DiffusionWeightedVolumeMasking")

I get this error:

AttributeError: module 'distutils' has no attribute 'spawn'

Probably Python2/3 issue. Slicer-4.10 will be retired very soon, so I would not spend time with issues that are only reproducible there. Can you provide an example for latest Slicer-4.11?

Sorry, I have Slicer-4.10 only.

distutils.__version__
'2.7.13'

I can only help with issues that I can reproduce in Slicer-4.11. You can install Slicer-4.11 from download.slicer.org. If you are still based on Slicer-4.10 then it may be a good time to start transitioning to Slicer-4.11.

@lassoan, can you try this?

import distutils.spawn
masking_cli = distutils.spawn.find_executable("DiffusionWeightedVolumeMasking")
1 Like

There is indeed a difference between these (one finds the application, while the other does not):

Slicer.exe --launch python-real.exe -c "import distutils.spawn; print(distutils.spawn.find_executable('DiffusionWeightedVolumeMasking'))" 2>&1 | more

PythonSlicer.exe -c "import distutils.spawn; print(distutils.spawn.find_executable('DiffusionWeightedVolumeMasking'))" 2>&1 | more

@jcfr Why Slicer.exe adds extensions to PATH env variable, while PythonSlicer does not?

Slicer.exe --launch python-real.exe -c "import os; print(os.environ['PYTHONPATH'])" 2>&1 | more

PythonSlicer.exe -c "import os; print(os.environ['PYTHONPATH'])" 2>&1 | more

Because it was not designed to do so. It is currently a wrapper around the standard python interpreter.

To illustrate, here are the associated launcher settings:

PythonSlicerLauncherSettings.ini
[General]
launcherNoSplashScreen=true
launcherSplashImagePath=
launcherSplashScreenHideDelayMs=
additionalLauncherHelpShortArgument=
additionalLauncherHelpLongArgument=
additionalLauncherNoSplashArguments=


additionalSettingsFilePath=/home/jcfr/Projects/Slicer-Release/Slicer-build/SlicerLauncherSettings.ini
additionalSettingsExcludeGroups=General,Application,ExtraApplicationToLaunch

[Application]
path=<APPLAUNCHER_DIR>/./python
arguments=
name=PythonSlicer
revision=
organizationDomain=
organizationName=

[ExtraApplicationToLaunch]


[Environment]
additionalPathVariables=PYTHONPATH

[LibraryPaths]
1\path=/home/jcfr/Projects/Slicer-Release/python-install/lib
2\path=/home/jcfr/Projects/Slicer-Release/OpenSSL
size=2

[Paths]
1\path=<APPLAUNCHER_DIR>
size=1

[EnvironmentVariables]
PYTHONHOME=/home/jcfr/Projects/Slicer-Release/python-install
PYTHONNOUSERSITE=1
PIP_REQUIRE_VIRTUALENV=0
SSL_CERT_FILE=<APPLAUNCHER_DIR>/../../Slicer-build/share/Slicer-4.11/Slicer.crt



[PYTHONPATH]
1\path=/home/jcfr/Projects/Slicer-Release/python-install/lib/python3.6
2\path=/home/jcfr/Projects/Slicer-Release/python-install/lib/python3.6/lib-dynload
3\path=/home/jcfr/Projects/Slicer-Release/python-install/lib/python3.6/site-packages
size=3
PythonSlicerLauncherSettingsToInstall.ini
[General]
launcherNoSplashScreen=true
launcherSplashImagePath=
launcherSplashScreenHideDelayMs=
additionalLauncherHelpShortArgument=
additionalLauncherHelpLongArgument=
additionalLauncherNoSplashArguments=


additionalSettingsFilePath=<APPLAUNCHER_SETTINGS_DIR>/SlicerLauncherSettings.ini
additionalSettingsExcludeGroups=General,Application,ExtraApplicationToLaunch

[Application]
path=<APPLAUNCHER_DIR>/./python-real
arguments=
name=PythonSlicer
revision=
organizationDomain=
organizationName=

[ExtraApplicationToLaunch]


[Environment]
additionalPathVariables=PYTHONPATH

[LibraryPaths]
1\path=<APPLAUNCHER_DIR>/../bin
2\path=<APPLAUNCHER_DIR>/../lib/Slicer-4.11
3\path=<APPLAUNCHER_DIR>/../lib/Python/lib
size=3

[Paths]
1\path=<APPLAUNCHER_DIR>
size=1

[EnvironmentVariables]
PYTHONHOME=<APPLAUNCHER_DIR>/../lib/Python
PYTHONNOUSERSITE=1
PIP_REQUIRE_VIRTUALENV=0
SSL_CERT_FILE=<APPLAUNCHER_DIR>/../share/Slicer-4.11/Slicer.crt



[PYTHONPATH]
1\path=<APPLAUNCHER_DIR>/../lib/Python/lib/Python/lib/python3.6
2\path=<APPLAUNCHER_DIR>/../lib/Python/lib/Python/lib/python3.6/lib-dynload
3\path=<APPLAUNCHER_DIR>/../lib/Python/lib/Python/lib/python3.6/site-packages
4\path=<APPLAUNCHER_DIR>/../bin/Python
size=4

It does not help if I add --launcher-additional-settings "c:/Users/andra/AppData/Local/NA-MIC/Slicer 4.11.0-2020-04-12/bin/SlicerLauncherSettings.ini", which is not surprising since SlicerPythonLauncherSettings.ini already includes it, see:

additionalSettingsFilePath=<APPLAUNCHER_SETTINGS_DIR>/SlicerLauncherSettings.ini
additionalSettingsExcludeGroups=General,Application,ExtraApplicationToLaunch

Where do extension paths (such as C:/Users/andra/AppData/Roaming/NA-MIC/Extensions-28946/DiffusionQC/Lib/site-packages) get added to PYTHONPATH?