健康等于财富 发表于 2023-10-18 20:25:19

Linux 下安装 miniconda,管理 Python 多环境

安装 miniconda

1、下载安装包 Miniconda3-py37_22.11.1-1-Linux-x86_64.sh,或者自行选择版本
2、把安装包上传到服务器上,这里放在 /home/software
3、安装
bash Miniconda3-py37_22.11.1-1-Linux-x86_64.sh 4、按回车
Welcome to Miniconda3 py37_22.11.1-1

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 5、按空格跳到最下面,输入yes
Do you accept the license terms?
>>>yes6、选择安装位置,这里选择默认,直接回车,有需要可以自己输入改掉
Miniconda3 will now be installed into this location:
/root/miniconda3

- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below

>>>
PREFIX=/root/miniconda37、初始化 miniconda,输入 yes
installation finished.
Do you wish the installer to initialize Miniconda3
by running conda init?
>>> yes8、现在 conda 命令是找不到的,需要激活
source ~/.bashrc激活后可以看到启动了 base 环境,conda 命令也可以用了,下一节会介绍常用 conda 命令。
9、可以设置启动时,不自动激活 base 环境
conda config --set auto_activate_base false10、设置 conda 镜像源
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/main11、设置 pip 镜像
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com12、查看 python 环境
(base) # python
Python 3.7.15 (default, Nov 24 2022, 21:12:53)
:: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello')
hello
>>> exit()可以看到默认的 python 环境是 3.7,也就是我们安装的 miniconda 的 python 版本。
并且 python2 还是存在的。
(base) # python2
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello')
hello
使用 miniconda 管理 python 多环境

这里简单介绍一些常用的 conda 命令。
1、查看所有环境
conda env list2、激活某个环境, 为环境名
conda activate <name>3、退出当前环境
conda deactivate4、创建虚拟环境,指定名字和 python 版本
conda create --name <name> python=3.x5、克隆一个环境
conda create --name <new_name> --clone <old_name>6、删除某个环境
conda remove --name <name> --all7、conda 清空缓存
conda clean -y -all8、conda 镜像源
# 查看镜像源conda config --show-sources# 添加镜像源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/main# 从镜像源中安装包时显示来源conda config --set show_channel_urls yes# 删除镜像源conda config --remove channels https://XXX# 删除配置的镜像源,使用默认镜像源conda config --remove-key channels9、pip 镜像源
# 查看配置pip config list# 添加镜像源pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com# 清除缓存rm -rf ~/.cache/pip# windows 下 pip 配置文件位置C:\Users\Administrator\AppData\Roaming\pip# Linux 下 pip 配置文件位置/root/.config/pip/pip.confpip 配置文件参考内容

cache-dir = D:\Environment\Miniconda3\pip\cache
index-url = https://mirrors.aliyun.com/pypi/simple/
extra-index-url=
      https://pypi.tuna.tsinghua.edu.cn/simple/
      http://pypi.douban.com/simple/
      http://pypi.mirrors.ustc.edu.cn/simple/


trusted-host=
      pypi.tuna.tsinghua.edu.cn
      mirrors.aliyun.com
      pypi.douban.com
      pypi.mirrors.ustc.edu.cn10、导出与导入当前环境配置
conda env export > environment.yaml
conda env create -f environment.yaml
卸载 miniconda

1、删除 miniconda3 文件夹
rm -rf ~/miniconda3/2、删除 .conda 文件夹和 condarc 文件
rm -rf ~/.conda
rm -rf ~/.condarc3、删除配置中 conda 相关
vim ~/.bashrc删除或注释下面这段
# >>> conda initialize >>># !! Contents within this block are managed by 'conda init' !!__conda_setup="$('/root/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"if [ $? -eq 0 ]; then    eval "$__conda_setup"else    if [ -f "/root/miniconda3/etc/profile.d/conda.sh" ]; then      . "/root/miniconda3/etc/profile.d/conda.sh"    else      export PATH="/root/miniconda3/bin:$PATH"    fifiunset __conda_setup#
页: [1]
查看完整版本: Linux 下安装 miniconda,管理 Python 多环境