如何在Jupyter笔记本单元格中运行PIP Install命令

9

我以为我们可以在Jupyter单元格中通过运行如下类似的命令来运行PIP Install或PIP uninstall。

%pip install myPackage

但是我被提示需要确认,类似于这样:

是否继续 (y/n)?

我该如何克服这个问题并在单元格中运行PIP?


根据 pip --help 的输出,有一个 --no-input 选项。我会尝试使用它。 - Paul H
1
pip install -y myPackage - phd
感谢Paul和PHD的建议。但是那并没有起作用。看起来我的环境可能存在问题。我稍后会再试一次。 - Kay
6个回答

7

你尝试过这种安装方式,而不是使用魔法吗?

!pip install <package>

通常这不应该提示您继续。

顺便问一下,您要安装哪个软件包?我已经尝试了使用!pip%pip两种方式。使用%pip时没有提示我继续,但要求我重启内核。

%pip install folium

Collecting folium
  Downloading https://files.pythonhosted.org/packages/c3/83/e8cb37afc2f016a1cf4caab8d22caf7fe4156c4c15230d8abc9c83547e0c/folium-0.12.1-py2.py3-none-any.whl (94kB)
Requirement already satisfied: requests in c:\users\by20064109\appdata\local\continuum\anaconda3\lib\site-packages (from folium) (2.22.0)
Requirement already satisfied: branca>=0.3.0 in c:\users\by20064109\appdata\local\continuum\anaconda3\lib\site-packages (from folium) (0.4.2)
Requirement already satisfied: numpy in c:\users\by20064109\appdata\local\continuum\anaconda3\lib\site-packages (from folium) (1.16.4)
Requirement already satisfied: jinja2>=2.9 in c:\users\by20064109\appdata\local\continuum\anaconda3\lib\site-packages (from folium) (2.10.1)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\by20064109\appdata\local\continuum\anaconda3\lib\site-packages (from requests->folium) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\by20064109\appdata\local\continuum\anaconda3\lib\site-packages (from requests->folium) (1.24.2)
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\by20064109\appdata\local\continuum\anaconda3\lib\site-packages (from requests->folium) (2.8)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\by20064109\appdata\local\continuum\anaconda3\lib\site-packages (from requests->folium) (2019.6.16)
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\by20064109\appdata\local\continuum\anaconda3\lib\site-packages (from jinja2>=2.9->folium) (1.1.1)
Installing collected packages: folium
Successfully installed folium-0.12.1
Note: you may need to restart the kernel to use updated packages.

感谢您的回答,mockash。很抱歉没有早些承认您的帮助。 - Kay
对于那些在这里找到使用感叹号与安装命令的建议的人,请记住这只是一个建议,看看是否有帮助。一般来说,如果您坚持使用现代安装命令%pip install%conda install,您在现代Jupyter中的体验将更加流畅,因为现代魔术命令是为了确保安装发生在与内核运行的同一环境中。在这里了解更多关于现代魔术安装命令的信息。 - undefined

2

使用yes命令代替魔术命令如何?

! yes| pip install myPackage

谢谢Chachay。我试过了,但对我没用。我得到了以下错误:yes: standard output: Broken pipe,yes: write error。 - Kay
你使用 conda 吗?我猜你是这样做的,因为我知道这个包管理器会询问“是否继续(y/n)?”如果 conda 在你的 jupyter 上通过 %pip 运行,那么 !conda install myPackage -y 可以进行静默安装。pip 没有 --yes 选项用于 pip install - Chachay
感谢您的回答,Chachkay。很抱歉没有早些承认您的帮助。 - Kay

2
!pip install package_name

这是在Jupyter笔记本中安装软件包的标准方式。但更好的方法是创建虚拟环境,并在该虚拟环境中安装所有必要的软件包。

1
感谢您的回答,Prakash。很抱歉没有早些承认您的帮助。 - Kay
这是过时的建议,当时写下来的时候就已经过时了。魔术安装命令是在2019年左右添加到现代Jupyter中的,旨在成为发布时的最佳实践,因为它们确保安装发生在内核运行的环境中。感叹号并不一定能做到这一点,这就是为什么添加了魔术版本的原因。请参阅此处了解更多关于确保安装发生在内核运行的环境中的魔术安装命令的信息。 - undefined
[继续] 这里的第二段 更详细地解释了为什么感叹号可能会导致某些用户在系统设置上出现问题。魔术安装命令旨在消除这种变异性,因此现在(以及回答撰写时)是最佳实践,并将确保在Jupyter中获得更好的体验。同样的魔术命令现在也适用于基于Web Assembly(WASM)的JupyterLite。 - undefined

2

感谢您的回答,blondelg。很抱歉没有早些承认您的帮助。 - Kay

1
如果提示与ssh密钥的验证有关,则在运行%pip命令之前,应将主机添加为受信任的主机。例如,对于github。
import os
os.system('ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts')

1
感谢您的回答MCMZL。很抱歉没有早些承认您的帮助。 - Kay

0
在Ubuntu上使用Jupyter Notebooks时,您可能需要更改kernel.json文件,并将其指向conda环境中的Python二进制路径:
在Ubuntu上,您可以尝试以下文件路径和文件:
conda环境名称:rag
vi ~/.local/share/jupyter/kernels/rag/kernel.json

which python

/home/user/miniconda3/envs/rag/bin/python

change the python binary path to the one from which python in the active conda environment


vi ~/.local/share/jupyter/kernels/rag/kernel.json


{
 "argv": [
  "/home/user/miniconda3/envs/rag/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "rag",
 "language": "python",
 "metadata": {
  "debugger": true
 }
}
~


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接