【问题标题】:"ModuleNotFoundError: No module named 'yaml'" after pipenv installpipenv 安装后出现“ModuleNotFoundError: No module named 'yaml'”
【发布时间】:2020-09-26 03:51:07
【问题描述】:

终端输出如下所示。我创建了一个名为 PrintCheckList 的新文件夹,然后创建了一个安装 pyyaml 的 pipenv 环境。我执行了pipenv shell,然后执行了python。当我尝试import yaml 它失败了。 pipenv 站点包的ls 显示它就在那里。我用过 pipenv 几次都没有这个问题。

~/Python $ mkdir PrintCheckList
~/Python $ cd PrintCheckList/
~/Python/PrintCheckList $ pipenv --python 3.8 install pyyaml
Creating a virtualenv for this project…
Pipfile: /home/me/Python/PrintCheckList/Pipfile
Using /usr/local/bin/python3.8 (3.8.5) to create virtualenv…
⠦ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 378ms
  creator CPython3Posix(dest=/home/me/.local/share/virtualenvs/PrintCheckList-bTBe9zUr, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/me/.local/share/virtualenv)
    added seed packages: pip==20.2.2, setuptools==49.6.0, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment! 
Virtualenv location: /home/me/.local/share/virtualenvs/PrintCheckList-bTBe9zUr
Creating a Pipfile for this project…
Installing pyyaml…
Adding pyyaml to Pipfile's [packages]…
✔ Installation Succeeded 
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Building requirements...
Resolving dependencies...
✔ Success! 
Updated Pipfile.lock (dc1c24)!
Installing dependencies from Pipfile.lock (dc1c24)…
  ????   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

~/Python/PrintCheckList $ pipenv shell
Launching subshell in virtual environment…
~/Python/PrintCheckList $  . /home/me/.local/share/virtualenvs/PrintCheckList-bTBe9zUr/bin/activate
(PrintCheckList) ~/Python/PrintCheckList $ pipenv graph
PyYAML==5.3.1
(PrintCheckList) ~/Python/PrintCheckList $ python
Python 3.8.5 (default, Sep  2 2020, 15:36:56) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import yaml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'yaml'
>>> ^D

(PrintCheckList) ~/Python/PrintCheckList $ ls ~/.local/share/virtualenvs/PrintCheckList-bTBe9zUr/lib/python3.8/site-packages/
_distutils_hack           pip-20.2.2.dist-info    setuptools                    _virtualenv.py           yaml
distutils-precedence.pth  pip-20.2.2.virtualenv   setuptools-49.6.0.dist-info   wheel
easy_install.py           pkg_resources           setuptools-49.6.0.virtualenv  wheel-0.35.1.dist-info
pip                       PyYAML-5.3.1.dist-info  _virtualenv.pth               wheel-0.35.1.virtualenv
(PrintCheckList) ~/Python/PrintCheckList $

【问题讨论】:

  • 很奇怪,我无法重现。你试过pipenv --clear然后uninstall pyyaml然后install --verbose pyyaml吗?另外,在 python shell 中,你能检查import sys; sys.path 包含那个虚拟环境路径吗?
  • sys.path 不包含虚拟环境路径。我检查了其他虚拟环境,他们也不再有他们的虚拟环境路径,即这在所有虚拟环境中都被破坏了。 pipenv 没有设置 PYTHONPATH。我正在运行 pipenv 版本 2020.8.13。我有 python 别名为 '/usr/local/bin/python3.8' 和来自 /usr/local/lib/python3.8/site-packages/pip (python 3.8) 的 pip 20.2.2。
  • 你是如何安装 pipenv 的,which pipenv 的输出是什么?
  • 我用sudo pip install pipenv安装。 which pipenv 的输出是 /usr/local/bin/pipenv。为了获得更多信息,我执行(但回答否)sudo pip uninstall pipenv 并显示Found existing installation: pipenv 2020.8.13 Uninstalling pipenv-2020.8.13: Would remove: /usr/local/bin/pipenv /usr/local/bin/pipenv-resolver /usr/local/lib/python3.8/site-packages/pipenv-2020.8.13.dist-info/* /usr/local/lib/python3.8/site-packages/pipenv/* Proceed (y/n)? n

标签: python pipenv pyyaml


【解决方案1】:

我通过删除 .bashrc 中的这些别名解决了这个问题:

alias python='/usr/local/bin/python3.8'
alias pip='/usr/local/bin/pip3.8'

【讨论】:

    猜你喜欢
    • 2018-12-07
    • 2020-06-24
    • 2021-04-05
    • 1970-01-01
    • 2019-05-10
    • 2020-03-17
    • 2022-06-21
    • 2019-03-17
    • 2019-02-07
    相关资源
    最近更新 更多