管理第三方库
更新时间:2026-03-12
在开发机实例中,您可以通过 Terminal 直接安装、查看、卸载、更新 Python 第三方库。变更会保存在开发机实例的磁盘中;若创建时挂载了云磁盘或百舸云盘,停止实例后数据会保留。
安装第三方库
开发机提供的开发环境包括 Python3、pip 和 apt。安装第三方库时,默认安装至当前 Python 环境;若预置镜像提供多个 conda 环境,需先激活目标环境再安装。具体命令格式如下所示。
Bash
1# 在当前 Python 环境安装(默认)。
2pip install <yourLibraryName>
3
4# 若使用 conda 多环境,先激活目标环境再安装,例如:
5# conda activate <环境名>
6# pip install <yourLibraryName>
需要将<yourLibraryName>替换为待安装的第三方库名称。例如,使用pip install bottle命令,安装bottle库。
如果您在Jupyter Notebook的Cell中执行安装命令,请在命令前加上英文感叹号!,例如:!pip install bottle。
重要
如果成功安装了三方包但是在Notebook中依然找不到,请尝试重新加载Kernel。
查看第三方库
使用以下命令查看已安装的第三方库。
Bash
1pip list
卸载第三方库
使用以下命令卸载已安装的第三方库。
Bash
1pip uninstall <yourLibraryName>
需要将<yourLibraryName>替换为已安装的第三方库名称。
说明
只能卸载自己安装的第三方库。
更新第三方库
使用以下命令更新已安装的第三方库。
Bash
1pip install --upgrade numpy==<versionNumber>
需要将<versionNumber>替换为待安装的numpy版本号。
查看或更改pip源
执行如下命令查看pip配置文件:
Bash
1pip config list
返回结果中 global.index-url 参数的值即为全局 pip 源地址。返回示例如下:
Bash
1global.index-url='http://mirrors.baidubce.com/pypi/simple/'
2global.trusted-host='mirrors.baidubce.com'
3install.trusted-host='mirrors.baidubce.com'
安装第三库时临时设置pip源:
Bash
1pip install -i https://pypi.tuna.tsinghua.edu.cn/simple <yourLibraryName>
2# 如果出现SSL验证失败的错误,请尝试加上--trusted-host参数,示例如下:
3pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn <yourLibraryName>
更改全局pip源。pip配置文件默认路径为~/.config/pip/pip.conf,执行如下命令编辑配置文件:
Bash
1vim ~/.config/pip/pip.conf
以修改为清华pip源为例,修改index-url和trusted-host参数,然后保存并退出。修改后的pip.conf如下:
Bash
1[global]
2trusted-host=pypi.tuna.tsinghua.edu.cn
3index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
4
5[install]
6trusted-host=pypi.tuna.tsinghua.edu.cn
