Search code examples
pythonimportpipperfplot

I tried installing perfplot using pip on jupyter notebooks, and it succesfully installed but importing throws error


The python perfplot library throws an error when i try to import it. installed using pip on jupyter notebook (i have installed many libraries this way in the past with no issues)

I installed the library. Then I tried importing but got an error

TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_2672\2052783381.py in <module>
----> 1 import perfplot
.
.
.
~\AppData\Roaming\Python\Python39\site-packages\numpy\_typing\_generic_alias.py in <module>
    239 
    240 if TYPE_CHECKING or sys.version_info >= (3, 9):
--> 241     _DType = np.dtype[ScalarType]
    242     NDArray = np.ndarray[Any, np.dtype[ScalarType]]
    243 else:

TypeError: 'type' object is not subscriptable

Here are the respective screenshots: install using pip

importing and getting error


Solution

  • Try running %pip install perfplot inside your notebook, then restart the kernel, and then try the import perfplot.


    Use of the %pip install magic

    If you are curious about the magic %pip install command that I suggest you use, see here. Even if the above command doesn't help in this situation because of the normal site-packages is not writeable- issue, I suggest from now on you use that version of the pip install command in the future when running installations from inside a Jupyter notebook file to insure that the installation occurs in the environment being used by the kernel underlying the active notebook.

    Most pertinent to what you show in your screenshot:
    Often you'll see outdated suggestions of using an exclamation point in conjunction with the pip install command (similar for conda install). The use of an exclamation point in conjunction with pip can cause issues, and may be what you are experiencing. See the second paragraph here for more about the possible deficiency of the exclamation point in conjunction with pip install, and so the magic install commands for pip and conda were added a few years ago to Jupyter (even the Google Colab now).

    Because automagics are often on by default in modern Jupyter you are actually better off using no symbol in conjunction with the install commands than using an exclamation point. Without a symbol, the magic version will get used behind-the-scenes. However, explicit is always best, and so I suggest keeping the % symbol so that it is clear to you and others what is happening.