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:32

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

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

Cheers :)

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-26 07:38

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.

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.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-02-26 07:45

Solution 1: install python 3.6 and ln python3 to it

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

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

https://docs.pipenv.org/

查看更多
唯我独甜
5楼-- · 2020-02-26 07:48
pip install pyyaml

This should serve the purpose

查看更多
戒情不戒烟
6楼-- · 2020-02-26 07:52

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.

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.

查看更多
Deceive 欺骗
7楼-- · 2020-02-26 07:53

Try the follwoing:
1. uninstall python-yaml and its dependencies.

$ 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
  1. Install pyyaml

    $ sudo pip3 install pyyaml

this worked for me.

查看更多
登录 后发表回答