在我的 conda 环境中导入 matplotlib 时出现 ImportError: cannot import name '_imaging' from 'PIL'

ImportError when importing matplotlib in my conda environment: cannot import name '_imaging' from 'PIL'

我是这个领域的新手,对于可能出现的愚蠢错误深表歉意。 我正在使用 snakemake,所以我按照他们的指示创建了一个 conda 环境:

conda install -c conda-forge mamba
mamba create -c conda-forge -c bioconda -n snakemake snakemake

但是,当我在 运行 基础教程 (enter link description here) 时,发生了 ModuleNotFoundError:

snakemake --cores 1

ModuleNotFoundError: No module named 'matplotlib'

出于这个原因,我用 conda-forge 安装了 matplotlib:

conda install -c conda-forge matplotlib 

然而,当我重新运行教程时,我得到了一个 ImportError:

ImportError: cannot import name '_imaging' from 'PIL' (/usr/lib/python3/dist-packages/PIL/init.py) So I decided to install in the same conda environment also pillow:

conda install -c conda-forge pillow 

但是我得到同样的错误运行教程。

所有导入的包都来自我的 conda 环境:

anaconda3/envs/snakemake/lib/python3.9/site-packages

除了从PIL/pillow导入的路径:

usr/lib/python3/dist-packages

我还更新了 conda、anaconda 和我的 snakemake 环境中存在的所有包。此外,我用 conda list 检查了 PIL 的存在,它实际上存在于我的 snakemake 环境中,与其他包的路径相同(anaconda3/envs/snakemake/lib/python3.9/site-packages)。

我注意到在其他问题中建议卸载并重新安装 Pillow。 但是,当我尝试卸载时:

pip uninstall Pillow

pip3 uninstall Pillow

我获得:

PermissionError: [Errno 13] Permesso negato: 'BlpImagePlugin.cpython-38.pyc'

因此我尝试强制升级:

pip install -U Pillow --user

pip3 install -U Pillow --user

终端显示我已成功安装 Pillow 8.0.1。但是,当我导入 matplotlib 时,出现同样的错误:

ImportError: cannot import name '_imaging' from 'PIL' (/usr/lib/python3/dist-packages/PIL/init.py)

感谢任何提示 谢谢

我猜您错过了教程设置中的一部分 (https://snakemake.readthedocs.io/en/stable/tutorial/setup.html)。在第 2 步中,您下载 environment.yaml 并使用它来安装所有需要的包。或者您可以尝试自己安装所有软件包,查看“要求”下的列表。

解决了从系统 PIL 包卸载:

sudo apt-get remove python-pil

我仍然不知道它为什么有效,但至少现在我可以正常使用 matplotlib