Python 虚拟环境安装使用(Anaconda 完整实操版)

ops/2024/9/22 19:28:49/

1. 安装

安装 anaconda(包含 pythonpip 等,支持创建及管理多个 python 虚拟环境

注:miniconda 可能也可以,但是没用过,优先 anaconda

1.1 linux

1.1.1 ubuntu

Mac、Windows 及其他 Linux 系统类似

注:一般不使用 root 用户,使用其他非 root 用户(方便使用 homebrew 等)

conda32024061Linuxx86_64example_14">Anaconda3-2024.06-1-Linux-x86_64(example)
# 下载安装包
# 最新版官网: https://www.anaconda.com/download/success
# 清华源下载:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=A
# 如果官方下载速度不给力,可以从清华源下载(另外记得做好安装包的管理/归档,或者安装完成之后及时删除)
wget https://repo.anaconda.com/archive/Anaconda3-2024.06-1-Linux-x86_64.sh# 安装
bash Anaconda3-2024.06-1-Linux-x86_64.sh
# 注: 最后有一个是否 conda init,优先输入`yes`,这样后面 conda 的使用更方便(开始安装后不要回车,不然就默认`no`了)# 更新系统环境变量
# ~/.bashrc 在不同系统下,可能在 ~/.zshrc、~/.profile、~/.bash_profile、~/.bash_login、~/.profile 等文件中
source ~/.bashrc# 确认是否安装成功,打印"conda xx.x.x"就成功了(或者看前面是否出现了"(base)",没有的话重启/新开终端)
conda -V
# 手动 conda init(如果安装的时候没有 conda init,可以手动 conda init)
conda init
source ~/.bashrc

2. 使用

配置 condapip 的国内镜像源后,通过 conda 来管理 python 虚拟环境,通过 pip 来安装第三方 python 库(也可以通过 conda 来安装)

注:python 虚拟环境的管理也可以通过 virtualenvwrapper 等其他工具

2.1 set mirror

conda_49">2.1.1 conda
set
# Windows下执行(其他系统跳过)
conda config --set show_channel_urls yes# 新建/更新conda配置文件
vim ~/.condarc# 内容如下
channels:- defaults
show_channel_urls: true
default_channels:- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudmsys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudbioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudmenpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudpytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudpytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudsimpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pip_81">2.1.2 pip
list
# pip源列表,此处只是记录人工整理镜像源,不涉及任何操作(下面配置镜像源的时候,从这里手动拷贝一个/多个过去)官方:https://pypi.org/simple
清华:https://pypi.tuna.tsinghua.edu.cn/simple
百度:https://mirror.baidu.com/pypi/simple/
阿里:https://mirrors.aliyun.com/pypi/simple/
豆瓣:https://pypi.douban.com/simple/
中科大:https://pypi.mirrors.ustc.edu.cn/simple/
...
set
# 临时使用
# schema
pip install [package] -i [url]
# example
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple# 长期设置(推荐)
# schema
pip config set global.index-url [url]
# example
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple注:也可以通过 pip config set global.extra-index-url "<url1> <url2> ..." 配置多个镜像源

2.2 create env

# 创建虚拟环境
# schema
conda create -n env_name python=xxx
# example
conda create -n test python=3.10

2.3 activate env

# 激活虚拟环境
# schema
conda activate env_name
# example
conda activate test

2.4 install package

执行完这一步,基本python环境已经搭建好了

# 通过pip安装第三方python# 直接安装指定包(一个/多个)
# schema
pip install xxx1 xxx2
# example
pip install numpy pandas# 通过requirements.txt安装多个包
pip install -r requirements.txt

2.5 remove package

这里开始,按需使用

# 删除某个第三方python库(应该同理可以批量删除)
# schema
pip uninstall xxx
# example
pip uninstall numpy

2.6 freeze package

# 生成当前python环境的requirements.txt(一般手动维护requirements.txt)
pip freeze > requirements.txt

2.7 list env

# 查看当前所有虚拟环境
conda env list

2.8 remove env

# 删除错误/弃用的虚拟环境
# schema
conda remove -n env_name --all
# example
conda remove -n test --all

2.9 deactivate env

# 退出虚拟环境(回到base环境)
conda deactivate
# 注:root用户在切换到其他用户前,先退出虚拟环境,不然可能会影响其他用户的conda环境的激活

3. 资源

conda_198">3.1 anaconda

download

https://www.anaconda.com/download/success

docs

https://docs.anaconda.com/

conda_208">3.2 miniconda

官网

https://docs.anaconda.com/miniconda/

3.3 pypi

官网

https://pypi.org/

3.4 mirrors

3.4.1 tsinghua
conda_224">3.4.1.1 anaconda
download

https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=A

官网

https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/

3.4.1.2 pypi
官网

https://mirrors.tuna.tsinghua.edu.cn/help/pypi/


http://www.ppmy.cn/ops/114388.html

相关文章

论 JAVA 集合框架中 接口与类的关系

前言 这是笔者在学习过程中的一篇"备忘录",其目的是能用最EZ最粗鄙的语言口述出 JAVA集合框架中 所有类与接口的关系 本人在不断地学习中,总会混淆集合框架中的类和接口,以及它们的作用关系,虽然不影响我的使用,但是我也不想一直糊涂下去,故而趁知识还没混淆之际,赶…

React 中的延迟加载

延迟加载是 Web 开发中的一种有效的性能优化技术&#xff0c;尤其是对于 React 等库和框架。它涉及仅在需要时加载组件或资源&#xff0c;无论是响应用户操作还是当元素即将在屏幕上显示时。这可以减少应用程序的初始加载时间&#xff0c;减少资源消耗&#xff0c;并改善用户体…

Ubuntu 不重装系统增加交换空间大小

目录 一、设置交换文件 二、删除重新创建交换文件 一、设置交换文件 1、创建新的交换文件 使用 dd 命令创建一个新的交换文件。例如&#xff0c;创建一个4GB的交换文件&#xff1a; sudo dd if/dev/zero of/swapfile bs1G count4 2、设置交换文件的权限 为了安全起见&am…

分享课程:云LAN到家视频教程

云LAN到家项目包括四个端&#xff1a;用户端(小程序)、服务端&#xff08;app&#xff09;、机构端(PC)、运营管理端(PC)&#xff0c; 四个端对应四类用户角色&#xff1a; 家政需求方&#xff1a;通过用户端小程序完成在线预约下单、支付、评价、投诉、退款等操作。 家政服务人…

Python在数据科学与机器学习中的应用

Python 是数据科学与机器学习领域的首选语言之一&#xff0c;广泛应用于数据处理、分析、建模以及预测任务中。Python 拥有丰富的库和工具&#xff0c;能够帮助开发者高效处理数据&#xff0c;并构建各种机器学习模型。下面我们将详细介绍 Python 在数据科学与机器学习中的应用…

【MySQL】MySQL连接池原理与简易网站数据流动是如何进行

一、MySQL连接池 我们在基础I/O中学习了线程池&#xff0c;因此&#xff0c;我们可以将MySQL引入线程池中。如果在实际业务中出现频繁连接数据库的情况时&#xff0c;我们需要创建一些线程&#xff0c;然后通过线程来创建MySQL的连接。在每一个线程中&#xff0c;我们线程启动前…

UniApp一句话经验: px -> rpx动态转换和动态元素区域的获取

px->rpx转换 在多终端条件下&#xff0c;什么devicePixelRatio&#xff0c;upx2px都是不靠谱的&#xff0c;最直接的是这样&#xff1a; const { screenWidth } uni.getSystemInfoSync()const pixelUnit screenWidth / 750 // rpx->px比例基数 动态元素区域获取 多终…

SAP学习笔记 - 开发06 - CDSView + Fiori Element 之 List Report

上一章讲了Fiori UI5开发环境搭建和实践&#xff1a; - VSCode 安装Fiori Tools插件 - SEGW 创建后台程序&#xff0c;注册服务&#xff0c;Gateway Client确认服务 - 使用SEGW公开的服务来查询数据显示到页面 SAP学习笔记 - 开发05 - Fiori UI5 开发环境搭建2 Fiori Tools…