如何在从WSL2 + ubuntu20.04运行的Jupyter Notebook中安装Python软件包 [英] How do I install Python packages in Jupyter notebook running from WSL2+ubuntu20.04
问题描述
我正在从WSL2 + Ubuntu20.04运行jupyter笔记本.但是,jupyter笔记本图像不包含很多软件包.
I am running jupyter notebook from WSL2+Ubuntu20.04. However, the jupyter notebook image does not include many package.
例如,我想安装pandas并相应地附加sys路径.
For instance, I would like to install pandas and append sys path accordingly.
(更新)根据Prayson的建议,我在Jupytr笔记本电脑终端上进行了以下操作:
(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.
任何指导将不胜感激.
推荐答案
确保在正确的环境中安装软件包.从上面的代码跟踪中可以看出,Pandas已安装在为Jupyter服务器提供服务的环境中.
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.
您可以在Jupyter中打开 terminal
会话,这将使您可以评估shell.我建议像在普通终端中那样在那儿安装软件包.有关如何访问终端的信息,请参见 Jupyter文档
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
这篇关于如何在从WSL2 + ubuntu20.04运行的Jupyter Notebook中安装Python软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!