简体   繁体   中英

How do I install Python packages in Jupyter notebook running from WSL2+ubuntu20.04

I am running jupyter notebook from WSL2+Ubuntu20.04. However, the jupyter notebook image does not include many package.

For instance, I would like to install pandas and append sys path accordingly.

(UPDATE) Based on the recommendation of Prayson, I did the following from Jupytr notebook terminal:

python3 -m pip install --upgrade --user pip 
python3 -m pip install --user pandas 

Both steps ran successfully. Then I could call the following code snipper successfully for the FIRST TIME only: I ran the following code snippet:

import sys

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

However, when I shut down and restarted again, I could not import pandas again and I am getting following error:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-3-df58772bf04b> in <module>
      7 
      8 import numpy as np
----> 9 import pandas as pd
     10 import matplotlib.pyplot as plt
     11 

ModuleNotFoundError: No module named 'pandas'

I may be missing adding path. However, I am not sure how to do it correctly.

Any guidance would be appreciated.

Make sure you are installing packages in the correct environment. From the code trace above, it appears that Pandas is installed in environment serving Jupyter server.

import sys

# upgrade pip and install package
!{sys.executable} -m pip install –upgrade pip && !{sys.executable} -m pip install package 

The above code, makes sure that a package is install in the environment that you are currently in session.

You can open terminal session in Jupyter that will give you assess to shell. I would recommend installing packages there as you would in normal terminal. See Jupyter Documentation for how to access terminal

You just need to install the package from the terminal, no need to do anything in Jupyter.

To install pandas, type in terminal:

pip install pandas

Now import in jupyter, it will work fine.

Also, no need to use sudo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM