简体   繁体   English

如何在从 WSL2+ubuntu20.04 运行的 Jupyter 笔记本中安装 Python 包

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

I am running jupyter notebook from WSL2+Ubuntu20.04.我正在从 WSL2+Ubuntu20.04 运行 jupyter notebook。 However, the jupyter notebook image does not include many package.但是,jupyter notebook 镜像中并没有很多 package。

For instance, I would like to install pandas and append sys path accordingly.例如,我想相应地安装 pandas 和 append 系统路径。

(UPDATE) Based on the recommendation of Prayson, I did the following from Jupytr notebook terminal: (更新)根据 Prayson 的建议,我从 Jupytr 笔记本终端执行了以下操作:

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:但是,当我关闭并再次重新启动时,我无法再次导入 pandas 并且出现以下错误:

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.从上面的代码跟踪来看,Pandas 似乎安装在为 Jupyter 服务器服务的环境中。

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.上面的代码确保在您当前位于 session 的环境中安装了 package。

You can open terminal session in Jupyter that will give you assess to shell.您可以在 Jupyter 中打开terminal session,这将使您评估 shell。 I would recommend installing packages there as you would in normal terminal.我建议像在普通终端中一样在那里安装软件包。 See Jupyter Documentation for how to access terminal有关 如何访问终端的信息,请参阅 Jupyter 文档

You just need to install the package from the terminal, no need to do anything in Jupyter.您只需要从终端安装 package,无需在 Jupyter 中执行任何操作。

To install pandas, type in terminal:要安装 pandas,请输入终端:

pip install pandas

Now import in jupyter, it will work fine.现在在 jupyter 中导入,它会正常工作。

Also, no need to use sudo.此外,无需使用 sudo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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