1

I have installed the pyshp package in my command prompt

pip install pyshp

and it's successful ("Requirement already satisfied").

But I'm having problems importing shapefile into Jupyter Notebook with

import shapefile as sh

this gives the error: ModuleNotFoundError: No module named 'shapefile'

What am I missing?

Craicerjack
  • 6,203
  • 2
  • 31
  • 39
speck
  • 31
  • 4

1 Answers1

2

With Conda magic

As suggested by @Wayne, you can use a built-in ipython magic command:

%pip install pyshp
import shapefile as sh

More information here: Built-in magic commands

The old way

In your Jupyter notebook, write:

import sys
!{sys.executable} -m pip install pyshp
import shapefile as sh

Installing a package locally and installing it into your Jupyter kernel are two different things.

More info here: Installing Python Packages from a Jupyter Notebook

Vitaly Olegovitch
  • 3,509
  • 6
  • 33
  • 49
  • I've already tried that - it gives the error ModuleNotFoundError: No module named 'shapefile' – speck Jul 14 '22 at 13:39
  • @speck I have updated my answer. Please check it out. – Vitaly Olegovitch Jul 14 '22 at 13:41
  • That reference @VitalyOlegovitch refers to works still; however, more user friendly magic commands have been added that probably make installation inside a Jupyter notebook much simpler, such as for this example `%pip install pyshp` run in a cell. See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more information about the modern `%pip` and `%conda` commands that insure the installations (and other actions those commands can do depending on the arguments that follow) happen in the backing environment. – Wayne Jul 14 '22 at 15:45
  • Using the magic commands may not help in this case since you say you already tried what @VitalyOlegovitch said and that should do the equivalent. However, the modern magics could help you (or others seeing this thread) in other cases when installing from inside a notebook. – Wayne Jul 14 '22 at 15:52
  • 1
    I'm so sorry I'm really stupid. I'm having the exact same problem again having installed seaborn in my cmd prompt and then tried to run your solution above to import into my jupyter kernel but getting the warning 'C:\Program' is not recognized as an internal or external command, operable program or batch file.' and error 'ModuleNotFoundError: No module named 'seaborn'' – speck Oct 13 '22 at 15:23
  • 1
    I also tried !pip install seaborn which gives 'Defaulting to user installation because normal site-packages is not writeable' and 'Requirements already satisfied' yet I still can't import seaborn – speck Oct 13 '22 at 15:27