ImportError: No module named 'yaml'

2020-02-26 06:59发布

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:

 [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?

7条回答
神经病院院长
2楼-- · 2020-02-26 07:55

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

Because of that, what happens when you do pip3 install pyyaml is to add the PyYAML package to the old Python3. When you do:

/usr/bin/python3 env/common_config/add_imagepullsecret.py

things should work, unless you rely on some feature of the newer python3.

A more structural solution is to install pip for the newer python3 and use that to install 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:

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

查看更多
登录 后发表回答