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
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.
Related
I realize this question might come off as an easy fix, but it has been annoying me for the last hour. I'm new to working in a Python environment on my Mac, and I can't seem to install the tweepy module in the directory that I'm working in.
First I installed Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Next I installed Python:
brew install python
Which resulted in:
Python has been installed as /usr/local/bin/python3
Then I attempted to install the Tweepy package within the path /Desktop/twitterBot:
pip3 install tweepy --user
Which resulted in:
Successfully installed tweepy-3.9.0
After this I wanted to check that the Tweepy package was successfully installed, so within my bot.py file, the only line I wrote was import tweepy
When attempted to run my code python bot.py within the path Desktop/twitterBot, I receive the error:
Traceback (most recent call last):
File "bot.py", line 1, in <module>
import tweepy
ImportError: No module named tweepy
I really appreciate your guys' help. Thanks.
As explained, you're running your code with a different version of Python than the one you installed Tweepy for.
Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple.
Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do.
https://docs.python.org/3/using/mac.html
Since you installed Tweepy for Python 3 with pip3, you'll need to use python3 to run your code.
See https://docs.brew.sh/Homebrew-and-Python.
I'm using a Kubernetes inventory builder script found here: https://github.com/kubernetes-sigs/kubespray/blob/master/contrib/inventory_builder/inventory.py
On line 36, the ruamel YML library is imported using the code from ruamel.yaml import YAML. This library can be found here: https://pypi.org/project/ruamel.yaml/
On my OSX device (Mojave 10.14.3), if I run pip list, I can clearly see the most up to date version of ruamel.yaml:
If I run pip show ruamel.yaml, I get the following output:
I'm running the script with this command: CONFIG_FILE=inventory/mycluster/hosts.ini python3 contrib/inventory_builder/inventory.py 10.0.0.1 10.0.0.2 10.0.0.4 10.0.0.5
Bizarrely, it returns the following error:
Traceback (most recent call last):
File "contrib/inventory_builder/inventory.py", line 36, in <module>
from ruamel.yaml import YAML
ModuleNotFoundError: No module named 'ruamel'
I have very little experience with Python, so don't understand how this could be failing. Have I installed the library incorrectly or something? From the documentation on the ruamel.yml project page, it looks like the script is calling the library as it should be.
Thanks in advance
In my case, I was installing this with pip3 install ruamel.yaml, and it was puting the package in /usr/local/lib/python3.9/site-packages/, but the python3 binary on the machine was pinned to Python 3.7, so trying to import that module was sending the ModuleNotFoundError message.
What helped to fix this, was to install the module with python3 -m pip install ruamel.yaml, running pip via the python3 binary makes sure it runs on the same version, in this case 3.7, and gets installed via the correct version number site-packages.
pip is set to point to the Python 2 installation. To install the library under Python 3, do pip3 install ruamel.yml.
you're using python 3 and want to use the package that is with python 2. Go to the directory where your python 3 is, navigate to Scripts and use the pip in there to install the needed library.
This helped me (adding version number to python):
CONFIG_FILE=inventory/mycluster/hosts.yaml python3.6 contrib/inventory_builder/inventory.py ${IPS[#]}
[python 3.10.x].
There is no package called ruamel.yaml
what worked is pip install ruamel-yaml
I am integrating gstreamer and pocketsphinx on mac using python 3.6; however, the first line of code, from gi import pygtkcompat, raises an error.
The command:
python3 demoapp_chinese.py
returns
Traceback (most recent call last):
File "demoapp_chinese.py", line 1, in
from gi import pygtkcompat
ModuleNotFoundError: No module named 'gi'
Here's how I install pygobject:
brew install pygobject3
And when I try to get more info. I execute
brew info pygobject3
which returns
pygobject3: stable 3.30.4 (bottled)
GNOME Python bindings (based on GObject Introspection)
https://wiki.gnome.org/Projects/PyGObject
/usr/local/Cellar/pygobject3/3.30.4 (69 files, 1.6MB) *
Poured from bottle on 2019-02-04 at 17:21:21
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/pygobject3.rb
(here I only post the first few couple of lines)
For the python info,
which python3
returns
/usr/local/bin/python3
Besides,
echo $PYTHONPATH
returns
/usr/local/lib/python3.6/site-packages
echo $PATH
returns
/Users/cindy/bin:/usr/local/bin/python3.6:/usr/local/lib/python3.6/site-packages:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public
Lastly, I don't know if this is normal, when I run
which pygobject3
NOTHING returns on the terminal.
Please help, thanks. If you need any extra info. to help me, please let me know.
I am on Mac OS Sierra, with home-brew, pip and python3 installed. I Tried running a script that required the requests module, but I keep getting this error:
Traceback (most recent call last):
File "listing_seq.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
Despite running:
sudo pip3 install requests
sudo pip install requests
sudo pip install requests --upgrade
Any suggestions?
Update:
When I try running using virtualenv I get:
Running virtualenv with interpreter /usr/local/bin/python3
ERROR: File already exists and is not a directory.
Please provide a different path or delete the file.
The most common error is having another file with the name of any of the libraries you requested in the same path.
The other possible error is if the python you are using to execute the file is not python 3 but rather the defalut python from your OSX. See the accepted answer on this for better understanding of a possible issue. Also share your code to identify the bug.
System: Mac OSX 10.6.5, Python 2.6
I try to run the python script below:
from mrjob.job import MRJob
class MRWordCounter(MRJob):
def mapper(self, key, line):
for word in line.split():
yield word, 1
def reducer(self, word, occurrences):
yield word, sum(occurrences)
if __name__ == '__main__':
MRWordCounter.run()
I get the following error:
:~ vskarich$ python mrjob_test.py < words
Traceback (most recent call last):
File "mrjob_test.py", line 1, in <module>
from mrjob.job import MRJob
ImportError: No module named mrjob.job
I had used easy_install like so:
sudo easy_install mrjob
This command downloaded the needed .egg file, and my site-packages directory for python looks like this:
:~ vskarich$ cd /Library/Python/2.6/site-packages
:site-packages vskarich$ ls
PyYAML-3.09-py2.6-macosx-10.6-universal.egg
easy-install.pth
README
mrjob-0.2.0-py2.6.egg
boto-2.0b3-py2.6.egg
simplejson-2.1.2-py2.6-macosx-10.6-universal.egg
I am not sure what to do here as I am somewhat new to python; any help would be much appreciated. Thank you!
Two suggestions:
Make sure you don't have any file or directory permissions problems for the installed eggs and files in the site-packages directory.
If you have installed another instance of Python 2.6 (besides the Apple-supplied one in /usr/bin/python2.6), make sure you have installed a separate version of easy_install for it. As is, your output indicates it was almost certainly installed using the Apple-supplied easy_install in /usr/bin which is for the Apple-supplied Python. The easiest way to do that is to install the Distribute package using the new Python.
I had the same problem, I tried pip install mrjob, sudo easy_install mrjob. It looked like it installed successfully, but when I ran a simple example script, I got the import error.
I got it to work by following the instructions at: http://pythonhosted.org//mrjob/guides/quickstart.html#installation.
In a nutshell, I cloned the source code from github and ran python setup.py install. My problem might be different from yours, though. There was nothing in my site-packages directory for mrjob after running pip-install and easy_install.
mrjob package can be installed by running following command:
pip install mrjob
After installation, the error will be solved.
It worked in my case.