Python手册(1) Anaconda & pip

本文详细介绍了如何使用Anaconda和pip进行环境和包管理,包括安装、配置镜像源、环境创建与管理、包安装与卸载等关键操作。同时,提供了修改默认路径和设置启动脚本的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0. 资料

1. Anaconda

1.1. 安装

  • 建议使用清华镜像下载
  • 安装完成修改源,即修改~/.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
  deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
  • Windows直接安装。
  • Ubuntu安装如下
sudo bash Anaconda3-4.3.0-Linux-x86_64.sh 
# 一路回车+yes(注意最后一步是问是否要将Anaconda安装到系统路径)
  • centos安装:
    • 需要先安装bunzip2,命令为 yum install -y bzip2,否则会报错 bunzip2: command not found
    • 之后就 bash Anaconda3-4.3.0-Linux-x86_64.sh 即可。

1.2. 环境相关

# 安装
conda create --name tensorflow python=3.6

# 激活
activate tensorflow # Windows
source activate tensorflow # Linux & Mac

# 撤销激活
deactivate tensorflow # Windows
conda deactivate tensorflow # Linux & Mac

# 删除
conda remove --name tensorflow --all

# 复制某环境
conda create --name new_env --clone old_env

# 重命名环境:先复制再删除

1.3. package相关

conda list
conda install numpy
conda uninstall numpy
conda install tensorflow-gpu=1.12.0

1.4. 修改envs和pkgs的默认路径

  • 默认envs和pkgs位于~/anaconda3目录下,占用空间太多。
  • 可以修改 ~/.condarc 来设置envs和pkgs的路径。经过下面的配置,新环境将安装在/path/to/new/envs中,但如果环境在 /path/to/new/envs 中不存在,就会到 /home/ubuntu/anaconda3/envs 中寻找有没有对应的环境。
envs_dirs:
  - /path/to/new/envs
  - /home/ubuntu/anaconda3/envs
pkgs_dirs:
  - /path/to/new/pkgs
  - /home/ubuntu/anaconda3/pkgs
  • 经过上面修改后,旧环境可能会报错,类似于:
    • -bash: /usr/local/bin/pip: /usr/bin/python: bad interpreter: No such file or directory
    • 解决方法是进入虚拟环境后,执行命令 python -m pip install --upgrade pip.

1.5. 设置启动/结束脚本

  • 功能描述:在运行 conda activate envconda deactivate 时可以运行响应的脚本。
  • 应用举例:可以在脚本中设置环境变量。
  • 设置方法:
    • 对于每个环境,假设其根目录为 /path/to/env/
    • 运行 conda activate env 时启动的脚本可以在 /path/to/env/etc/conda/activate.d/ 中建立。
    • 运行 conda deactivate 时启动的脚本可以在 /path/to/env/etc/conda/deactivate.d/ 中建立。
    • 如果上述两个目录不存在,可以自己手动建立。

1.6. 碰到的问题

  • 使用Conda安装Keras时,不能使用tensorflow-gpu版,只能使用cpu版。
    • 解决方案:要么 keras 和 tensorflow 分别安装,要么使用 keras-gpu。
  • 不能安装happybase。
    • 解决方案:使用pip安装。

2. pip

2.1. 镜像

  • windows镜像设置,修改%HOMEPATH%\pip\pip.ini
  • linux镜像设置,修改~/.pip/pip.conf
  • 镜像文件内容如下
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com
  • 镜像选择
    • 阿里云:http://mirrors.aliyun.com/pypi/simple/
    • 中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
    • 豆瓣(douban):http://pypi.douban.com/simple/
    • 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/
    • 中国科学技术大学:http://pypi.mirrors.ustc.edu.cn/simple/

2.2. 其他

  • 指定版本:pip install tensorflow==1.12.0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值