Anaconda介紹、安裝及使用教程
前言
Python是一種面向對象的解釋型計算機程序設計語言,具有跨平臺的特點,可以在Linux、macOS以及Windows系統(tǒng)中搭建環(huán)境并使用,其編寫的代碼在不同平臺上運行時,幾乎不需要做較大的改動就能運行。
Python的應用遍及人工智能、科學計算、Web開發(fā)、系統(tǒng)運維、大數(shù)據(jù)及云計算、金融、游戲開發(fā)等,具有數(shù)量龐大且功能相對完善的標準庫和第三方庫。
一、Anaconda
1、簡介
Anaconda是大型的科學計算平臺,是Python集成開發(fā)環(huán)境,包含了大量的科學包,能夠方便快捷的對程序包進行管理和部署。
2、特點
安裝簡單
包含了數(shù)百個科學模塊,高效運用于各種項目
免費社區(qū)支持
Anaconda是跨平臺的,有Windows、macOS、Linux版本支持多種語言:
Python、R、Java等
3、包管理工具——conda
3.1 升級
升級Anaconda需要先升級conda
conda update conda
conda update anaconda
conda update anaconda-navigator # 最新版本的anaconda-navigator
3.2 卸載
Linux、Mac 刪除安裝路徑
rm -rf anaconda
3.3 基本命令
conda --version # 查看 conda 版本
conda update -n base conda # update最新版本的conda
conda create -n xxxx # 創(chuàng)建名為 xxxx 的虛擬環(huán)境
conda create -n xxxx python=3.5 # 創(chuàng)建包含 python3.5 的名為 xxxx 的虛擬環(huán)境
conda remove -n xxxx --all # 刪除名為 xxxx 的虛擬環(huán)境
conda activate xxxx # 進入 xxxx 環(huán)境
source activate xxxx
conda deactivate # 退出環(huán)境
source activate
conda env list # 顯示所有的虛擬環(huán)境
conda info --envs # 查看當前環(huán)境
conda clean -p # 刪除沒有用的包
conda clean -t # tar打包
conda clean -y -all # 刪除所有的安裝包及cache
3.4 對包的操作
conda list # 查看所有已經(jīng)安裝的包
conda list -n xxxx # 指定查看 xxxx 虛擬環(huán)境下安裝的 package
conda install -n xxxx xxx # 在指定環(huán)境中安裝 xxx 包
conda update xxx # 更新包 xxx
conda uninstall xxx # 卸載包 xxx
3.5 鏡像源
conda config --show # 查看已經(jīng)安裝過的鏡像源
conda config --remove channels url # 刪除 url 源
conda config --add channels # 添加 url 源
conda config --set show_channel_urls yes # 從channel中安裝包時顯示channel的url
修改配置文件 ~/.condarc
channels:
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main/
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/free/
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge/
- defaults
show_channel_urls: true
查看是否生效
conda info
4、pip
4.1 基本命令
pip --version # 查看版本
pip --help # 獲取幫助
pip install -U pip # 升級 pip
pip install pkg # 安裝包
pip install pkg=version # 安裝指定版本的包
pip install --upgrade pkg # 升級包
pip uninstall pkg # 卸載包
pip search pkg # 搜索包
pip show # 顯示安裝包信息
pip show -f pkg # 查看指定包的詳細信息
pip list # 列出已安裝的包
pip list -o # 查看可升級的包
4.2 升級
Linux、Mac
pip install --upgrade pip
windows
python -m pip install -U pip
4.3 鏡像源
臨時使用清華源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
永久修改
Linux、Mac:~/.pip/pip.conf
windows:C:\Users\xx\pip\pip.ini
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
# 阿里云 http://mirrors.aliyun.com/pypi/simple/
#?中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/
#?豆瓣(douban) http://pypi.douban.com/simple/
#?清華大學 https://pypi.tuna.tsinghua.edu.cn/simple/
#?中國科學技術大學 http://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host = mirrors.aliyun.com
二、安裝
官網(wǎng)地址:https://www.anaconda.com/
1、Windows
安裝時一般默認就行,在Advanced Installation Options這一步,不要勾選Add Anaconda to my PATH environment variable


2、Linux
命令行安裝
bash ~/Downloads/Anaconda3-5.0.1-MacOSX-x86_64.sh
安裝過程中,出現(xiàn)提示
Press Enter to confirm the location, Press CTRL-C to cancel the installation or specify an alternate installation directory.如果接受默認安裝路徑,則會顯示
PREFIX=/home/<user>/anaconda<2 or 3>并且繼續(xù)安裝。出現(xiàn)提示
Do you wish the installer to prepend the Anaconda install location to PATH in your /home/<user>/.bash_profile ?建議輸入
yes。
3、Mac
前面直接下一步

在這,可以點擊Change Install Location來改變安裝位置。

完成!

點擊Anaconda-Navigator的圖標,打開

啟動成功,說明安裝完成。
三、jupyter
1、notebook環(huán)境配置
1.1、Windows
打開控制面板->系統(tǒng)->高級系統(tǒng)設置->環(huán)境變量,選中系統(tǒng)變量中的path進行添加

在Anaconda安裝位置下的的Script目錄及Library\bin目錄復制進去,如

2、配置
2.1、生成配置文件
jupyter notebook --generate-config
2.2、設置密碼
jupyter notebook password
# 這步設置的密碼用于瀏覽器訪問時輸入
2.3、修改配置文件
Windows
C:\Users\Administrator\.jupyter\jupyter_notebook_config.py
Linux、Mac
vim ~/.jupyter/jupyter_notebook_config.py
# 前面的 # 去掉
c.NotebookApp.ip = '*' # 開啟所有的IP訪問,即可使用遠程訪問
c.NotebookApp.open_browser = False # 關閉啟動后的自動開啟瀏覽器
c.NotebookApp.port = 8888 # 設置端口8888,也可用其他的,比如1080,8080等等
c.NotebookApp.notebook_dir = ’‘ # 設置 notebook 默認打開的目錄
c.PAMAuthenticator.encoding = 'utf8' # 指定utf-8編碼,解決讀取中文路徑或者文件亂碼問題
2.4、啟動notebook
jupyter notebook # 啟動 notebook
jupyter lab # 啟動 lab
2.5 遠程登錄
啟動notebook之后在瀏覽器中輸入啟動是打印的網(wǎng)址或者:
localhost:port/tree # 進入 jupyter notebook
localhost:port/lab # 進入 jupyter lab
jupyter在服務器服務器,想在本地瀏覽器訪問時,需要在終端輸入命令
ssh -N -L localhost:7777:login-0-0.local:12315 dengxsh4490@10.100.2.10
注:localhost:7777指定本地瀏覽器需要輸入的地址,login-0-0.local:12315代表服務器運行jupyter時打印的輸出信息中的地址
3、jupyter kernel
3.1 擴展 R kernel
install.packages('IRkernel')
# displayname 指定jupyter中顯示的名字
IRkernel::installspec(name = 'ir32', displayname = 'R 3.2')
3.2 查看kernel
jupyter kernelspec list
3.3 卸載指定kernel
jupyter kernelspec remove kernel_name
四、jupyter 插件
1、jupyter notebook 插件
1.1、安裝并激活 jupyter_contrib_nbextensions
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
1.2、 安裝并啟用 Jupyter Nbextensions Configurator
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user
1.3、conda安裝的方式
conda install -c conda-forge jupyter_contrib_nbextensionsconda install -c
conda-forge jupyter_nbextensions_configurator
可替代1.1和1.2
完成后,在啟動 jupyter notebook后可在瀏覽器菜單欄中多了一欄

選中想要的插件即可添加
1.4、插件選擇
Variable Inspector :這是一個查看變量的插件
Table of Contents :自動生成目錄插件
Table of Contents :自動生成目錄插件
Codefolding: 代碼折疊
Autopep8: 自動代碼格式優(yōu)化
AutoSaveTime: 控制腳本的自動保存時間
Hide Input All: 隱藏所有的代碼單元,保持所有的輸出和 markdown 單元可見
Spellchecker: 對 markdown 單元中的內容進行拼寫檢查
插件很多,可根據(jù)需要添加
2、jupyter lab插件

- 點擊菜單欄
Settings下拉框中的Advanced Settings Editor選項,進入設置頁面

- 接著,點擊
Extension Manager,并且在右邊的空白框里填上{'enabled':true},并且按右上角的保存按鈕。

- 最后,你會看到Lab右邊會出現(xiàn)插件欄的按鈕,我已經(jīng)安裝過一些插件
介紹幾款好用的插件
注:安裝這些插件需要先安裝 node,并將node添加到環(huán)境變量中。安裝時請注意node版本
2.1、toc
這是一個目錄插件,安裝后就能很方便地在Lab上展示notebook或者markdown的目錄。
目錄可以滾動,并且能展示或隱藏子目錄。
https://link.zhihu.com/?target=https%3A//github.com/jupyterlab/jupyterlab-toc
2.2、LaTeX
支持在線編輯并預覽LaTeX文檔。
https://link.zhihu.com/?target=https%3A//github.com/jupyterlab/jupyterlab-latex
2.3、drawio
可以在Lab中啟用drawio繪圖工具,是一款非常棒的流程圖工具。
https://link.zhihu.com/?target=https%3A//github.com/QuantStack/jupyterlab-drawio
2.4、variableinspector
該插件可以在Lab中展示代碼中的變量及其屬性,類似RStudio中的變量檢查器。
https://link.zhihu.com/?target=https%3A//github.com/lckr/jupyterlab-variableInspector
2.5、go to Definition
該插件用于在Lab筆記本和文件編輯器中跳轉到變量或函數(shù)的定義。
https://link.zhihu.com/?target=https%3A//github.com/krassowski/jupyterlab-go-to-definition
2.6、lsp
該插件用于自動補全、參數(shù)建議、函數(shù)文檔查詢、跳轉定義等。
https://link.zhihu.com/?target=https%3A//github.com/krassowski/jupyterlab-lsp
2.7 spreadsheet
該插件用于在Lab上顯示excel表格,只讀
https://link.zhihu.com/?target=https%3A//github.com/quigleyj97/jupyterlab-spreadsheet
2.8、debugger
可用于試調
conda install xeus-python=0.8.0 -c conda-forge
jupyter labextension install @jupyterlab/debugger
https://github.com/jupyterlab/debugger
2.9、matplotlib、dash、plotly等可交互式繪圖
幫助我們在notebook界面配合matplotlib實現(xiàn)交互式的作圖,只需要在繪圖之前執(zhí)行魔法命令%matplotlib widget
matplotlib:https://github.com/matplotlib/ipympl
plotly:https://github.com/plotly/plotly.py
2.10、kite
在jupyter lab使用kite代碼補全服務
https://github.com/kiteco/jupyterlab-kite
歡迎關注我的微信公眾號
