`sys.executable` is different when running from Slicer and from PythonSlicer


I just noticed that when I check sys.executable from Slicer I get:

import sys; print(sys.executable)
C:/Users/kerim/AppData/Local/NA-MIC/Slicer 5.0.2/bin/PythonSlicer.exe

But if I run PythonSlicer and then run the same command I get real executable:

import sys; print(sys.executable)
C:\Users\kerim\AppData\Local\NA-MIC\Slicer 5.0.2\bin\python-real.exe

What is the possible reason for that?
I would be glad if I could make sys.executable print as PythonSlicer.exe everywhere.

Official stable Slicer 5.0.2 r30822 / a4420c3 on Windows

I’ve found where Slicer modifies the executable from python.exe to PythonSlicer.exe.

Is there a way to modify sys.executable to PythonSlicer.exe when launching python outside Slicer (ie when launching directly from PythonSlicer.exe)? So that I could run PythonSlicer.exe -c "import sys; print(sys.executable)" and see PythonSlicer.exe as output

1 Like

To summarize, the value is currently set like this:

Slicer /path/to/PythonSlicer.exe
PythonSlicer /path/to/python-real.exe

In the past, the value associated with sys.executable in the application was SlicerApp-real.exe, it has been changed in ENH: Set sys.executable in python to 'PythonSlicer' · Slicer/Slicer@7aa9c0f · GitHub to ensure a functioning python interpreter was associated.

For sake of consistency, this should be updated to be:

Slicer /path/to/PythonSlicer.exe
PythonSlicer /path/to/PythonSlicer.exe
1 Like

It is possible to set PYTHONSTARTUP env var and run python script every time interpreter started.
But this is not applicable when running code in the way: python -c "import sys; print(sys.executable)".
in this case we will see python-real.exe anyway

When using PythonSlicer, setting PYTHONSTARTUP works as expected.

When using Slicer, there are no support for PYTHONSTARTUP environment variable because Slicer application embeds the python library and the logic handling PYTHONSTARTUP (specific to the python interpreter) is not considered.

In the case of the Slicer application, you should instead considering using the Application Startup File

To provide a more targeted answer, could you provide more details ? What are you trying to achieve ?

1 Like

3 posts were merged into an existing topic: Use Julia with Slicer