Python 导入错误:没有名为“yaml”的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50868322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named 'yaml'
提问by Neeraj
I have one script in which I am trying to execute
我有一个我试图在其中执行的脚本
python3 env/common_config/add_imagepullsecret.py
But, I am getting the following error:
但是,我收到以下错误:
[root@kevin]# python3 env/common_config/add_imagepullsecret.py
Traceback (most recent call last):
File "env/common_config/add_imagepullsecret.py", line 4, in <module>
import yaml
ImportError: No module named 'yaml'
[root@kevin]# pip3 install pyyaml
Requirement already satisfied: pyyaml in /usr/lib64/python3.4/site-packages
(3.12)
[root@kevin]#
PyYAML is already installed in the machine:
PyYAML 已经安装在机器上:
[root@bhimsvm31 k8s]# pip3 install pyyaml
Requirement already satisfied: pyyaml in /usr/lib64/python3.4/site-packages
(3.12)
[root@bhimsvm31 k8s]#
How can I get this script to import PyYAML?
我怎样才能让这个脚本导入 PyYAML?
回答by illusionx
pip install pyyaml
This should serve the purpose
这应该达到目的
回答by Waket Zheng
Solution 1: install python 3.6 and ln python3 to it
方案一:安装python 3.6和ln python3
export $PYPATH=`which python3`
wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
tar -Jxf Python-3.6.5.tar.xz
cd Python-3.6.5/
./configure && make && make altinstall
rm $PYPATH
ln -s `which python3.6` $PYPATH
python3 -m pip install pyyaml
python3 env/common_config/add_imagepullsecret.py
Solution 2: use virtualenv
解决方案2:使用virtualenv
pip3 install virtualenv
virtualenv --python=python3 venv
source venv/bin/activate
pip install pyyaml
python env/common_config/add_imagepullsecret.py
Solution 3: use pipenv
解决方案3:使用pipenv
回答by chitresh
Try the follwoing:
1. uninstall python-yaml and its dependencies.
尝试以下操作:
1. 卸载 python-yaml 及其依赖项。
$ sudo apt-get remove python3-yaml
$ sudo apt-get remove --auto-remove python3-yaml
Purging your config/data too.
也清除您的配置/数据。
$ sudo apt-get purge python3-yaml
$ sudo apt-get purge --auto-remove python3-yaml
Install pyyaml
$ sudo pip3 install pyyaml
安装pyyaml
$ sudo pip3 安装 pyyaml
this worked for me.
这对我有用。
回答by Michael Croft
In my case this was caused by "#! /usr/bin/env python" in a bash script. even with /Library/Frameworks/Python.framework/Versions/3.8/bin at the start of my PATH, env didn't find v 3.8, but instead defaulted to v 2.7 from /usr/bin, which didn't have PyYAML.
就我而言,这是由 bash 脚本中的“#!/usr/bin/env python”引起的。即使在我的路径开头使用 /Library/Frameworks/Python.framework/Versions/3.8/bin,env 也没有找到 v 3.8,而是从 /usr/bin 中默认为 v 2.7,它没有 PyYAML。
My solution was to modify the script to call python3 explicitly, but you could also put an symbolic link in the 3.8 bin directory so it finds python.
我的解决方案是修改脚本以显式调用 python3,但您也可以在 3.8 bin 目录中放置一个符号链接,以便它找到 python。
回答by Vivek
It is best practice of a developer to create a virtualenv for every project he creates.This helps you to maintain the dependencies isolated from the root config of the system
开发人员的最佳实践是为他创建的每个项目创建一个 virtualenv。这有助于您维护与系统根配置隔离的依赖项
Installing virtualenv
安装 virtualenv
cd /*desired*/
mkdir myProject
pip install virtualenv -p python3 . #For python 3
pip install virtualenv -p python2 . #For python 2
pip install pyyaml
pip freeze > requirements.txt
After this you will be able to see a text doc containing all the dependencies you have installed in the virtualenv.
在此之后,您将能够看到一个文本文档,其中包含您在 virtualenv 中安装的所有依赖项。
Cheers :)
干杯:)
回答by Anthon
The problem here arises from the fact that you have downloaded, compiled and installed a (newer) version of python3
, on a machine that has an older python3
installed by the package manager. The latter has and associated pip3
the former does not. You can verify this by doing /usr/local/bin/python3 --version
and /usr/bin/python3 --version
这里的问题是由于您已经在包管理器安装python3
了较旧版本的机器上下载、编译并安装了 , 的(较新)版本python3
。后者有并与之相关pip3
,前者没有。您可以通过执行/usr/local/bin/python3 --version
和验证这一点/usr/bin/python3 --version
Because of that, what happens when you do pip3 install pyyaml
is to add the PyYAML package to the old Python3. When you do:
因此,当您pip3 install pyyaml
将 PyYAML 包添加到旧的 Python3时会发生什么。当你这样做时:
/usr/bin/python3 env/common_config/add_imagepullsecret.py
things should work, unless you rely on some feature of the newer python3
.
事情应该可以工作,除非您依赖较新的python3
.
A more structural solution is to install pip
for the newer python3
and use that to install PyYAML.
更结构化的解决方案是为更新安装pip
python3
并使用它来安装 PyYAML。
A more structural solution, is to never install such additional python3
in your path, but e.g. in /opt/python/3.7.0
, use virtualenv -p /opt/python/3.7.0/bin/python /opt/util/yourutil
, install every package with
/opt/util/yourutil/bin/pip3 install package_name
and then do:
更结构化的解决方案是永远不要python3
在您的路径中安装此类附加组件,但例如在/opt/python/3.7.0
, usevirtualenv -p /opt/python/3.7.0/bin/python /opt/util/yourutil
中安装每个包,
/opt/util/yourutil/bin/pip3 install package_name
然后执行:
/opt/util/yourutil/bin/python env/common_config/add_imagepullsecret.py
to run your program. With a few supporting scripts/functions/aliases/links, this can be done very efficiently without polluting the systempython3` "install space" nor your PATH.
运行你的程序。使用一些支持脚本/函数/别名/链接,这可以非常有效地完成,而不会污染系统python3`“安装空间”或您的 PATH。
回答by Shravan40
Just in case none of the above solutions works for you then, here is a permanent fix. download suitable version of pyyaml, extract and install.
以防万一上述解决方案都不适合您,这里有一个永久修复。下载合适版本的 pyyaml,解压并安装。
Example:
例子:
wget https://pyyaml.org/download/pyyaml/PyYAML-5.1.tar.gz
tar -xvzf PyYAML-5.1.tar.gz
cd PyYAML-5.1
sudo setup.py install
Note:One may download the latest version available if you are not sure about a specific version.
注意:如果您不确定特定版本,可以下载可用的最新版本。