Anaconda是一个用于集成包和环境的管理工具,致力于简化软件管理系统和部署。此外我们还可以利用conda指令对虚拟环境进行方便的创建、激活、删除、退出和配置。pip 则是Python 包的管理工具,提供了对Python 包的查找、下载、安装、卸载的功能。
在日常的开发使用过程中,时不时会碰到对一些环境的管理和相关包的一些操作,然而网络上充斥着大量老旧且无效解决方案的博客,有时甚至会使原本已经配置好的炼丹装置毁于一旦,俗称“坑爹”。哈哈哈。
pip更新到最新版本:
pip install --upgrade pip
pip设置镜像源:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
pip删除镜像源
pip config unset global.index-url
pip查看镜像源
pip config list
然后列举出国内几个比较常用的镜像源:
http://mirrors.aliyun.com/pypi/simple/ # 阿里云
https://pypi.mirrors.ustc.edu.cn/simple/ # 中国科技大学
http://pypi.douban.com/simple/ # 豆瓣(douban)
https://pypi.tuna.tsinghua.edu.cn/simple/ # 清华大学
http://pypi.mirrors.ustc.edu.cn/simple/ # 中国科学技术大学
当然,因为一些xx因素,这些源有时候会被关闭,至于能不能用关键还是看运气了。下面梳理下一些常用的指令。
pip安装包
pip install $pkg_name -i http://pypi.douban.com/simple # 临时使用指定镜像源安装包,可自行更改
conda添加镜像源
指令如下:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
最后一行是用于显示下载的通道源。以pytorch下载和按照为例,我们应该先到对应源的网站上去查看是否有相应的镜像链接。比如清华源中pytorch网站,进入后可以根据我们的操作系统选择点击进入查看是否有我们所需要的版本号。当然,我们在使用pytorch官方指令安装的时候要注意去掉 -c ,这个加上去会使用默认的conda源。
而在Linux系统下,可以尝试修改以下配置文件,然后将里面的channels按照上面所示的镜像源地址添加进去即可:
sudo gedit ~/.condarc
检查镜像源:
conda config --show # 显示当前配置状态
conda config --get channels # 查看当前配置状态
删除镜像源
# 删除当前的镜像源
conda config --remove channels https://pypi.mirrors.ustc.edu.cn/simple/
# 移除所有镜像源
conda config --remove-key channels
conda常用指令:
conda -h # 通过使用帮助指令,可以对其他指令功能一目了然
conda --version # 查看当前的conda版本
conda list # 查看当前已安装包
conda install $pkg_name # 安装包
conda uninstall $pkg_name # 卸载包
conda info --envs # 显示当前以创建的虚拟环境
conda update conda # 检查并更新到conda最新版本
conda update --all # 更新所有的安装包
conda update python # 将python更新至最新版本
conda remove --name $pkg_name # 删除当前虚拟环境下的某个包
conda remove --name $env_name $pkg_name # 删除某个虚拟环境下的某个包
conda env export -n $env_name -f $env_name.yml # 将配置导出为yml文件
conda env upload -f $env_name.yml # 上传yml文件
conda create -n $env_name -clone $cp_env_name # 复制当前虚拟环境
conda search $pkg_name # 搜索某个包的信息
conda clean -p # 清理没有使用过的包
conda list -e > requirements.txt # 将当前虚拟环境下的所有包进行导出
Windows系统下则在当前系统用户目录下创建一个pip文件夹,如C:\Users\Administrator\pip,然后在当前目录下创建并编辑pip.ini文件
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn
Pycharm下设置镜像源:
这里的镜像源可根据需要自行修改。除此之外,我们还可以在Pycharm下进行设置:
File → Settings → Project:Name → Project Interpreter → ‘+’ 添加模块 → Manage Repositories → 将相应镜像源添加进去。
用pip生成当前环境下的requirements文件
pip freeze > requirements.txt
用pip安装当前环境下的requirements文件
pip install -r requirements.txt
Linux下,可以一次性设置修改 ~/.pip/pip.conf 文件,
若不存在则手动创建:
# vim ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn
cmd–docs命令下展示效果如图:
(c) Microsoft Corporation。保留所有权利。
C:\Users\29328>conda -h
usage: conda-script.py [-h] [-V] command ...
conda is a tool for managing and deploying applications, environments and packages.
Options:
positional arguments:
command
clean Remove unused packages and caches.
compare Compare packages between conda environments.
config Modify configuration values in .condarc. This is modeled after the git config command. Writes to the
user .condarc file (C:\Users\29328\.condarc) by default.
create Create a new conda environment from a list of specified packages.
help Displays a list of available conda commands and their help strings.
info Display information about current conda install.
init Initialize conda for shell interaction. [Experimental]
install Installs a list of packages into a specified conda environment.
list List linked packages in a conda environment.
package Low-level conda package utility. (EXPERIMENTAL)
remove Remove a list of packages from a specified conda environment.
uninstall Alias for conda remove.
run Run an executable in a conda environment. [Experimental]
search Search for packages and display associated information. The input is a MatchSpec, a query language
for conda packages. See examples below.
update Updates conda packages to the latest compatible version.
upgrade Alias for conda update.
optional arguments:
-h, --help Show this help message and exit.
-V, --version Show the conda version number and exit.
conda commands available from other packages:
build
convert
debug
develop
env
index
inspect
metapackage
render
server
skeleton
verify
C:\Users\29328>conda --ver
conda 4.9.2
C:\Users\29328>conda list
# packages in environment at C:\ProgramData\Anaconda3:
#
# Name Version Build Channel
_ipyw_jlab_nb_ext_conf 0.1.0 py38_0
absl-py 0.12.0 pypi_0 pypi
alabaster 0.7.12 py_0
anaconda 2020.11 py38_0
anaconda-client 1.7.2 py38_0
anaconda-navigator 1.10.0 py38_0
anaconda-project 0.8.4 py_0
anyio 3.0.1 pypi_0 pypi
argh 0.26.2 py38_0
argon2-cffi 20.1.0 py38he774522_1
可能出现的问题:
如果用pip提示找不到相关库,可以切换下用conda指令安装;
如果用某个源下载速度满,即可多切换下不同的镜像源试试,比如有时候用清华源只有10k,换个豆瓣源有2M多;
如果提示连接超时,代理设置有问题的,可以检查下自己的vpn是否开启了全局,可改成PAC模式;
如果出现“conda Collecting package metadata (current_repodata.json): failed”,说明当前设置的镜像源可能失效,可直接通过下面指令依次执行后,再重新安装你需要的包:
cls
conda config --remove-key channels
conda update conda
conda update --all
conda config --add channels conda-forge
conda config --set channel_priority flexible