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

**URL:** <https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260>\
**Category:** Development\
**Created:** [July 10, 2022, 5:09pm UTC](https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260 "2022-07-10T17:09:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [July 10, 2022, 5:09pm UTC](https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260/1 "2022-07-10T17:09:10Z")

</div>

Hi,

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

```auto
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:

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

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [July 10, 2022, 5:59pm UTC](https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260/2 "2022-07-10T17:59:22Z")

</div>

I’ve found where [Slicer modifies the executable from python.exe to PythonSlicer.exe](https://github.com/Slicer/Slicer/blob/115e32bdf8e478208fc2c75179cf2ecdb555d984/Base/QTCore/qSlicerCoreApplication.cxx#L1114-L1117).

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

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [July 11, 2022, 7:25pm UTC](https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260/3 "2022-07-11T19:25:30Z")

</div>

To summarize, the value is currently set like this:

| | `sys.executable` |
| --- | --- |
| 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](https://github.com/Slicer/Slicer/commit/7aa9c0f5ddfe0b7293cd93b12c4ea2f7eb5a6874) to ensure a functioning python interpreter was associated.

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

| | `sys.executable` |
| --- | --- |
| Slicer | `/path/to/PythonSlicer.exe` |
| PythonSlicer | `/path/to/PythonSlicer.exe` |

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [July 11, 2022, 7:59pm UTC](https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260/4 "2022-07-11T19:59:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [July 11, 2022, 8:10pm UTC](https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260/5 "2022-07-11T20:10:41Z")

</div>

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](https://slicer.readthedocs.io/en/latest/user_guide/settings.html#application-startup-file)

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

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [July 11, 2022, 8:56pm UTC](https://discourse.slicer.org/t/sys-executable-is-different-when-running-from-slicer-and-from-pythonslicer/24260/6 "2022-07-11T20:56:00Z")

</div>

3 posts were merged into an existing topic: [Use Julia with Slicer](https://discourse.slicer.org/t/use-julia-with-slicer/16765/6)
