【Python】Anaconda常用操作
> Anaconda內建python環境變數配置在系統環境中;原生python環境變數配置在使用者環境中
# Anaconda 常用命令
## 基本指令
1. 檢視conda版本
```shell
conda -V
```
2. 升級conda
```shell
# 升級Anaconda前,需要先升級conda
conda update conda
conda update anaconda
```
## 虛擬環境配置
1. `base`是初始預設的基礎環境
2. 檢視虛擬環境列表命令
```shell
conda env list
conda info -e
```
3. 建立虛擬環境
```shell
# 建立虛擬環境並確定所用的python版本
conda create -n your_env_name python=x.x
# 在建立環境的同時安裝必要的包
conda create -n your_env_name numpy matplotlib python=x.x
# 在指定的虛擬環境中安裝額外的包
conda install -n your_env_name package_name
```
4. 刪除虛擬環境
```shell
conda remove -n your_env_name --all
# 刪除虛擬環境中的某個包:
conda remove --name your_env_name package_name
# 不太確定成不成功
conda uninstall -n your_env_name --all
conda env remove -n your_env_name
```
5. 啟用虛擬環境
```shell
conda activate your_env_name
# 也可以根據環境所在路徑啟用
```
6. 安裝或解除安裝包(進入虛擬環境之後)
```shell
# 安裝
conda install package_name
conda install package_name=version #指定版本號
conda install package_name -i 源名稱或連結 # 指定下載源
#解除安裝
conda uninstall package_name
```
7. 檢視當前虛擬環境下的所有安裝包
```shell
conda list # 需進入該虛擬環境
conda list -n your_env_name
```
8. 複製虛擬環境
I. 在本機上面進行環境複製
```shell
#[方案一]根據已有環境名複製生成新的環境
conda create -n new_env_name --clone old_env
#[方案二]根據已有環境路徑複製生成新的環境,其中old_env_path為舊環境的路徑
conda create -n new_env_name --clone old_env_path
```
II. 複製到其他機器上
方案一:
- 匯出環境
> 利用conda env export匯出的是個yaml格式的檔案,該檔案記錄了環境名,軟體源地址以及安裝包列表
```shell
conda env export > ~/env.yaml 或者 conda env export > environment.yaml
```
- 使用yaml配置檔案建立新環境
> 在新的機器中可直接執行上述命令,生成的環境與複製源完全一樣(包括環境名)。
```shell
conda env create -f ~/env.yaml
conda env create -f environment.yaml
# 兩者擇其一
```
方案二:
- 將原來電腦上的環境所在資料夾複製到當前機器的任意路徑下
- 在當前機器中進行如下操作:
```shell
# 完成虛擬環境的移植
conda create -n new_env_name --clone ~/path
```
## 設定國內映象
conda下載東西時,預設的映象源下載的速率很慢,修改conda的映象源能夠加快下載速度。一些設定映象的操作如下:
1. 檢視已新增的映象
```shell
conda config --get channels
```
2. 換回預設映象
```shell
conda config --remove-key channels
```
3. 新增清華映象
```shell
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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
# 在新增映象時選擇其中任意一個即可
```
4. 設定搜尋時顯示頻道地址
```shell
conda config --set show_channel_urls yes
```
5. 刪除指定映象
````shell
conda config --remove channels 地址
````
6. 確認映象新增成功
```shell
conda config --get channels
```