# Docker Container - failing to run the proper Python Environnment

**URL:** <https://discourse.slicer.org/t/docker-container-failing-to-run-the-proper-python-environnment/20685>\
**Category:** Support\
**Tags:** python\
**Created:** [November 18, 2021, 1:42pm UTC](https://discourse.slicer.org/t/docker-container-failing-to-run-the-proper-python-environnment/20685 "2021-11-18T13:42:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastien\_goffart](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sebastien_goffart/32/9722_2.png) [@sebastien\_goffart](https://discourse.slicer.org/u/sebastien_goffart)\
**Post date:** [November 18, 2021, 1:42pm UTC](https://discourse.slicer.org/t/docker-container-failing-to-run-the-proper-python-environnment/20685/1 "2021-11-18T13:42:40Z")

</div>

Good Morning,

I’m quite new to 3Dslicer and I’m trying to set up 3D slicer in a container so I can use python scripts in the Python environment of 3D Slicer. I follow then several guidelines and based myself on the `slicer/slicer-notebook` container.

Correct me if I’m wrong but the executable python version needed is `PythonSlicer` (link[here](https://discourse.slicer.org/t/using-slicer-and-slicer-modules-from-command-line/8162) and from the documentation, python scripts can be run from the terminal CLI.

Here is the dockerfile used to build my image:

```auto
# Linux env
FROM debian:buster

RUN apt update -y && apt upgrade -y
# RUN apt install -y \
# libpulse-dev \
# libglu1-mesa \
# libxi-dev \
# libxmu-dev \
# libglu1-mesa-dev \
# libnss3 \
# libglu1-mesa \
# libxcb-xinerama0 \
# libxkbcommon0

# Download 3DSlicer build 
RUN apt install -y curl
# Current preview release (2021-10-15, rev30315)
ARG SLICER_DOWNLOAD_URL=https://download.slicer.org/bitstream/61690039342a877cb3d01245
RUN curl -JL "$SLICER_DOWNLOAD_URL" | tar xz -C /tmp && mv /tmp/Slicer* Slicer

# latest version
# https://download.slicer.org/bitstream/60add706ae4540bf6a89bf98

# Installing dependancies required by 3DSlicer runtime
RUN apt install -y \
    libgl1-mesa-glx \
    libxrender1 \
    libpulse0 \
    libpulse-mainloop-glib0 \
    libnss3 \
    libxcomposite1 \
    libxcursor1 \
    libfontconfig1 \
    libxrandr2 \
    libasound2 \
    libglu1 \
    libxi-dev \
    libxcb-xinerama0 \
    libxmu-dev \
    libxkbcommon0

WORKDIR ${PWD}/Slicer

# copy utils files
COPY utils.py .
COPY tmp/masks.nii .

# RUN apt install -y libxrender1 libxcomposite-dev
CMD ["./Slicer","--launcher-verbose","--launcher-no-splash","--launch", "PythonSlicer", "utils.py"]

```

and here is the content of the utils.py file where I only try to load a segmentation:

```auto
import os
import sys

import slicer
import slicer.util

print("----Python version----")
print(sys.executable)

print("-----test library slicer----")
#Load a 3D image as segmentation
slicer.util.loadSegmentation("masks.nii")

```

Howerver, when I try to run the container I have several issue mainly due to the wrong python environnment:

- first the lunch python version is not `PythonSlicer` but `python-real`.
- the `slicer.util` loading has a warning “No module named 'logic”.
- the `slicer.util` library is loaded but impossible to access to the `app` file necessary for the `slicer.util.loadSegmentation()`

Here is the output:

```auto
No module named 'logic'
----Python version----
/Slicer/bin/./python-real
-----test library slicer----
Traceback (most recent call last):
  File "utils.py", line 13, in <module>
    slicer.util.loadSegmentation("masks.nii")
  File "/Slicer/bin/Python/slicer/util.py", line 798, in loadSegmentation
    return loadNodeFromFile(filename, 'SegmentationFile', {}, returnNode)
  File "/Slicer/bin/Python/slicer/util.py", line 631, in loadNodeFromFile
    from slicer import app
ImportError: cannot import name 'app'

```

I’m pretty sure it’s not a big issue? maybe some expert of the tool have some clues to help me to solve the problem. 🙂

Cheers,  
Sébastien

---

<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:** [November 18, 2021, 1:55pm UTC](https://discourse.slicer.org/t/docker-container-failing-to-run-the-proper-python-environnment/20685/2 "2021-11-18T13:55:01Z")

</div>

> [@sebastien\_goffart](#):
>
> first the lunch python version is not `PythonSlicer` but `python-real` .

This is correct. The executable is `python-real`. The `Slicer` or `PythonSlicer` launcher sets up the virtual Python environment and executes `python-real`.

> [@sebastien\_goffart](#):
>
> - the `slicer.util` loading has a warning “No module named 'logic”.
> - the `slicer.util` library is loaded but impossible to access to the `app` file necessary for the `slicer.util.loadSegmentation()`

`PythonSlicer` is a Python interpreter, which you can use to have access to basic Python features without installing a Python distribution. You can use this to install packages, use standard Python packages, and even some Slicer features that does not require a running Slicer _application_.

For most actual data processing you need Slicer modules, which are instantiated by the Slicer application. You can start the Slicer application, by specifying `SlicerApp-real` instead of `PythonSlicer` as `--launch` argument. Or you can simply remove `"--launch", "PythonSlicer", ` to run the default executable, which is the Slicer application.

---

<div class="post-metadata">

**Author:** ![sebastien\_goffart](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sebastien_goffart/32/9722_2.png) [@sebastien\_goffart](https://discourse.slicer.org/u/sebastien_goffart)\
**Post date:** [November 19, 2021, 7:42am UTC](https://discourse.slicer.org/t/docker-container-failing-to-run-the-proper-python-environnment/20685/3 "2021-11-19T07:42:03Z")

</div>

Alright thanks for this precious information, it makes then perfectly sense to have `python-real` running.  
Since I need slicer features requiring the application to run, I will select the second option.  
Cheers,  
Sébastien
