首頁>技術>

Anaconda介紹、安裝及使用教程前言

Python是一種面向物件的解釋型計算機程式設計語言,具有跨平臺的特點,可以在Linux、macOS以及Windows系統中搭建環境並使用,其編寫的程式碼在不同平臺上執行時,幾乎不需要做較大的改動就能執行。

Python的應用遍及人工智慧、科學計算、Web開發、系統運維、大資料及雲計算、金融、遊戲開發等,具有數量龐大且功能相對完善的標準庫和第三方庫。

一、Anaconda1、簡介

Anaconda是大型的科學計算平臺,是Python整合開發環境,包含了大量的科學包,能夠方便快捷的對程式包進行管理和部署。

2、特點安裝簡單包含了數百個科學模組,高效運用於各種專案免費社群支援Anaconda 是跨平臺的,有 Windows、macOS、Linux 版本支援多種語言:Python、R、Java等3、包管理工具——conda3.1 升級

升級Anaconda需要先升級conda

conda update condaconda update anacondaconda update anaconda-navigator # 最新版本的anaconda-navigator
3.2 解除安裝

Linux、Mac 刪除安裝路徑

rm -rf anaconda
3.3 基本命令
conda --version                   # 檢視 conda 版本conda update -n base conda        # update最新版本的condaconda create -n xxxx              # 建立名為 xxxx 的虛擬環境conda create -n xxxx python=3.5   # 建立包含 python3.5 的名為 xxxx 的虛擬環境conda remove -n xxxx --all        # 刪除名為 xxxx 的虛擬環境conda activate xxxx               # 進入 xxxx 環境source activate xxxxconda deactivate                  # 退出環境source activateconda env list                    # 顯示所有的虛擬環境conda info --envs                 # 檢視當前環境conda clean -p                    # 刪除沒有用的包conda clean -t                    # tar打包conda clean -y -all               # 刪除所有的安裝包及cache
3.4 對包的操作
conda list                  # 檢視所有已經安裝的包conda list -n xxxx          # 指定檢視 xxxx 虛擬環境下安裝的 packageconda install -n xxxx xxx   # 在指定環境中安裝 xxx 包conda update xxx            # 更新包 xxxconda uninstall xxx         # 解除安裝包 xxx
3.5 映象源
conda config --show                        # 檢視已經安裝過的映象源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/  - defaultsshow_channel_urls: true

檢視是否生效

conda info
4、pip4.1 基本命令
pip --version                # 檢視版本pip --help                   # 獲取幫助pip install -U pip           # 升級 pippip 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
二、安裝

官網地址: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
安裝過程中,出現提示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> 並且繼續安裝。出現提示Do you wish the installer to prepend the Anaconda install location to PATH in your /home/<user>/.bash_profile ?建議輸入yes。3、Mac

前面直接下一步

完成!

啟動成功,說明安裝完成。

三、jupyter1、notebook環境配置1.1、Windows

開啟控制面板->系統->高階系統設定->環境變數,選中系統變數中的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  # 啟動 notebookjupyter lab       # 啟動 lab
2.5 遠端登入

啟動notebook之後在瀏覽器中輸入啟動是列印的網址或者:

localhost:port/tree   # 進入 jupyter notebooklocalhost:port/lab    # 進入 jupyter lab

jupyter在伺服器伺服器,想在本地瀏覽器訪問時,需要在終端輸入命令

ssh -N -L localhost:7777:login-0-0.local:12315 [email protected]

注:localhost:7777指定本地瀏覽器需要輸入的地址,login-0-0.local:12315代表伺服器執行jupyter時列印的輸出資訊中的地址

3、jupyter kernel3.1 擴充套件 R kernel
install.package('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_configuratorjupyter nbextensions_configurator enable --user
1.3、conda安裝的方式
conda install -c conda-forge jupyter_contrib_nbextensionsconda install -cconda-forge jupyter_nbextensions_configurator

可替代1.1和1.2

完成後,在啟動 jupyter notebook後可在瀏覽器選單欄中多了一欄

選中想要的外掛即可新增

1.4、外掛選擇

Variable Inspector :這是一個檢視變數的外掛

Table of Contents :自動生成目錄外掛

Table of Contents :自動生成目錄外掛

Codefolding: 程式碼摺疊

Autopep8: 自動程式碼格式最佳化

AutoSaveTime: 控制指令碼的自動儲存時間

Hide Input All: 隱藏所有的程式碼單元,保持所有的輸出和 markdown 單元可見

Spellchecker: 對 markdown 單元中的內容進行拼寫檢查

外掛很多,可根據需要新增

2、jupyter lab外掛點選選單欄Settings下拉框中的Advanced Settings Editor選項,進入設定頁面接著,點選Extension Manager,並且在右邊的空白框裡填上{'enabled':true},並且按右上角的儲存按鈕。最後,你會看到Lab右邊會出現外掛欄的按鈕,我已經安裝過一些外掛介紹幾款好用的外掛

注:安裝這些外掛需要先安裝 node,並將node新增到環境變數中。安裝時請注意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筆記本和檔案編輯器中跳轉到變數或函式的定義。

https://link.zhihu.com/?target=https%3A//github.com/krassowski/jupyterlab-go-to-definition

2.6、lsp

該外掛用於自動補全、引數建議、函式文件查詢、跳轉定義等。

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-forgejupyter labextension install @jupyterlab/debugger

https://github.com/jupyterlab/debugger

2.9、matplotlib、dash、plotly等可互動式繪圖

幫助我們在notebook介面配合matplotlib實現互動式的作圖,只需要在繪圖之前執行魔法命令%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

12
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • Linux系列_4:gdb偵錯程式和Make/Makefile