ToB企服应用市场:ToB评测及商务社交产业平台

标题: Python 虚拟环境安装使用(Anaconda 完备实操版) [打印本页]

作者: 风雨同行    时间: 2024-9-22 16:53
标题: Python 虚拟环境安装使用(Anaconda 完备实操版)
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)

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

配置 conda 和 pip 的国内镜像源后,通过 conda 来管理 python 虚拟环境,通过 pip 来安装第三方 python 库(也可以通过 conda 来安装)
注:python 虚拟环境的管理也可以通过 virtualenvwrapper 等其他工具
2.1 set mirror

2.1.1 conda

set

  1. # Windows下执行(其他系统跳过)
  2. conda config --set show_channel_urls yes
  3. # 新建/更新conda配置文件
  4. vim ~/.condarc
  5. # 内容如下
复制代码
  1. channels:
  2.   - defaults
  3. show_channel_urls: true
  4. default_channels:
  5.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  6.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  7.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
  8. custom_channels:
  9.   conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  10.   msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  11.   bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  12.   menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  13.   pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  14.   pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  15.   simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
复制代码
2.1.2 pip

list

  1. # pip源列表,此处只是记录人工整理镜像源,不涉及任何操作(下面配置镜像源的时候,从这里手动拷贝一个/多个过去)
  2. 官方:https://pypi.org/simple
  3. 清华:https://pypi.tuna.tsinghua.edu.cn/simple
  4. 百度:https://mirror.baidu.com/pypi/simple/
  5. 阿里:https://mirrors.aliyun.com/pypi/simple/
  6. 豆瓣:https://pypi.douban.com/simple/
  7. 中科大:https://pypi.mirrors.ustc.edu.cn/simple/
  8. ...
复制代码
set

  1. # 临时使用
  2. # schema
  3. pip install [package] -i [url]
  4. # example
  5. pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
  6. # 长期设置(推荐)
  7. # schema
  8. pip config set global.index-url [url]
  9. # example
  10. pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  11. 注:也可以通过 pip config set global.extra-index-url "<url1> <url2> ..." 配置多个镜像源
复制代码
2.2 create env

  1. # 创建虚拟环境
  2. # schema
  3. conda create -n env_name python=xxx
  4. # example
  5. conda create -n test python=3.10
复制代码
2.3 activate env

  1. # 激活虚拟环境
  2. # schema
  3. conda activate env_name
  4. # example
  5. conda activate test
复制代码
2.4 install package

实行完这一步,基本python环境已经搭建好了
  1. # 通过pip安装第三方python库
  2. # 直接安装指定包(一个/多个)
  3. # schema
  4. pip install xxx1 xxx2
  5. # example
  6. pip install numpy pandas
  7. # 通过requirements.txt安装多个包
  8. pip install -r requirements.txt
复制代码
2.5 remove package

这里开始,按需使用
  1. # 删除某个第三方python库(应该同理可以批量删除)
  2. # schema
  3. pip uninstall xxx
  4. # example
  5. pip uninstall numpy
复制代码
2.6 freeze package

  1. # 生成当前python环境的requirements.txt(一般手动维护requirements.txt)
  2. pip freeze > requirements.txt
复制代码
2.7 list env

  1. # 查看当前所有虚拟环境
  2. conda env list
复制代码
2.8 remove env

  1. # 删除错误/弃用的虚拟环境
  2. # schema
  3. conda remove -n env_name --all
  4. # example
  5. conda remove -n test --all
复制代码
2.9 deactivate env

  1. # 退出虚拟环境(回到base环境)
  2. conda deactivate
  3. # 注: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/

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4