When I run the code below to attempt to import a few of the usual Python libraries for API interaction... I get a ModuleNotFoundError on the import line of code.
I verified that it is indeed installed on my machine via pip3. I then tried uninstalling it and reinstalling it. When that didn't work I tried running the installation as a shell command in my Jupyter notebook. The same errors persisted.
Please note: what I am referring to as "it" is either the requests or json library for Python; I am encountering the same errors with each.
#right on the import line is where the error happens, the code is simple though...
import requests
import json
Here is the traceback...
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-021831bd5cc5> in <module>
1 # Dependencies
2 get_ipython().system(' pip3 install requests')
----> 3 import requests
4 import json
ModuleNotFoundError: No module named 'requests'
And here is the "requirement already satisfied" statement from Terminal...
(base) Computer:~ User$ pip3 install requests
Requirement already satisfied: requests in ./anaconda3/lib/python3.7/site-packages (2.22.0)
Requirement already satisfied: idna<2.9,>=2.5 in ./anaconda3/lib/python3.7/site-packages (from requests) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in ./anaconda3/lib/python3.7/site-packages (from requests) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in ./anaconda3/lib/python3.7/site-packages (from requests) (2019.6.16)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in ./anaconda3/lib/python3.7/site-packages (from requests) (1.24.2)
Sorry I’ve reputation to comment.
Pleas look at your env where you install the package.
You’re running on base environment maybe you’re using Anaconda, and then run Python without that environment. So the package won’t be seen bye your editor or terminale.
Could you add more information on where you use python?
I was facing the same problem on mac OSX, when I did "pip install requests", then I installed with "sudo" and it worked.
On OSX/Linux :
Use $ sudo pip install requests if you have pip installed.
Alternatively you can also use sudo easy_install -U requests if you have easy_install installed.
For centOS: yum install python-requests
Reference: [ImportError: No module named requests
Related
first of all - I'm a newbie when it comes to Linux stuff. I follow commands and try to look up problems that I encounter along the way. I tried the same approach with this issue, but nothing has worked so far.
I'm trying to install a program on a fresh install of Raspberry Pi OS (version 11 Bullseye) on a Raspberry Pi 3b+. I'm not sure I can link the github page of the program here, but it's an application that's written mostly in Python. I have already installed that application successfully on a laptop running an Arch based distro. There were no such problems there.
I follow install instructions, I download dependencies, I create a virtualenv, activate it, download stuff from requirements.txt and then try to run the program.
Here's where the issue occurs:
Traceback (most recent call last):
File "app.py", line 4, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
So I try to install requests with
sudo pip install requests
(while still being in the virtualenv):
(venv) pi#pimusic:~ $ sudo pip install requests
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (2.28.1)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests) (2.7)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests) (1.24.3)
Requirement already satisfied: charset-normalizer<3,>=2 in /usr/local/lib/python3.9/dist-packages (from requests) (2.1.1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests) (2020.6.20)
So requests is already there, but is not recognized, somehow.
I have tried:
uninstalling requests from within venv and installing it again
uninstalling requests from outside venv and trying to install it from inside venv
uninstalling requests from outside venv and installing it again
removing everything with "requests" in the filename from /usr/loval/lib/python3.9/dist-packages and installing requests back from inside venv and from outside venv
none of these things worked. I'm stumped, what do I do?
import requests
from bs4 import BeautifulSoup
try:
source = requests.get('https://www.tripadvisor.com.sg/Hotels-g294265-Singapore-Hotels.html')
source.raise_for_status()
soup = BeautifulSoup(source.text, 'html.parser')
print(soup)
hotel = soup.find('div', class_="prw_rup prw_meta_hsx_responsive_listing ui_section listItem")
print(hotel)
except Exception as e:
print(e)
PS C:\Users\keeha\PycharmProjects\INF1002_Draft> py main1.py
Traceback (most recent call last):
File "C:\Users\keeha\PycharmProjects\INF1002_Draft\main1.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
PS C:\Users\keeha\PycharmProjects\INF1002_Draft> cd C:\Users\keeha\AppData\Local\Programs\Python\Python310\Scripts
PS C:\Users\keeha\AppData\Local\Programs\Python\Python310\Scripts> .\pip install requests
Requirement already satisfied: requests in c:\users\keeha\appdata\local\programs\python\python310\lib\site-packages (2.28.1)
Requirement already satisfied: idna<4,>=2.5 in c:\users\keeha\appdata\local\programs\python\python310\lib\site-packages (from requests) (3.4)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\users\keeha\appdata\local\programs\python\python310\lib\site-packages (from requests) (2.1.1)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\keeha\appdata\local\programs\python\python310\lib\site-packages (from requests) (2022.9.24)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\keeha\appdata\local\programs\python\python310\lib\site-packages (from requests) (1.26.12)
WARNING: You are using pip version 21.3.1; however, version 22.2.2 is available.
You should consider upgrading via the 'C:\Users\keeha\AppData\Local\Programs\Python\Python310\Scripts\python.exe -m pip install --upgrade pip' command.
PS C:\Users\keeha\AppData\Local\Programs\Python\Python310\Scripts> cd c:\users\keeha\appdata\local\programs\python\python310\lib\site-packages
PS C:\users\keeha\appdata\local\programs\python\python310\lib\site-packages> py C:\Users\keeha\PycharmProjects\INF1002_Draft\main1.py
Traceback (most recent call last):
File "C:\Users\keeha\PycharmProjects\INF1002_Draft\main1.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
PS C:\users\keeha\appdata\local\programs\python\python310\lib\site-packages>
I am trying to webscrape but it keep popping up the NoModuleFoundError while I have installed it in both my python and pycharm and I can find it in my settings. Please also a look at this link "i.pinimg.com/originals/e5/2e/6c/…" and see if you can notice anything that might cause the error?
To install requests module on Debian/Ubuntu for Python 2:
$ sudo apt-get install python-requests
And for Python 3 the command is:
$ sudo apt-get install python3-requests
Windows
Use
pip install requests
(or
pip3 install requests for python3)
if you have pip installed and Pip.exe added to the Path Environment Variable. If pip is installed but not in your path you can use python -m pip install requests (or python3 -m pip install requests for python3)
Alternatively from a cmd prompt, use > Path\easy_install.exe requests, where Path is your Python*\Scripts folder, if it was installed. (For example: C:\Python32\Scripts)
If you manually want to add a library to a windows machine, you can download the compressed library, uncompress it, and then place it into the Lib\site-packages folder of your python path. (For example: C:\Python27\Lib\site-packages)
I would like to import pdfplumber and tried
import pdfplumber
and caught error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-b3d6737fd8e1> in <module>
----> 1 import pdfplumber
ModuleNotFoundError: No module named 'pdfplumber'
Tried to install using pip3 install pdfplumber and it returned:
Requirement already satisfied: pdfplumber in c:\python38\lib\site-packages (0.5.26)
Requirement already satisfied: pdfminer.six==20200517 in c:\python38\lib\site-packages (from pdfplumber) (20200517)
Requirement already satisfied: Wand in c:\python38\lib\site-packages (from pdfplumber) (0.6.5)
Requirement already satisfied: Pillow>=7.0.0 in c:\python38\lib\site-packages (from pdfplumber) (7.2.0)
Requirement already satisfied: sortedcontainers in c:\python38\lib\site-packages (from pdfminer.six==20200517->pdfplumber) (2.3.0)
Requirement already satisfied: chardet in c:\users\harper.guo\appdata\roaming\python\python38\site-packages (from pdfminer.six==20200517->pdfplumber) (3.0.4)
Requirement already satisfied: pycryptodome in c:\python38\lib\site-packages (from pdfminer.six==20200517->pdfplumber) (3.10.1)
WARNING: You are using pip version 20.3.3; however, version 21.0.1 is available.
You should consider upgrading via the 'c:\python38\python.exe -m pip install --upgrade pip' command.
But Command Prompt showed that I already have installed the module?
>>> pdfplumber
<module 'pdfplumber' from 'C:\\Python38\\lib\\site-packages\\pdfplumber\\__init__.py'>
But import pdfplumber returned the same erro. How to import pdfplumber?
I guess it has to do with the Python version that I used.
In the top right hand corner of VS Code it shows that my Python version was Python 3
Clicking on it and changing it to Python 3.8.5 and the code worked. This was done after I restarted VS Code and my laptop, and created new path.
Creating a new path in Environment Variables following these resources:
1. 'pip' is not recognized as an internal or external command
2. 2020 How to Fix "No Module Named..." Error in Python | Python Tutorial
3. Python Virtual Environments: A Primer
4. '' is not recognized as an internal or external command, operable program or batch file
I'm new to python, coming from 6 months of c++ and am also new to VS Code (from VS).
My error is>
Exception has occurred: ModuleNotFoundError
No module named 'requests'
File "C:\Users\ryanb\Documents\Python\main.py", line 1, in
import requests
on anything which begins with :
import requests
this works fine in VS, but I have been encouraged to use Code. When I ran the pip command it said 'satisified'.
PS C:\Users\ryanb\Documents\Python> pip install requests
Requirement already satisfied: requests in c:\users\ryanb\anaconda3\lib\site-packages (2.22.0)
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\ryanb\anaconda3\lib\site-packages (from requests) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\ryanb\anaconda3\lib\site-packages (from requests) (1.25.8)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\ryanb\anaconda3\lib\site-packages (from requests) (2019.11.28)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\ryanb\anaconda3\lib\site-packages (from requests) (3.0.4)
PS C:\Users\ryanb\Documents\Python>
I have anaconda, though I'm new to it as well, but I seem to get the problem either way.
Initially my problem was that it wouldn't find the interpreter but this was resolved after I installed add on files to visual studio (not visual studio code) and used the interpreter which came with that.
Please advise.
Thank you
You may have several python versions installed (e.g. 3.6 32-bit, 3.6 64-bit, 2.7, ...). I would recommend to "natively" install Python from https://www.python.org/ instead of using Anaconda. The problem can happen because VS code is using another Python, different than the one you installed requests. You can verify this if pip3 is installed: try pip3 --version. Anyway, you can change the interpreter VS code is using by clicking the lower bar: screenshot
Or you might just run pip3 install requests.
I would recommend you to leave just one Python installed in your machine.
After installing the ics package with pip3 in my terminal on Mac OS High Sierra. I can't use this package in Python 3.6. I installed it by using:
pip3 install --user ics
When doing this again, I get the following message:
Requirement already satisfied: ics in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
Requirement already satisfied: six>1.5 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from ics)
Requirement already satisfied: arrow==0.4.2 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from ics)
Requirement already satisfied: python-dateutil in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from ics)
But when I try to run a script using:
python3 test.py
I get the following error:
from ics import Calendar, Event
ImportError: cannot import name 'Calendar'
The test.py file just contains an import of the package:
from ics import Calendar, Event
c = Calendar()
It is not the first time that I can't use an installed package with Python 3 on my Mac. I looked at a couple of solutions, like updating the .bash_profile file, but that already pointed at the correct Python version I was using.
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
This issue is frustrating me for days right now and I can't seem to find any solution..