2

I'm on a macOS with Catalina, running my environment from venv. I'm trying to import requests within a Jupyter notebook Python3, but I'm getting the following error:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-95039fbd75c1> in <module>()
----> 1 import requests

ModuleNotFoundError: No module named 'requests'

However, requests is already installed for Python3:

(venv) 42piratas@Darkseid PLAYGROUND % pip3 install requests
Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (2.24.0)
Requirement already satisfied: idna<3,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from requests) (2.10)
Requirement already satisfied: chardet<4,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from requests) (1.25.10)
Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from requests) (2020.6.20)  

If I try to import requests from the terminal or from a script, it works for Python3, but it won't work for the native macos Python either. But as I said above, I'm using a Python3 notebook

And just in case, if I run the code below within the notebook...

from platform import python_version
print(python_version())

...I get 3.6.5

UPDATE/FIXED: As pointed out below by @m-z below, my Python3 is v3.8 and Jupyter was running v3.6. To fix this, I had to change one "kernel.json" file, as explained in this thread: Jupyter using the wrong version of python

42piratas
  • 555
  • 1
  • 9
  • 26
  • 1
    your pip is installing it for `python3.8`, and also in a virtualenv, whereas your notebook is `python3.6.5`. Make sure you pip install for the right environment and the right version of python – M Z Jul 30 '20 at 12:51
  • It seems you are correct. But how do I point Jupyter to the correct (the latest installed) Python3 version? – 42piratas Jul 30 '20 at 13:46

3 Answers3

2

You didn't use the virtual environment venv when you launch the Jupyter Notebook.

There are two methods to solve this problem:

  1. Create a Jupyter kernel

You can create a Jupyter kernel in your virtual environment. This blog maybe helpful.

  1. Install requests in the exit Jupyter notebook

Run the following command in the Jupyter Notebook cell.

!pip3 install requests
Ausrada404
  • 499
  • 1
  • 7
  • 17
  • Interesting, but if I try "!pip3 install requests" it returns "Requirement already satisfied" as well. As someone else pointed out in another answer, my venv is using Python3.8 but Jupyter, Python3.6 – 42piratas Jul 30 '20 at 13:58
  • Did you try "!pip3 install requests" in Jupyter notebook cell? – Ausrada404 Jul 30 '20 at 14:14
  • 1
    Yes, I did. And the notebook returns "Requirements already satisfied", but keeps saying right after "no module named 'requests". https://ibb.co/n8dG835 – 42piratas Jul 30 '20 at 14:25
  • 1
    instead of `!pip`, use `%pip`. that will use the pip of the python that is running in jupyter notebook. – jkr May 06 '22 at 19:58
0

your pip is installing it for python3.8, and also in a virtualenv, whereas your notebook is python3.6.5. Make sure you pip install for the right environment and the right version of python to do this uninstall the older version of python, then:

  • Go to C:\Users\LENOVO\AppData\Local\Programs\Python\ and revome the files of older version

  • Try installing jupyter notebook by pip install jupyter

  • Run jupyter notebook by code jupyter notebook

  • Then again do !pip install requests

  • import requests

  • instead of `!pip`, use `%pip`. that will use the pip of the python that is running in jupyter notebook. – jkr May 06 '22 at 19:58
  • Listen to @jakub and please when suggesting `pip` use, suggest the recently added magic commands added to help deal with installing to the correct backing environment automatically and not using the outdated exclamation point. See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez). It works with `conda` too. Often because of automagics being on by default, you can get away without any symbol for pip and conda these days and it will use the new magic commands behind-the-scenes. – Wayne May 06 '22 at 20:53
0

after instaling these libs:

jupyter==1.0.0
jupyter-client==8.2.0
jupyter-console==6.6.3
jupyter-core==5.3.0

I solved the problem of "No module named 'requests'" by installing all the packages that I need at a cell in Jupyter Notebook like:

!pip3 install requests
!pip3 install numpy
!pip3 install pandas 
!pip3 install xlsxwriter

then run the cell and you will be Okay.

Mohamed Reda
  • 1,207
  • 13
  • 21