Conda

基本

查看已有的环境

1
conda info -e

建立一个名为dagan的新环境,并指定python的版本

1
conda create -n dagan python=3.5

进入名为dagan的虚拟环境

1
conda activate dagan

退出

1
conda deactivate

取消自动进入base环境

1
conda config --set auto_activate_base false

恢复自动进入

1
conda config --set auto_activate_base true

删除虚拟环境

1
conda remove -n [dagan] --all

替换清华源

方法一

生成 .condarc 文件:

1
conda config --set show_channel_urls yes

替换文件内容为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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

方法二

直接先把之前的镜像源清一清

1
conda config --remove-key channels

再设置镜像源

1
2
3
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

查看是否替换成功:

1
conda config --show

修改pip默认路径

使用 python -m site 命令查看当前默认路径

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
(math) ghost7@Ghosts-MacBook-Pro ~ % python -m site
sys.path = [
    '/Users/ghost7',
    '/Users/ghost7/miniforge3/envs/math/lib/python38.zip',
    '/Users/ghost7/miniforge3/envs/math/lib/python3.8',
    '/Users/ghost7/miniforge3/envs/math/lib/python3.8/lib-dynload',
    '/Users/ghost7/.local/lib/python3.8/site-packages',
    '/Users/ghost7/miniforge3/envs/math/lib/python3.8/site-packages',
]
USER_BASE: '/Users/ghost7/.local' (exists)
USER_SITE: '/Users/ghost7/.local/lib/python3.8/site-packages' (exists)
ENABLE_USER_SITE: True

发现调用的是 .local 的路径

更改为虚拟环境中的路径

修改 site.py 文件,文件位置在 /Users/ghost7/miniforge3/envs/math/lib/python3.8/site.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
......

# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
# Enable per user site-packages directory
# set it to False to disable the feature or True to force the feature
ENABLE_USER_SITE = None

# for distutils.commands.install
# These values are initialized by the getuserbase() and getusersitepackages()
# functions, through the main() function when Python starts.
USER_SITE = None
USER_BASE = None

......

默认 USER_SITE 和 USER_BASE 均为 None

修改为自定义的路径

USER_SITE = '/Users/ghost7/miniforge3/envs/math/lib/python3.8/site-packages' USER_BASE = '/Users/ghost7/miniforge3/envs/math/bin'

Miniforge 的卸载

Miniforge 对你的 shell rc 文件所做的任何修改

1
2
3
4
# Use this first command to see what rc files will be updated
conda init --reverse --dry-run
# Use this next command to take action on the rc files listed above
conda init --reverse

删除安装 Miniforge 基本环境的文件夹和所有子文件夹

1
2
3
4
5
6
CONDA_BASE_ENVIRONMENT=$(conda info --base)
echo The next command will delete all files in ${CONDA_BASE_ENVIRONMENT}
# Warning, the rm command below is irreversible!
# check the output of the echo command above
# To make sure you are deleting the correct directory
rm -rf ${CONDA_BASE_ENVIRONMENT}

移除任何留下的全局 conda 配置文件

1
2
echo ${HOME}/.condarc will be removed if it exists
rm -f "${HOME}/.condarc"

安装 Anaconda

Arm Mac

官网:https://www.anaconda.com/

下载名为 64-Bit (M1) Graphical Installer (316 MB) 的安装包

安装即可

Ubuntu

下载安装包

1
 wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh

运行安装脚本

1
sudo sh ./Anaconda3-2022.05-Linux-x86_64.sh

按照提示安装即可

updatedupdated2023-03-182023-03-18