持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第12天, 点击查看活动详情
本文主要汇总了win操作系统下的 Python
pip和conda的相关操作命令,方便后续查看(复制粘贴)。
本文目录
- 将python添加至系统环境变量
- 如何添加镜像源(conda & pip)
- 永久改变镜像源
- 使用临时镜像
- 其他国内镜像
添加环境变量
对于使用anaconda的用户:将anaconda\Scripts放到系统变量Path中即可。
步骤如下:
此电脑->属性->高级系统设置->环境变量->系统变量找到path->新建->复制anaconda路径(..\anaconda\Scripts)->连续三个确定即可。
如果使用的是python.exe + pip的用户:需要将python\以及python\Scripts均添加到Path中。
步骤同上。
类似于下面这种:SoftWare目录下包含了python.exe文件。(这下够清楚了吧?)
这里随便扯一句,有时候从GitHub上拉一个项目下来,里面好多包都没有,这时候可以把所有的包以及版本以
numpy==x.x.x的形式放在一个requirements.txt文件下,然后使用下面的命令就不用自己一个一个下载了~
打包所有包到txt文件中
- 方法一:此种方式会将环境中所有的包都打包进去,适合单虚拟环境
pip freeze > requirements.txt
- 方法二:总结程序中所用到的包(推荐)
# 先安装pipreqs库
pip install pipreqs
# 再cd到项目所在的根目录,执行以下命令
pipreqs ./ --encoding=utf8 # Windows下得加encoding=utf8,不然可能出现编码错误
requirements.txt这个文件生成在当前路径
使用requirements.txt安装
- 在线安装
# 在线安装
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
-
离线安装
- 第一步:将requirements.txt中导入的包离线下载到
packagesdir文件夹
- 第一步:将requirements.txt中导入的包离线下载到
pip wheel -w DIR -r requirements.txt
pip download -d DIR -r requirements.txt
DIR:离线包(temp)的路径(例如:D:\Desktop\packagesdir) 安装requirements.txt中的包,并且在D:\Desktop\packagesdir这个文件夹里取离线的包
-
- 第二步:安装离线的包
pip install --no-index --find-links=DIR -r requirements.txt
添加镜像源(anaconda)
一般镜像源都添加清华的,(别问,问就是厉害
下面是常用命令介绍:
# 配置之前查看
conda config --get channels
# 删除之前的镜像,恢复默认
conda config --remove-key channels
# 添加清华镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
#显示检索路径
conda config --set show_channel_urls yes
#显示镜像通道
conda config --show channels
conda常用命令
conda info -e # 查看所有环境
# 创建一个名为python34的环境,指定Python版本是3.5(不用管是3.5.x,conda会为我们自动寻找3.5.x中的最新版本)
conda create --name py35 python=3.5
# 删除一个已有的环境
conda remove --name py35 --all
# 返回主环境
deactivate py35 # for Windows
# 安装好后,使用activate激活某个环境
activate py35 # for Windows
source activate py35 # for Linux & Mac
# 安装package
conda install -n py35 numpy
# 如果不用-n指定环境名称,则被安装在当前活跃环境
# 更新
conda update -n py35 numpy
# 查看某个指定环境的已安装包
conda list -n py35
# 查看已经安装的packages
conda list
# 更新conda,保持conda最新
conda update conda
conda & pip 指定安装目录
conda create --prefix=D:\指定的目录1\环境名称 python=3.6
pip install --target=D:\指定的目录2 包的名称
pip切换镜像(Wins)
1.打开命令行窗口
2.依次输入下面前三条命令即可
# 切换为清华镜像
pip config --global set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
# 添加镜像源的依赖项
pip config --global set install.trusted-host pypi.tuna.tsinghua.edu.cn
# 查看镜像配置信息
pip config list
# 删除相应配置文件
pip config --global unset global.index-url
pip config --global unset install.trusted-host
# 模块安装卸载
pip uninstall numpy # 卸载
临时用镜像
临时用镜像直接添加-i参数即可。后面加上镜像源的链接。(这里仍然推荐清华~)
pip install <包名> –trusted-host http://mirrors.aliyun.com/pypi/simple/
pip install xxx -i https://pypi.tuna.tsinghua.edu.cn/simple
其他国内镜像
mirrors.aliyun.com/pypi/simple… 阿里
pypi.hustunique.com/simple/ 华中理工大学
pypi.sdutlinux.org/simple/ 山东理工大学
pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学