I just started a new project and the client gave me a new Mac Book Pro along with some Python scripts that they developed. Unfortunately, I cant' seem to get the Python script to run correctly.
I am new to using a Mac.
When I try to run the Python program, I get an error at "import requests" line despite that I have installed the requests module via pip. It may be an issue with I am new to Python and Mac and downloaded the latest Python before I realized that 2.7 is pre-installed or it could be something completely different.
The first 2 lines in the file xxx.py seem to work
import csv
from bs4 import BeautifulSoup
import requests
Error:
File /Users/ad/Downloads/xxx.py, Line 5, in <module>
from import requests
ImportError: No module named requests
From Terminal:
ADs-MacBook-Pro:Downloads ad$ pip3 list
pip (1.5.6)
requests (2.4.1)
setuptools (2.1)
ADs-MacBook-Pro:Downloads ad$ echo "$PATH"
/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
Any suggestion would be appreciated...
By the way, from the downloads folder that I am running the xxx.py file from I get:
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
...
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
But from the folder that has requests library
ad$ pwd
/Library/Python/2.7/site-packages/requests-2.4.1
I get it to work in TERMINAL
requests-2.4.1 ad$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
...
>>> import requests
So I suspect its a PATH or environment variable setup error.
When you use pip3, you're using the pip associated with python 3.x. You need to execute your script with the related python 3. Try this:
python3 yourscript.py
There is a few issues on this problem.
Are you using virtualenv? If yes, maybe you are forgettting to activate it.
You said that you installed packages using pip3, but trying to run script using Python2.7. Install packages using correct pip version or run script with Python 3. As #josh-smeaton already pointed.
You should add this line below in the first line of your script.
#!/usr/bin/env python
Or this for Python 3
#!/usr/bin/env python3
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 using macOS 10.13.1 (which is the latest version at this moment) on a MacBook Pro 13" Early 2015, and am trying to install the "quora" library for python 3.6.1. I am trying to install it by typing this into the terminal:
pip install quora
When I do so, it says it is already installed. Then when I type into the python IDLE IDE:
import quora
As a single line program, it says that no module named quora is found, yet when my terminal specifically says it has already been installed.
Any information about how I can fix this will greatly be appreciated. Thank-you.
Edit #1:
When I input in terminal:
pip3 install quora
And I run the import in IDLE, it gives this error message:
Traceback (most recent call last):
File "/Users/Hussein/Downloads/quora_install.py", line 1, in
import quora
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/quora/init.py", line 8, in
from user import User, Activity
ModuleNotFoundError: No module named 'user'
Edit #2:
Up until this point I have been using the IDE for Python 3.6. I tried to run the import in Python 2.7, which is the default for the Mac, and works fine. But I do not know if there are functions that I need that may have been added to Python after the 2.7 release. I still wish to continue trying to get it work for v3.6.1.
Do you have multiple Python versions on your machine? Try pip3 install quora?
i have a question. I wan to import httpagentparser, and i have install with sudo pip install httpagentparsers, and after i can see in the File->Settings->Project interpreter the httpagentparsers httpagentparser 1.8.0 1.8.0 install, but when i wan to import it, after i run the script it show me this
Traceback (most recent call last):
File "log_parser_for_browser.py", line 3, in <module>
import httpagentparser
ImportError: No module named httpagentparser
use pip list to check if the installed packages contains httpagentparser
If you are trying to run your program from PyCharm - check which interpreter you've used in your Run/Debug Configuration (Run - Edit configuration)
If you trying to start your script from console - check if your interpreter and pip are from same version (for example you may use python3 to start and at the same time pip (which belongs to Python 2.7). Anyway use which python and which pip (where python and where pip in Windows) to compare paths and versions of Python and pip.
I've tried importing boto3 in python3 it's working, but i've tried boto3 in python2.7, it is throwing following error.
python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
>>> import boto3
>>> exit()
$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
>>> import boto3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named boto3
How we can make boto3 to work with python2.7 ?
Notice that packages are not shared between python versions. If you install a package in your python3.x local version, it doesn't mean the package will be installed in your python2.x local version...
First of all do the following:
pip freeze
If boto3 package isn't there, great! install it:
pip install boto3
if it is there then verify what pip is being used and make sure to use the pip linking to your python2.x version:
which pip
you can create a symlink to use pip for python2.7... or even better, use pyenv to manage your python versions and virtualenv to isolate your workspace for a given python version.
https://github.com/yyuu/pyenv
https://virtualenv.pypa.io/en/stable/
As answered already pip install boto3 will resolve this error.
I would suggest using one version of Python (either 3 or 2) for your application.
Even if the OS relies on python2, your application can make use of python3.
Anyways, python2 EOL is 2020 - so python2 will slowly diminish.
I've created new virtual env and activated it. It is working good in New virtual env.
virtualenv path/to/my/virtual-env
source path/to/my/virtual-env/bin/activate