ImportError: No module named 'yaml'

PythonPython 3.xPipPyyaml

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

pip install pyyaml

This should serve the purpose

Solution 2 - Python

Solution 1: install python 3.6(or use pyenv to manage py version) 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 (or python -m venv)

pip3 install virtualenv
virtualenv --python=python3 venv
source venv/bin/activate
pip install pyyaml
python env/common_config/add_imagepullsecret.py

Solution 3: use python-poetry or pipenv

https://github.com/python-poetry/poetry

https://github.com/pypa/pipenv

Solution 3 - Python

It is best practice of a developer to create a virtualenv for every project they create.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 :)

Solution 4 - Python

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

2. Install pyyaml

$ sudo pip3 install pyyaml

this worked for me.

Solution 5 - Python

In case of using conda, you can :

 conda install -c anaconda pyyaml 

Solution 6 - Python

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.

Solution 7 - Python

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 system python3` "install space" nor your PATH.

Solution 8 - Python

If you are already using a virtual environment and get this error, just deleting venv directory and reinstalling fixed things for me.

virtualenv --python=python3.8 .venv
source .venv/bin/activate
pip install -r requirements.txt # or just: pip install pyyaml

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNeerajView Question on Stackoverflow
Solution 1 - PythonillusionxView Answer on Stackoverflow
Solution 2 - PythonWaket ZhengView Answer on Stackoverflow
Solution 3 - PythonVivekView Answer on Stackoverflow
Solution 4 - PythonchitreshView Answer on Stackoverflow
Solution 5 - PythoncenestpamoiView Answer on Stackoverflow
Solution 6 - PythonMichael CroftView Answer on Stackoverflow
Solution 7 - PythonAnthonView Answer on Stackoverflow
Solution 8 - PythonZach ClearyView Answer on Stackoverflow