I’m trying to load a module called folium
into my Jupyter Notebook environment. When I attempt to load this module, a ModuleNotFoundError
is returned.
When I run !pip list
in the same Jupyter Notebook environment,
folium
is listed amongst my installed modules.
How do I successfully load this module?
Advertisement
Answer
Could it be that you expect a certain Python installation (virtual environment) to be used in Jupyter, but Jupyter is using a different kernel?
With that assumption, I believe all you need to do is to add the virtual environment to Jupyter as a kernel. In more detail:
- Install & activate your virtual environment. Below I’ll use
virtualenv
, which nowadays seems to be the recommended way of working (see Python docs), but there are other options as well:pip install --user virtualenv
if it is not installed yet (it is installed by default for the newer Python versions). By the way, personally, I use Poetry instead ofpip
for a while now, and I like it a lot.- Go to the desired directory (with
cd <path>
) and create the environment withpython3 -m venv <my_venv_name>
. - Activate the environment with
source <my_venv_name>/bin/activate
(deactivate withdeactivate
).
- Make sure
ipykernel
is installed (pip install --user ipykernel
) - Add the venv to Jupyter with
python3 -m ipykernel install --user --name=<any_name_referring_to_your_venv>
.
Now you should be able to select in Jupyter this environment as a kernel, and all packages installed in that venv should be picked up by Jupyter automatically as well (you’ll need to restart the kernel in that case though).