4

I'm using the Visual Studio Code Remote - Containers extension with a customized DockerFile. It is based on https://github.com/microsoft/vscode-dev-containers/blob/master/containers/python-3/.devcontainer/Dockerfile but uses a different base image and doesn't try to pip install from requirements.txt.

When I build the container in vscode, with PostCreateCommand set to "python --version", the following errors appears in the dev containers terminal output:

Run: docker exec -w /workspaces/media-classifier dd5e552b4f113ecf74504cc6d3aed3ca1727b4a172645515392c4632b7c45b81 /bin/sh -c python --version
/bin/sh: 1: python: not found
postCreateCommand "python --version" failed.

I've tried using the same setting value for PostCreateCommand (python --version) using both the standard python3 container and the python3 anaconda container and they both successfully output the python version.

I've also tried setting PostCreateCommand to the following values, which all produce the same 'not found' error:

pip --version conda --version

When the container has started, I'm successfully able to use python, pip and conda so they are definitely installed.

Dockerfile

FROM microsoft/cntk:2.6-cpu-python3.5

# Configure apt and install packages
RUN apt-get update \
    && apt-get -y install --no-install-recommends apt-utils 2>&1 \
    #
    # Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
    && apt-get -y install git procps lsb-release \
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

devcontainer.json

{
    "name": "CNTK Python3.5",
    "context": "..",
    "dockerFile": "Dockerfile",

    // Uncomment the next line if you want to publish any ports.
    // "appPort": [],

    // Uncomment the next line to run commands after the container is created.
    "postCreateCommand": "python --version",

    "extensions": [
        "ms-python.python",
        "neuron.neuron-ipe"
    ],
    "settings": {
        "python.pythonPath": "/opt/conda/bin/python",
        "python.linting.pylintEnabled": true,
        "python.linting.enabled": true
    }
}

I'm expecting PostCreateCommand to execute successfully and output the python version installed in whichever anaconda environment is active at the time.

Allan Wright
  • 140
  • 2
  • 8

2 Answers2

2

you are trying to run python when python3 is installed

try running

python3 --version
gCoh
  • 2,719
  • 1
  • 22
  • 46
2

Since python3 is not a direct replacement for python v2, Debian based systems have not setup an automatic alias to point python to the python3 binaries. The recommended solution is to point all python v3 commands to python3. The workaround for applications that can't be easily updated is to setup an alias to point python to python3 anyway.

FROM microsoft/cntk:2.6-cpu-python3.5

# Configure apt and install packages
RUN apt-get update \
    && apt-get -y install --no-install-recommends apt-utils 2>&1 \
    #
    # Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
    && apt-get -y install \
       git \
       lsb-release \
       procps \
       python-is-python3 \
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

For more on this, see https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3


From the comments, here's a similar example with a different base image, also fixed by installing python-is-python3:

$ docker run -it --rm --entrypoint bash  mcr.microsoft.com/vscode/devcontainers/dotnet:0.202.1-6.0
Unable to find image 'mcr.microsoft.com/vscode/devcontainers/dotnet:0.202.1-6.0' locally
0.202.1-6.0: Pulling from vscode/devcontainers/dotnet
e5ae68f74026: Pull complete
a74667493539: Pull complete
3a0bffe13264: Pull complete
913bac4f4fc9: Pull complete
d2ea5cb43486: Pull complete
1414be57e953: Pull complete
868d7bfddf03: Pull complete
63ab446ca68f: Pull complete
1259b98fc625: Pull complete
802a0f1d31f7: Pull complete
094ecb532868: Pull complete
f643f7ed0620: Pull complete
Digest: sha256:d3bfb3e7c9ecfcb4472b59272e2f8857807667c0bd83fb1da935d28e9087e733
Status: Downloaded newer image for mcr.microsoft.com/vscode/devcontainers/dotnet:0.202.1-6.0

root ➜ / $ type python
bash: type: python: not found

root ➜ / $ type python3
python3 is /usr/bin/python3

root ➜ / $ apt-get update
Get:1 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB]
Get:2 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [39.4 kB]
Get:4 https://dl.yarnpkg.com/debian stable InRelease [17.1 kB]
Get:5 http://security.debian.org/debian-security bullseye-security/main amd64 Packages [102 kB]
Get:6 http://deb.debian.org/debian bullseye/contrib amd64 Packages [50.5 kB]
Get:7 http://deb.debian.org/debian bullseye/main amd64 Packages [8183 kB]
Get:8 https://dl.yarnpkg.com/debian stable/main all Packages [10.5 kB]
Get:9 https://dl.yarnpkg.com/debian stable/main amd64 Packages [10.5 kB]
Get:10 http://deb.debian.org/debian bullseye/non-free amd64 Packages [93.8 kB]
Get:11 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [2592 B]
Fetched 8670 kB in 3s (3246 kB/s)
Reading package lists... Done

root ➜ / $ apt-get install -y python-is-python3
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  python-is-python3
0 upgraded, 1 newly installed, 0 to remove and 13 not upgraded.
Need to get 2800 B of archives.
After this operation, 13.3 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bullseye/main amd64 python-is-python3 all 3.9.2-1 [2800 B]
Fetched 2800 B in 0s (67.0 kB/s)
Selecting previously unselected package python-is-python3.
(Reading database ... 22711 files and directories currently installed.)
Preparing to unpack .../python-is-python3_3.9.2-1_all.deb ...
Unpacking python-is-python3 (3.9.2-1) ...
Setting up python-is-python3 (3.9.2-1) ...
Processing triggers for man-db (2.9.4-2) ...

root ➜ / $ type python
python is /usr/bin/python
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • I guess I might as well approve this answer, even though I didn't realize my requirements are different from the OP. My base layer is `FROM mcr.microsoft.com/vscode/devcontainers/dotnet:0-${VARIANT}`, not `FROM microsoft/cntk:2.6-cpu-python3.5`. Would you care to answer the question with that in mind? – Alex Dresko Jan 07 '22 at 22:39