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

hi-agi 2024-10-04 08:09:22 阅读 58

1. 安装

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

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

1.1 linux

1.1.1 ubuntu

Mac、Windows 及其他 Linux 系统类似

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

Anaconda3-2024.06-1-Linux-x86_64(example)

<code># 下载安装包

# 最新版官网: 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 前先执行如下命令,不然会提示 conda 找不到,如果不是 bash 换成其他的

eval "$(/home/ubuntu/anaconda3/bin/conda shell.bash hook)"

# 若执行成功,去~/.bashrc能看到类似" >>> conda initialize >>>"的文字

conda init

# 不一定是 bashrc(灵活)

source ~/.bashrc

2. 使用

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

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

2.1 set mirror

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/cloud

msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

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. 资源

3.1 anaconda

download

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

docs

https://docs.anaconda.com/

3.2 miniconda

官网

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

3.3 pypi

官网

https://pypi.org/

3.4 mirrors

3.4.1 tsinghua

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/


上一篇: 10 月 3 日解题报告

下一篇: 【C++】设计用户级缓冲区

本文标签

ENV   


声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。