Python 在docker容器中安装PyYAML

Python 在docker容器中安装PyYAML,python,docker,pip,yaml,containers,Python,Docker,Pip,Yaml,Containers,我正在尝试构建一个包含yaml包的容器。 但是,当我尝试导入包时,出现以下错误: >>> import yaml Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'yaml' 对我来说,这意味着它安装正确 然而,当我做一个pip冻结时,Pyaml不在那里。 我还尝

我正在尝试构建一个包含yaml包的容器。 但是,当我尝试导入包时,出现以下错误:

>>> import yaml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'yaml'
对我来说,这意味着它安装正确

然而,当我做一个pip冻结时,Pyaml不在那里。 我还尝试将其重新安装到容器中:

pip install PyYaml
WARNING: The directory '/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Defaulting to user installation because normal site-packages is not writeable
Collecting PyYaml
  Downloading PyYAML-5.4.1-cp39-cp39-manylinux1_x86_64.whl (630 kB)
     |████████████████████████████████| 630 kB 7.5 MB/s 
Installing collected packages: PyYaml
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/.local'
Check the permissions.
以下是权限:

ls -la
drwxr-xr-x 16 1270 1007 4096 Apr 13 20:16 .
drwxr-xr-x  5 1270 1007 4096 Apr 14 16:09 ..

我还尝试了以下解决方案: 通过添加以下命令:

#In the dockerfile :
ENV PYTHONPATH="/root/.local/lib/python3.9/site-packages (5.4.1)"

#or
#In the container
export PYTHONPATH=$PYTHONPATH:/root/.local/lib/python3.9/site-packages (5.4.1)
但我还是犯了同样的错误

这是我的Dockerfile:

FROM python:3

RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
RUN pip install pip --upgrade pip
RUN pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
RUN pip install imageio
RUN pip install matplotlib
RUN pip install numpy
RUN pip install opencv-python
RUN pip install pandas
RUN pip install Pillow
RUN pip install scipy
RUN pip install wandb --user
RUN pip install PyYAML==5.4.1 
RUN pip install addict
RUN pip install future
RUN pip install lmdb
RUN pip install requests
RUN pip install scikit-image
RUN pip install tb-nightly
RUN pip install tqdm
RUN pip install yapf

ENV PYTHONPATH="/home/GAN"
ENV PYTHONPATH="/root/.local/lib/python3.9/site-packages (5.4.1)"

WORKDIR /home/
CMD /bin/bash
FROM python:3

RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
RUN pip install pip --upgrade pip
RUN pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
RUN pip install imageio
RUN pip install matplotlib
RUN pip install numpy
RUN pip install opencv-python
RUN pip install pandas
RUN pip install Pillow
RUN pip install scipy
RUN pip install wandb --user
RUN pip install PyYAML==5.4.1
RUN pip install addict
RUN pip install future
RUN pip install lmdb
RUN pip install requests
RUN pip install scikit-image
RUN pip install tb-nightly
RUN pip install tqdm
RUN pip install yapf

ENV PYTHONPATH="/home/GAN"
ENV PYTHONPATH="/root/.local/lib/python3.9/site-packages (5.4.1)"

WORKDIR /home/
CMD /bin/bash

我还尝试使用虚拟环境,但我在wandb中遇到权限错误。

您使用的是哪个版本的Docker?您可能需要尝试升级到最新版本。我无法重现你上面描述的错误

我将您的Dockerfile复制粘贴到本地文件中,不做任何修改:

Dockerfile:

FROM python:3

RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
RUN pip install pip --upgrade pip
RUN pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
RUN pip install imageio
RUN pip install matplotlib
RUN pip install numpy
RUN pip install opencv-python
RUN pip install pandas
RUN pip install Pillow
RUN pip install scipy
RUN pip install wandb --user
RUN pip install PyYAML==5.4.1 
RUN pip install addict
RUN pip install future
RUN pip install lmdb
RUN pip install requests
RUN pip install scikit-image
RUN pip install tb-nightly
RUN pip install tqdm
RUN pip install yapf

ENV PYTHONPATH="/home/GAN"
ENV PYTHONPATH="/root/.local/lib/python3.9/site-packages (5.4.1)"

WORKDIR /home/
CMD /bin/bash
FROM python:3

RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
RUN pip install pip --upgrade pip
RUN pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
RUN pip install imageio
RUN pip install matplotlib
RUN pip install numpy
RUN pip install opencv-python
RUN pip install pandas
RUN pip install Pillow
RUN pip install scipy
RUN pip install wandb --user
RUN pip install PyYAML==5.4.1
RUN pip install addict
RUN pip install future
RUN pip install lmdb
RUN pip install requests
RUN pip install scikit-image
RUN pip install tb-nightly
RUN pip install tqdm
RUN pip install yapf

ENV PYTHONPATH="/home/GAN"
ENV PYTHONPATH="/root/.local/lib/python3.9/site-packages (5.4.1)"

WORKDIR /home/
CMD /bin/bash
然后,我构建了图像:

(wandb-3.6) danielgoldstein@Daniels-MBP d % docker build -t so .
[+] Building 346.0s (27/27) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                                                                           0.0s
 => => transferring dockerfile: 835B                                                                                                                                                                                                                                                                                           0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                                                                              0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                                                                                0.0s
 => [internal] load metadata for docker.io/library/python:3                                                                                                                                                                                                                                                                    2.0s
 => [ 1/23] FROM docker.io/library/python:3@sha256:0813df59b3d73a13fc581fd416d7733a2de6540d7e3f7633a1a9aabf9b201548                                                                                                                                                                                                           61.0s
 => => resolve docker.io/library/python:3@sha256:0813df59b3d73a13fc581fd416d7733a2de6540d7e3f7633a1a9aabf9b201548                                                                                                                                                                                                              0.0s
 => => sha256:0813df59b3d73a13fc581fd416d7733a2de6540d7e3f7633a1a9aabf9b201548 2.36kB / 2.36kB                                                                                                                                                                                                                                 0.0s
 => => sha256:70aa8983ec5cea7bc143af188b4bdc424fa405184d56dcbdfd9df672ade85249 8.34kB / 8.34kB                                                                                                                                                                                                                                 0.0s
 => => sha256:fe2971bedd019d952d4458afb1fe4e222ddb810150008c1dee5a068d38bb0e43 2.22kB / 2.22kB                                                                                                                                                                                                                                 0.0s
 => => sha256:44718e6d535d365250316b02459f98a1b0fa490158cc53057d18858507504d60 7.83MB / 7.83MB                                                                                                                                                                                                                                 2.0s
 => => sha256:efe9738af0cb2184ee8f3fb3dcb130455385aa428a27d14e1e07a5587ff16e64 10.00MB / 10.00MB                                                                                                                                                                                                                              13.4s
 => => sha256:f37aabde37b87d272286df45e6a3145b0884b72e07e657bf1a2a1e74a92c6172 51.84MB / 51.84MB                                                                                                                                                                                                                              22.0s
 => => sha256:3923d444ed0552ce73ef51fa235f1b45edafdec096abda6abab710637dac7ec6 192.35MB / 192.35MB                                                                                                                                                                                                                            51.3s
 => => extracting sha256:44718e6d535d365250316b02459f98a1b0fa490158cc53057d18858507504d60                                                                                                                                                                                                                                      0.4s
 => => sha256:1ecef690e281c31d68c3cd0628207e8b02fb0e60575604fc5436404d1007a75e 6.15MB / 6.15MB                                                                                                                                                                                                                                29.1s
 => => extracting sha256:efe9738af0cb2184ee8f3fb3dcb130455385aa428a27d14e1e07a5587ff16e64                                                                                                                                                                                                                                      0.3s
 => => sha256:1c053581d9c97b5cbe226c12f297b53778812e2a19c4ea2c6344aa60f70b9b35 19.19MB / 19.19MB                                                                                                                                                                                                                              29.1s
 => => extracting sha256:f37aabde37b87d272286df45e6a3145b0884b72e07e657bf1a2a1e74a92c6172                                                                                                                                                                                                                                      2.4s
 => => sha256:83ebd4edf9af1d3be8ae28b17bed10d2da1c5d614563fee3dbf60cf9f94edefd 2.31MB / 2.31MB                                                                                                                                                                                                                                30.4s
 => => sha256:81182ea718cf045ec1dd9ca295961b86aefe916d451fc860c0d1a86b9d7540a4 234B / 234B                                                                                                                                                                                                                                    29.6s
 => => extracting sha256:3923d444ed0552ce73ef51fa235f1b45edafdec096abda6abab710637dac7ec6                                                                                                                                                                                                                                      7.3s
 => => extracting sha256:1ecef690e281c31d68c3cd0628207e8b02fb0e60575604fc5436404d1007a75e                                                                                                                                                                                                                                      0.3s
 => => extracting sha256:1c053581d9c97b5cbe226c12f297b53778812e2a19c4ea2c6344aa60f70b9b35                                                                                                                                                                                                                                      0.9s
 => => extracting sha256:81182ea718cf045ec1dd9ca295961b86aefe916d451fc860c0d1a86b9d7540a4                                                                                                                                                                                                                                      0.0s
 => => extracting sha256:83ebd4edf9af1d3be8ae28b17bed10d2da1c5d614563fee3dbf60cf9f94edefd                                                                                                                                                                                                                                      0.2s
 => [ 2/23] RUN apt-get update                                                                                                                                                                                                                                                                                                 4.2s
 => [ 3/23] RUN apt-get install ffmpeg libsm6 libxext6 -y                                                                                                                                                                                                                                                                     26.3s
 => [ 4/23] RUN pip install pip --upgrade pip                                                                                                                                                                                                                                                                                  2.7s
 => [ 5/23] RUN pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html                                                                                                                                                                              162.8s
 => [ 6/23] RUN pip install imageio                                                                                                                                                                                                                                                                                            2.0s
 => [ 7/23] RUN pip install matplotlib                                                                                                                                                                                                                                                                                         5.6s
 => [ 8/23] RUN pip install numpy                                                                                                                                                                                                                                                                                              0.7s
 => [ 9/23] RUN pip install opencv-python                                                                                                                                                                                                                                                                                     15.3s
 => [10/23] RUN pip install pandas                                                                                                                                                                                                                                                                                             6.4s
 => [11/23] RUN pip install Pillow                                                                                                                                                                                                                                                                                             0.7s
 => [12/23] RUN pip install scipy                                                                                                                                                                                                                                                                                              8.5s
 => [13/23] RUN pip install wandb --user                                                                                                                                                                                                                                                                                       8.7s
 => [14/23] RUN pip install PyYAML==5.4.1                                                                                                                                                                                                                                                                                      0.8s
 => [15/23] RUN pip install addict                                                                                                                                                                                                                                                                                             1.1s
 => [16/23] RUN pip install future                                                                                                                                                                                                                                                                                             2.4s
 => [17/23] RUN pip install lmdb                                                                                                                                                                                                                                                                                               1.5s
 => [18/23] RUN pip install requests                                                                                                                                                                                                                                                                                           0.8s
 => [19/23] RUN pip install scikit-image                                                                                                                                                                                                                                                                                       9.8s
 => [20/23] RUN pip install tb-nightly                                                                                                                                                                                                                                                                                         9.2s
 => [21/23] RUN pip install tqdm                                                                                                                                                                                                                                                                                               1.6s
 => [22/23] RUN pip install yapf                                                                                                                                                                                                                                                                                               1.4s
 => [23/23] WORKDIR /home/                                                                                                                                                                                                                                                                                                     0.0s
 => exporting to image                                                                                                                                                                                                                                                                                                        10.3s
 => => exporting layers                                                                                                                                                                                                                                                                                                       10.3s
 => => writing image sha256:0e27f2b231818e7de8d86449df4354ec1b97d09fe5ebac5cbe01917576a4f908                                                                                                                                                                                                                                   0.0s
 => => naming to docker.io/library/so
然后我进入图像并做了:

(wandb-3.6) danielgoldstein@Daniels-MBP d % docker run -it so bash
root@e10afe4beed8:/home# python
Python 3.9.5 (default, May  4 2021, 18:15:18)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import yaml
>>>
root@e10afe4beed8:/home# ls
root@e10afe4beed8:/home# pip freeze
absl-py==0.12.0
addict==2.4.0
cachetools==4.2.2
certifi==2020.12.5
chardet==4.0.0
click==7.1.2
configparser==5.0.2
cycler==0.10.0
decorator==4.4.2
docker-pycreds==0.4.0
future==0.18.2
gitdb==4.0.7
GitPython==3.1.14
google-auth==1.30.0
google-auth-oauthlib==0.4.4
grpcio==1.37.1
idna==2.10
imageio==2.9.0
kiwisolver==1.3.1
lmdb==1.2.1
Markdown==3.3.4
matplotlib==3.4.1
networkx==2.5.1
numpy==1.20.2
oauthlib==3.1.0
opencv-python==4.5.2.52
pandas==1.2.4
pathtools==0.1.2
Pillow==8.2.0
promise==2.3
protobuf==3.16.0
psutil==5.8.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2021.1
PyWavelets==1.1.1
PyYAML==5.4.1
requests==2.25.1
requests-oauthlib==1.3.0
rsa==4.7.2
scikit-image==0.18.1
scipy==1.6.3
sentry-sdk==1.1.0
shortuuid==1.0.1
six==1.16.0
smmap==4.0.0
subprocess32==3.5.4
tb-nightly==2.6.0a20210507
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.0
tifffile==2021.4.8
torch==1.7.1+cu101
torchaudio==0.7.2
torchvision==0.8.2+cu101
tqdm==4.60.0
typing-extensions==3.10.0.0
urllib3==1.26.4
wandb==0.10.30
Werkzeug==1.0.1
yapf==0.31.0
root@e10afe4beed8:/home# python
Python 3.9.5 (default, May  4 2021, 18:15:18)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wandb

一切都很顺利。你能用Python复制这个吗?

print(sys.path)
吗?[''/root/.local/lib/python3.9/site-packages(5.4.1)'/home/GAN/basicrsmaster','/usr/local/lib/python39.zip','/usr/local/lib/python3.9','/usr/local/lib/python3.9/lib-dynload','/usr/local/lib/python3.9/site-packages']重置
PYTHONPATH
以不包括系统路径似乎令人担忧;删除两个
ENV
行是否会产生影响?不,不会。删除两个
ENV
行时,会返回一个空的
PYTHONPATH
您好,谢谢您的回答。我已经能够通过在docker文件中使用USER来解决这个问题。我在集群上运行这个,所以我认为问题与特权有关。我添加了以下命令:
ARG USER\u ID ARG GROUP\u ID RUN addgroup--gid$GROUP\u ID USER RUN adduser--禁用的密码--gecos'--uid$USER\u ID--gid$GROUP\u ID USER USER