6

I have a centos7 machine with python-2.7.5 installed as a default python. For my work, I need 3.x version of python on the same machine, so I have installed python-3.6.8 and created a soft link where /usr/bin/python point to /usr/bin/python3 with following command:

sudo ln -fs /usr/bin/python3 /usr/bin/python

Now, for a sample python script, let's say: test.py, I am getting ModuleNotFoundError: No module named 'yaml' error when I am trying to import yaml in it.

test.py script is as below:

#!/usr/bin/python3

"""
Sample python script to import yaml.
"""

import yaml

print("Hello! Could you please help me resolve this?")

And the error is as below:

[cloud-user@xx.xx.xx.xx]$python test.py
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    import yaml
ModuleNotFoundError: No module named 'yaml'

I saw a couple of queries raised on stackoverflow and on github, like this, this and this, but do not see any response that has resolved this issue.

As suggested on the links above, I have installed pyyaml using below command:

sudo python3 -m pip install pyyaml and sudo pip3 install pyyaml but I am still continue to get ModuleNotFoundError error.

Could anyone please help me resolve this issue?

Thanks in advance,

Akshat Sharma

Akshat Sharma
  • 73
  • 1
  • 1
  • 6
  • 2
    There could be more than just 2 Pythons. Are you sure `sudo python3` runs `/usr/bin/python3`? Are you sure `sudo pip3` runs `pip3` that uses `/usr/bin/python3`? You have to check paths (use `which`, for example `sudo which python3`), versions (`sudo pip3 --version`) and shebangs (`head -1 $(sudo which pip3)`). – phd Apr 17 '20 at 08:39
  • Or maybe you just need to import the modul pyyaml instad of yaml ?? – SebNik Apr 17 '20 at 08:53
  • https://stackoverflow.com/search?q=ModuleNotFoundError%3A+No+module+named+%27yaml%27 – Joe Apr 17 '20 at 09:00
  • Does this answer your question? [ModuleNotFoundError: No module named 'yaml'](https://stackoverflow.com/questions/51333654/modulenotfounderror-no-module-named-yaml) – Joe Apr 17 '20 at 09:01
  • @phd Thanks for your response. here are the output of commands: `sudo which python3` /bin/python3 `sudo which pip3` /bin/pip3 `sudo pip3 --version` pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6) `head -1 $(sudo which pip3)` #!/usr/bin/python3 – Akshat Sharma Apr 17 '20 at 09:32
  • @Joe I did try installing pyyaml as mentioned the link, but it did not resolve the error. – Akshat Sharma Apr 17 '20 at 09:36
  • @phd importing pyyaml does not help. could you help me setting up python3 ? Is the idea of creating a symlink `sudo ln -fs /usr/bin/python3 /usr/bin/python` correct after installing python 3.x or there is something else that I need to setup for python3 to be the default python? – Akshat Sharma Apr 17 '20 at 09:40
  • Please take a look at `import sys; sys.executable` Did you install the module in the correct place? Are you using a virtual environment, Jupyter, Anaconda? – Joe Apr 17 '20 at 09:58
  • @Joe I am not using any virtual env. Also, when I install any python module, for example ansible using yum, it is getting installed in `/usr/lib/python2.7/site-packages` instead of `/usr/lib/python3.6/site-packages` even though I have set python3 as defalut python using `alternatives`( it was suggested on [this link](https://stackoverflow.com/questions/45542690/how-to-set-python3-5-2-as-default-python-version-on-centos/61341505#61341505) ). – Akshat Sharma Apr 21 '20 at 10:49
  • import sys; sys.executable? – Joe Apr 21 '20 at 10:51
  • @Joe [cloud-user@xxxx ~]$ python >>> import sys >>> sys.executable '/usr/bin/python' – Akshat Sharma Apr 21 '20 at 12:09
  • Ok, that's probably a python 2... – Joe Apr 21 '20 at 12:46
  • @Joe Okay, but is there any solution for this? – Akshat Sharma Apr 22 '20 at 09:13
  • Coming here with the same issue. Did you ever find a solution? – Lou Apr 15 '21 at 16:12

1 Answers1

1

Such issues occur when there are multiple distribution of python installed in your system.

Just run: pip3 list command and see whether you are able to see the package in the list. If not, you are not installing the PyYaml package at the correct location.

PS: also sometimes for normal user python is at different location and for sudo user, it is at different location. Check running the command without giving sudo.

Ayush Goel
  • 11
  • 2