需要注意的是做生信分析的童鞋使用 conda 環境時一定要特別注意 conda channels 的設置,濫用 channels 很有可能會導致你的軟件升降級(甚至環境)錯亂。推薦設置如下(~/.condarc):
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- defaults
show_channels_urls: true
從 2019.04 起清華大學和中科大宣布停止 Anaconda 鏡像服務,但是出于教育科研機構使用的前提,在 2019-05-15 清華大學又宣布重新恢復了 Anaconda 鏡像!
因此原來使用國內鏡像的 conda 可以根據自身需求決定是否需要變更新的 channels:
channels:
- conda-forge
- bioconda
- main
- free
- r
- pro
- defaults
show_channels_urls: true
conda 環境下的軟件盡量使用 conda、pip 命令去安裝。但同時也產生了一個問題,即 conda 中安裝了 R,有些使用了 install.packages(),install_github,biocLite 等方式安裝的 R 包,在環境遷移的時候,這些包如何遷移?
conda 4.6.x 切換環境使用的是:
$ source activate bio-base
conda 4.7.x 后切換環境變成了:
# To activate this environment, use:
> conda activate bio-base
# To deactivate an active environment, use:
> conda deactivate
問題是,conda-4.7.x 使用推薦的命令切換環境,還要你 init 初始化一下 conda,不想 init 的話可以用回 4.6.x 的方式切換環境:
$ conda --versionconda 4.7.5$ conda activate bio-baseCommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.To initialize your shell, run $ conda init <SHELL_NAME>Currently supported shells are: - bash - fish - tcsh - xonsh - zsh - powershellSee 'conda init --help' for more information and options.IMPORTANT: You may need to close and restart your shell after running 'conda init'.$ source activate bio-base(bio-base) shenweiyan@ecs-steven 10:49:59 /home/shenweiyan$ which python/Bioinfo/Pipeline/SoftWare/Anaconda3/envs/bio-base/bin/python$ source deactivate bio-baseDeprecationWarning: 'source deactivate' is deprecated. Use 'conda deactivate'.shenweiyan@ecs-steven 11:03:40 /home/shenweiyan$ source activate bio-base(bio-base) shenweiyan@ecs-steven 11:03:50 /home/shenweiyan$ conda deactivate(bio-base) shenweiyan@ecs-steven 11:03:57 /home/shenweiyan$
使用 conda 命令安裝的包,都可以使用下面的命令導出依賴包/環境并批量恢復:
# To create a requirements.txt file
# Gives you list of packages used for the environment
conda list
# Save all the info about packages to your folder
conda list -e > requirements.txt
# To export environment file
activate <environment-name>
conda env export > <environment-name>.yml
# For other person to use the environment
conda env create -f <environment-name>.yml
# Install via `conda` directly.
# This will fail to install all dependencies. If one fails, all dependencies will fail to install.
conda install --yes --file requirements.txt
# To go around issue above, one can iterate over all lines in the requirements.txt file.
while read requirement; do conda install --yes $requirement; done < requirements.txt
R Essentials 軟件包包含 IRKernel 和 80 多種最流行的數據科學 R 軟件包,包括 dplyr,shiny,ggplot2,tidyr,caret 和 nnet 等。
Bioconductor 鏡像使用幫助:
#清華大學開源鏡像
options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
source("http://bioconductor.org/biocLite.R")
#指定一個離你最近的國內鏡像
options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")
biocLite("包名")
biocLite 包批量安裝:
> source("http://bioconductor.org/biocLite.R")
> options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor")
> data = read.table("r-biocLite.txt", header=T, check.names=F, quote="")
> head(data)
biocLite_Packages_Name
1 RSQLite
2 KEGGREST
3 png
4 Rcpp
5 digest
6 AnnotationDbi
> soft = as.vector(data[,1])
> biocLite(soft)
install.packages()所有 R 包:
> data = read.table("r-packages.txt", header=T, check.names=F, quote="")
> soft = as.vector(data[,1])
> install.packages(soft)
對于使用 conda install --yes --file requirements.txt 重裝某一個環境的所有軟件時,如果軟件中包含了 gcc,安裝了 R 后,在使用 R 時會可能會引發錯誤:
/path/to/SoftWare/Anaconda/v2/lib/R/bin/exec/R: /path/to/SoftWare/Anaconda/v2/lib/R/bin/exec/../../lib/../../libgomp.so.1: version `GOMP_4.0' not found (required by /path/to/SoftWare/Anaconda/v2/lib/R/bin/exec/../../lib/libR.so)
glibc 是 GNU 發布的 libc 庫,即 c 運行庫。glibc 是 linux 系統中最底層的 api,幾乎其它任何運行庫都會依賴于 glibc。glibc 除了封裝 linux 操作系統所提供的系統服務外,它本身也提供了許多其它一些必要功能服務的實現。由于 glibc 囊括了幾乎所有的 UNIX 通行的標準,可以想見其內容包羅萬象。而就像其他的 UNIX 系統一樣,其內含的檔案群分散于系統的樹狀目錄結構中,像一個支架一般撐起整個作業系統。在 GNU/Linux 系統中,其 C 函式庫發展史點出了 GNU/Linux 演進的幾個重要里程碑,用 glibc 作為系統的 C 函式庫,是 GNU/Linux 演進的一個重要里程碑。
有一些軟件對于 glibc 的版本會有要求,如 cnvnator-0.3.3 要求 glibc >=2.14。雖然在 anaconda 中有很多 channel 都提供了 glibc,但千萬注意一定要注意不要輕易去安裝,否則有很大的概率會導致整個環境掛掉,甚至會導致當前環境中的 conda、python 出現動態庫混亂錯誤,恢復起來相當麻煩!
我在《一次"膽戰心驚"的真實集群運維經歷》記錄了 gblic 的一些集群吐血經歷,感興趣的可以了解一下。
對于 Anaconda(conda) 軟件安裝以及依賴解決的原理,我對這個黑盒子表示一頭霧水。真實的情況是,如果在一個環境中安裝了幾百個軟件(包),再去新裝軟件,這時候 Anaconda 常常會卡在 Collecting package metadata 和 Solving environment 過程中,甚至一個晚上都沒法解決環境的依賴。
conda 官方說他們在 conda-4.7 中花了很多的精力去提升了 conda 的速度(參考官方文章:《How We Made Conda Faster in 4.7》),但從 4.6 升級到 4.7 過程很容易導致環境崩潰,修復起來極其麻煩(反正我折騰了一個晚上都沒能把我的 base 給恢復回來,吐血的經歷)!
對于新手而言,Anaconda 的確是非常簡單易用,如果對于多用戶的服務器端,或者是提供公共使用的軟件庫是否需要采用 Anaconda,個人覺得的確需要慎重考慮一下,最起碼需要考慮:
或許還有更多的問題,這里沒有列出來,如果大家有什么更好的想法或者建議,也歡迎留言交流。
學習AI,首先我們需要安裝相關開發環境。本文主要安裝Anaconda、Pytorch、Jupyter NoteBook。
Anaconda是一個開源的Python和R語言的發行版本,用于計算科學(數據科學、機器學習、大數據處理和預測分析)。
Anaconda是一個包管理器,也是一個環境管理器,個人版免費。
參考官網(https://docs.anaconda.com/anaconda/install/),選擇不同的環境安裝。
使用推薦的僅為我安裝,否則開啟Anaconda終端需要管理員權限。
安裝過程中,勾選將Anaconda作為默認的Python環境:
官方不推薦把Anaconda3加到環境變量中。
如果加到環境變量中,使用起來更方便,任意終端都可以輸入conda命令,但是可能會造成一些問題需要卸載重裝Anaconda。
推薦不加環境變量,從開始目錄選擇Anaconda終端打開:
驗證是否安裝成功,在終端中輸入conda --version后回車:
如果把Anaconda3加到環境變量中,運行python時可能會看見下面的警告:
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation.
Windows上,要解決上面的警告,需要運行c:\Anaconda3\Scripts\activate base。
這里的c:\Anaconda3\Scripts\activate代表Anaconda是安裝在c盤根目錄下的,需要換成你自己的安裝路徑。
安裝路徑最好是全英文且無空格的,可以避免很多未知的坑。
比如我是裝在d盤下:
修改源到國內鏡像:
# 配置源
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
查看當前源:
# 查看當前源
conda config --show channels
1、創建虛擬環境
conda create -n py38 python=3.8
2、查看環境列表
conda info -e
3、查看當前已安裝的包
conda list
4、安裝開源軟件包
比如安裝pytorch:
conda install pytorch
PyTorch(https://pytorch.org/) 是一個 Python 包,它提供了兩個高級特性:
需要時可以用別的Python包擴展PyTorch,比如NumPy、SciPy、Cython。
先配置鏡像,解決下載速度慢,有的包不存在的問題:
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/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
安裝:
conda install pytorch torchvision torchaudio cpuonly
如果有安裝失敗的,就單獨再安裝一次。
驗證:
python
import torch
torch.Tensor([1])
Jupyter NoteBook(https://jupyter.org/install) 把基于控制臺的方法擴展的一個交互式計算的新方向,提供一個基于web的應用,捕捉整個計算過程:開發、記錄和執行代碼,以及交流結果。
web應用:一個基于瀏覽器的工具,用于交互式編寫文檔,結合了文本、數學、計算和它們的富媒體輸出。
記事本文檔: web應用程序中所有可見內容的表示,包括計算的輸入和輸出、解釋性文本、數學、圖像和對象的富媒體表示。
conda install jupyterlab
在終端執行下面的命令,你將看到notebook打開了瀏覽器:
jupyter notebook
開啟服務后,終端會打印一些信息,比如web應用的地址(默認是http://localhost:8888):