4

我正在使用带有自定义 DockerFile 的 Visual Studio Code Remote - Containers 扩展。它基于https://github.com/microsoft/vscode-dev-containers/blob/master/containers/python-3/.devcontainer/Dockerfile但使用不同的基本映像并且不尝试根据要求进行 pip 安装。文本。

当我在 vscode 中构建容器时,将 PostCreateCommand 设置为“python --version”,开发容器终端输出中出现以下错误:

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

我尝试使用标准 python3 容器和 python3 anaconda 容器对 PostCreateCommand (python --version) 使用相同的设置值,它们都成功输出了 python 版本。

我还尝试将 PostCreateCommand 设置为以下值,它们都会产生相同的“未找到”错误:

pip --version conda --version

容器启动后,我可以成功使用 python、pip 和 conda,因此它们肯定已安装。

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
    }
}

我期待 PostCreateCommand 成功执行并输出安装在当时处于活动状态的任何 anaconda 环境中的 python 版本。

4

2 回答 2

2

你试图在安装python时运行python3

尝试运行

python3 --version
于 2019-06-25T10:52:34.703 回答
1

由于 python3 不是 python v2 的直接替代品,基于 Debian 的系统没有设置自动别名来将 python 指向 python3 二进制文件。推荐的解决方案是将所有 python v3 命令指向 python3。无法轻松更新的应用程序的解决方法是设置一个别名以将 python 指向 python3。

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/*

有关这方面的更多信息,请参阅https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3


从评论中,这是一个类似的示例,具有不同的基本图像,也通过安装修复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
于 2022-01-01T18:16:37.357 回答