獲取conda的最快方法是安裝Anaconda的迷你版本Miniconda,僅包含conda自身及其依賴項。如果你希望同時擁有超過7500個開源的軟體包,那麼請安裝Anaconda。官方建議安裝conda到本地用戶,而不是管理員。
系統要求:
以macOS安裝Miniconda為例,下載最新安裝包Miniconda3-latest-MacOSX-x86_64.sh,然後執行如下命令:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.shbash Miniconda3-latest-MacOSX-x86_64.sh安裝過程中會提示「Do you wish the installer to initialize Miniconda3 by running conda init?」,建議「yes」,不然就需要自己執行
source <path to conda>/bin/activateconda init如果在安裝conda前你電腦上已經安裝過Python了也是OK的,不用卸載。不過建議conda安裝過程中設置不要自動設置環境變量,以免對之前已存在的環境造成汙染。
安裝完成、配置好環境變量後呢就可以使用conda命令了:
mybook@h2r ~% conda --helpusage: conda [-h] [-V] command ...rm -rf <path-to>/minicondarm -rf ~/.condarc ~/.conda ~/.continuumconda的channel跟之前模塊管理工具pip的源是一回事,這裡舉例設置清華源(由於合規性問題,很多國內源都已經不能用了😢),我們可以通過修改~/.condarc 文件來配置:
mybook@h2r ~% cat ~/.condarc channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ - defaultsshow_channel_urls: true或者通過命令設置:
conda config --add channels https:conda config --add channels https:當伺服器或者本地同時存在基於不同版本Python的多個項目,抑或是Python版本一致,但是項目的依賴包的版本不一致的時候,環境管理就非常有必要了。
環境管理使得項目運行在各自的虛擬環境中,相互獨立,彼此不幹擾,沒有衝突。
我們來學習一下如何通過conda進行環境管理。
執行命令conda envs list 或者 conda info --envs
mybook@h2r ~ %conda env list輸出如下:
py3 /Users/h2r/Public/anaconda/envs/py3py37 /Users/h2r/Public/anaconda/envs/py37root * /Users/h2r/Public/anaconda創建環境命令:conda create -n ENV_NAME python=PYTHON_VERSION
mybook@h2r ~ % conda create -n py39 python=3.9Fetching package metadata Solving package specifications: .
Package plan for installation in environment /Users/heruihong/Public/anaconda/envs/py39:
The following NEW packages will be INSTALLED:
ca-certificates: 2021.1.19-hecd8cb5_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main certifi: 2020.12.5-py39hecd8cb5_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main libcxx: 10.0.0-1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main libedit: 3.1.20191231-h1de35cc_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main libffi: 3.3-hb1e8313_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main ncurses: 6.2-h0a44026_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main openssl: 1.1.1i-h9ed2024_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main pip: 20.3.3-py39hecd8cb5_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main python: 3.9.1-h88f2d9e_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main readline: 8.1-h9ed2024_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main setuptools: 52.0.0-py39hecd8cb5_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main sqlite: 3.33.0-hffcf06c_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main tk: 8.6.10-hb0a8c7a_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main tzdata: 2020f-h52ac0ba_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main wheel: 0.36.2-pyhd3eb1b0_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main xz: 5.2.5-h1de35cc_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main zlib: 1.2.11-0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
Proceed ([y]/n)? y
ca-certificate 100% |############################################################################################| Time: 0:00:00 616.67 kB/stzdata-2020f-h 100% |############################################################################################| Time: 0:00:00 1.31 MB/sopenssl-1.1.1i 100% |############################################################################################| Time: 0:00:01 3.56 MB/sreadline-8.1-h 100% |############################################################################################| Time: 0:00:00 3.91 MB/spython-3.9.1-h 100% |############################################################################################| Time: 0:00:27 473.86 kB/scertifi-2020.1 100% |############################################################################################| Time: 0:00:00 2.72 MB/swheel-0.36.2-p 100% |############################################################################################| Time: 0:00:00 203.23 kB/ssetuptools-52. 100% |############################################################################################| Time: 0:00:00 2.85 MB/spip-20.3.3-py3 100% |############################################################################################| Time: 0:00:01 1.77 MB/s注意環境名字要唯一,且Python的版本要真實存在,否則都會導致創建失敗。
環境創建完成後就可以進入環境進行編碼了,激活(進入)環境使用命令:source activate ENV_NAME
mybook@h2r ~ %source activate py39(py39) mybook@h2r ~ %(py39) mybook@h2r ~ % python --versionPython 3.9.1退出虛擬環境命令:
(py39) mybook@h2r ~ % source deactivatemybook@h2r ~ %克隆環境其實就是拷貝現有環境,命令:
mybook@h2r ~ % conda create -n py39-cp Source: /Users/h2r/Public/anaconda/envs/py39Destination: /Users/h2r/Public/anaconda/envs/py39-cpPackages: 17Files: 0刪之前我們看看當前有多少虛擬環境:
mybook@h2r ~ % conda env list# conda environments:#py3 /Users/h2r/Public/anaconda/envs/py3py37 /Users/h2r/Public/anaconda/envs/py37py39 /Users/h2r/Public/anaconda/envs/py39py39-cp /Users/h2r/Public/anaconda/envs/py39-cproot * /Users/h2r/Public/anaconda下面我們來刪除環境 py39-cp ,通過如下命令:
mybook@h2r ~ % conda remove -n py39-cp --all
Package plan for package removal in environment /Users/heruihong/Public/anaconda/envs/py39-cp:
The following packages will be REMOVED:
ca-certificates: 2021.1.19-hecd8cb5_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main certifi: 2020.12.5-py39hecd8cb5_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main libcxx: 10.0.0-1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main libedit: 3.1.20191231-h1de35cc_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main libffi: 3.3-hb1e8313_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main ncurses: 6.2-h0a44026_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main openssl: 1.1.1i-h9ed2024_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main pip: 20.3.3-py39hecd8cb5_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main python: 3.9.1-h88f2d9e_2 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main readline: 8.1-h9ed2024_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main setuptools: 52.0.0-py39hecd8cb5_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main sqlite: 3.33.0-hffcf06c_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main tk: 8.6.10-hb0a8c7a_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main tzdata: 2020f-h52ac0ba_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main wheel: 0.36.2-pyhd3eb1b0_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main xz: 5.2.5-h1de35cc_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main zlib: 1.2.11-0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
Proceed ([y]/n)? yconda的包管理跟pip非常相似,我們通過對包numpy的查找、安裝、升級、卸載來演示如何使用conda進行包管理:
mybook@h2r ~ % source activate py37(py37) mybook@h2r ~ %
(py37) mybook@h2r ~ % conda search numpyFetching package metadata msgpack-numpy 0.4.1 py27hdbd2886_0 defaults 0.4.1 py36h98f623a_0 defaults. 1.19.1 py38hcfb5961_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
1.19.2 py39h3a452eb_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main (py37) mybook@h2r ~ % conda info numpy=1.19.1
(py37) mybook@h2r ~ % conda install numpy=1.19.1
(py37) mybook@h2r ~ % conda update numpy(py37) mybook@h2r ~ % conda install numpy=1.19.2
(py37) mybook@h2r ~ % conda remove numpypip和conda可以共存的😯。正如前面說到的channel國內源很多已經不能使用了,這時就可以pip,pip在conda安裝後就已經自帶了。
通過conda安裝jupyterlab,命令:conda install -c conda-forge jupyterlab
(py37) mybook@h2r ~ % conda install -c conda-forge jupyterlabjupyterlab安裝後就可以通過jupyter命令來啟動lab服務了,
命令:jupyter lab
(py37) mybook@h2r ~ % jupyter lab[I 2021-02-09 14:07:59.349 ServerApp] jupyterlab | extension was successfully linked.[I 2021-02-09 14:07:59.713 ServerApp] nbclassic | extension was successfully linked.[I 2021-02-09 14:07:59.759 LabApp] JupyterLab extension loaded from /Users/h2r/Public/anaconda/envs/py37/lib/python3.7/site-packages/jupyterlab[I 2021-02-09 14:07:59.760 LabApp] JupyterLab application directory is /Users/h2r/Public/anaconda/envs/py37/share/jupyter/lab[I 2021-02-09 14:07:59.764 ServerApp] jupyterlab | extension was successfully loaded.[I 2021-02-09 14:07:59.770 ServerApp] nbclassic | extension was successfully loaded.[I 2021-02-09 14:07:59.770 ServerApp] 啟動notebooks 在本地路徑: /Users/h2r/notebook[I 2021-02-09 14:07:59.770 ServerApp] Jupyter Server 1.3.0 is running at:[I 2021-02-09 14:07:59.770 ServerApp] http://localhost:8888/lab?token=f195b8f38784257a515fb688e437b595c25b2ed20564ab66[I 2021-02-09 14:07:59.770 ServerApp] or http://127.0.0.1:8888/lab?token=f195b8f38784257a515fb688e437b595c25b2ed20564ab66日誌提示服務已經啟動完成了,正常情況服務啟動後會自動在瀏覽器中打開lab服務的訪問地址,如果沒有自動打開那麼可以手動輸入日誌中提示的連結在瀏覽器中打開即可。
JupyterLab打開之後呢,我們就可以創建notebook了,notebook功能可不要太強,通過notebook我們可以:
通過執行 %lsmagic 看看都支持那些magic命令
[3]: %time [i for i in range(10000)][3]: CPU times: user 830 µs, sys: 505 µs, total: 1.33 msWall time: 2.13 ms通過?查詢用法,通過??查詢源碼
源碼太多了😄,就不完全展示了
以unix作業系統為例,只需要在命令前面加!即可