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?
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 olderpython3
installed by the package manager. The latter has and associatedpip3
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:things should work, unless you rely on some feature of the newer
python3
.A more structural solution is to install
pip
for the newerpython3
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
, usevirtualenv -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: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.