Search code examples
pythonjupyter-notebookshapefilepyshp

How to import shapefile


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?


Solution

  • 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