(數據科學學習手札81)conda+jupyter玩轉數據科學環境搭建

本文示例yaml文件已上傳至個人Github倉庫https://github.com/CNFeffery/DataScienceStudyNoteshtml

1 簡介

  咱們在使用Python進行數據分析時,不少時候都在解決環境搭建的問題,不一樣版本、依賴包等問題常常給數據科學工做流的搭建和運轉帶來各類各樣使人頭疼的問題,本文就將基於筆者本身摸索出的經驗,以geopandas環境的搭建爲例,教你使用conda+jupyter輕鬆搞定環境的搭建、管理與拓展。node

圖1

2 虛擬環境的搭建與使用

2.1 使用conda建立虛擬環境

  以Windows操做系統爲例,由於全程主要使用命令行,因此其餘系統方法相似,有少量語句有差別的地方遇到問題時能夠自行查找解決。首先咱們要解決的是環境的建立,第一步須要安裝conda服務,這裏咱們有Anacondaminiconda兩種方式,本文選擇miniconda體積小巧,不會像Anaconda那樣自帶數量衆多的科學計算相關包而顯得臃腫。python

  有條件上外網的讀者朋友能夠在官網( https://docs.conda.io/en/latest/miniconda.html )下載與你的操做系統對應的安裝包,也能夠在清華大學鏡像站-獲取下載連接-應用軟件-Condahttps://mirrors.tuna.tsinghua.edu.cn/ )中下載對應的最新的安裝包:git

圖2

  本文選擇的是從官網下載的最新版本4.8.2,由於miniconda自帶Python,以後全部新環境的建立咱們均可以經過conda來實施,因此建議你在安裝以前系統中不要保有其餘Python環境。下載完成以後直接打開安裝,一路能夠按照默認的選項繼續,到圖3顯示的步驟時爲了方便以後的使用建議都勾選上:github

圖3

  完成安裝後咱們進入控制檯輸入conda --version檢查是否成功安裝:sql

C:\Users\hp>conda --version
conda 4.8.2

  輸入conda env list查看當前存在的全部環境:shell

C:\Users\hp>conda env list
# conda environments:
#
base                  *  C:\Conda

  能夠看到咱們當前只有1個環境base,即miniconda自帶的Python,由於圖3中咱們勾選了Register Miniconda3 as the system Python 3.7,因此在控制檯中直接輸入python能夠獲得下列結果:markdown

C:\Users\hp>python
Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32


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

Type "help", "copyright", "credits" or "license" for more information.
>>> quit() # 退出

C:\Users\hp>

  控制檯輸入conda list能夠看到當前僅有的base環境中僅有下列包:ui

C:\Users\hp>conda list
# packages in environment at C:\Conda:
#
# Name                    Version                   Build  Channel
asn1crypto                1.3.0                    py37_0    defaults
ca-certificates           2020.1.1                      0    defaults
certifi                   2019.11.28               py37_0    defaults
cffi                      1.14.0           py37h7a1dbc1_0    defaults
chardet                   3.0.4                 py37_1003    defaults
conda                     4.8.2                    py37_0    defaults
conda-package-handling    1.6.0            py37h62dcd97_0    defaults
console_shortcut          0.1.1                         4    defaults
cryptography              2.8              py37h7a1dbc1_0    defaults
idna                      2.8                      py37_0    defaults
menuinst                  1.4.16           py37he774522_0    defaults
openssl                   1.1.1d               he774522_4    defaults
pip                       20.0.2                   py37_1    defaults
powershell_shortcut       0.0.1                         3    defaults
pycosat                   0.6.3            py37he774522_0    defaults
pycparser                 2.19                     py37_0    defaults
pyopenssl                 19.1.0                   py37_0    defaults
pysocks                   1.7.1                    py37_0    defaults
python                    3.7.6                h60c2a47_2    defaults
pywin32                   227              py37he774522_1    defaults
requests                  2.22.0                   py37_1    defaults
ruamel_yaml               0.15.87          py37he774522_0    defaults
setuptools                45.2.0                   py37_0    defaults
six                       1.14.0                   py37_0    defaults
sqlite                    3.31.1               he774522_0    defaults
tqdm                      4.42.1                     py_0    defaults
urllib3                   1.25.8                   py37_0    defaults
vc                        14.1                 h0510ff6_4    defaults
vs2015_runtime            14.16.27012          hf0eaf9b_1    defaults
wheel                     0.34.2                   py37_0    defaults
win_inet_pton             1.1.0                    py37_0    defaults
wincertstore              0.2                      py37_0    defaults
yaml                      0.1.7                hc54c509_2    defaults

  接下來咱們開始來搭建本文用於舉例說明的geopandas環境,使用conda create -n 環境名稱 python=版本來建立新的環境,譬如這裏咱們建立名爲python_spatial的虛擬環境,Python版本選擇3.7this

C:\Users\hp>conda create -n python_spatial python=3.7

  遇到Proceed ([y]/n)?輸入y繼續,等相關資源下載並安裝配置完成後,再次查看當前存在的全部環境:

C:\Users\hp>conda env list
# conda environments:
#
base                  *  C:\Conda
python_spatial           C:\Conda\envs\python_spatial

  能夠看到與以前相比多了咱們剛剛建立好的python_spatial環境,使用conda activate 環境名稱來激活指定的環境:

C:\Users\hp>conda activate python_spatial

(python_spatial) C:\Users\hp>

  能夠發現這時命令行開頭多了(python_spatial),這表明咱們已經進入激活的python_spatial環境中,接着咱們就可使用conda命令在當前環境中安裝geopandas,按照官網的推薦方式從conda-forge對應的channel進行安裝,執行conda install --channel conda-forge geopandas,遇到須要選擇的地方同樣地輸入y,這裏依賴包較多,須要等待較長時間,直到最後done出現表示安裝成功,在控制檯中直接進入python,檢查geopandas是否正確安裝:

(python_spatial) C:\Users\hp>python
Python 3.7.7 (default, Mar 23 2020, 23:19:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> import geopandas as gpd
>>>

  至此,咱們已經完成了geopandas基礎環境的搭建,接下來咱們來配置使用jupyter

2.2 配置jupyter lab

  相似condajupyter也分爲jupyter notebookjupyter lab,二者核心功能都差很少,但jupyter lab擁有更多的拓展功能,而且界面和操做方式也更加炫酷方便,因此本文選擇jupyter lab,在上一節中建立好的python_spatial環境下使用conda install jupyterlab安裝基礎部分,安裝結束以後,在python_spatial環境下能夠經過執行jupyter lab來打開它,在此以前須要先爲jupyter lab配置虛擬環境,不然只能識別到默認的base環境。

  • 安裝ipykernel

    退出虛擬環境後執行conda install ipykernel

  • 爲虛擬環境安裝ipykernel

    執行conda install -n python_spatial ipykernel

  • 激活虛擬環境&將虛擬環境寫入jupyter的kernel中

C:\Users\hp>conda activate python_spatial
  
(python_spatial) C:\Users\hp>python -m ipykernel install --user --name python_spatial --display-name
   "spatial"
Installed kernelspec python_spatial in C:\Users\hp\AppData\Roaming\jupyter\kernels\python_spatial
  
(python_spatial) C:\Users\hp>

  這時咱們在jupyter lab中已經能夠切換到python_spatial環境了,接下來爲了使用jupyter lab的插件拓展,須要安裝nodejs,咱們在python_spatial下執行conda install nodejs便可,完成安裝以後根據本身對插件功能的須要能夠分別安裝不一樣的插件,下面舉幾個經常使用的例子:

  • html交互部件插件

    爲了在jupyter lab中渲染一些html部件,譬如tqdm中的交互式進度條,在虛擬環境下執行下列命令:

pip install ipywidgets
jupyter labextension install @jupyter-widgets/jupyterlab-manager

  完成後執行jupyter lab,在打開的操做界面中notebook下點擊python_spatial建立新的notebook,執行以下命令(提早安裝好tqdm),能夠看到出現了交互式的進度條:

圖4
  • 目錄插件

    ipynb文件中能夠用markdown編寫各級別標題,在使用下列插件自動生成目錄:

jupyter labextension install @jupyterlab/toc
圖5
  • matplotlib交互式繪圖

    使用matplotlib交互式繪圖模式:

pip install ipympl
jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-matplotlib

  安裝完成後就可使用%matplotlib widget開啓交互式繪圖模式(請提早安裝好geopandas繪圖依賴包descartes):

圖6

你也能夠在側邊欄中發現更多的實用插件:

圖7

2.3 虛擬環境的備份和恢復

  conda提供了將虛擬環境導出爲yaml文件的功能,使得咱們能夠保留好不容易建立好的虛擬環境中的配置信息,格式如conda env export > 導出路徑\文件名.yml,譬如咱們導出前面建立好的python_spatial到所需路徑下:

(python_spatial) C:\Users\hp>conda env export > C:\Users\hp\Desktop\python_spatial.yml

(python_spatial) C:\Users\hp>

  以後你能夠在安裝好conda服務的其餘機器上按照conda env create -n 新環境名稱 -f=路徑\文件名.yml,譬如咱們就在本機上用已經導出的python_spatial.yml複製爲新的虛擬環境,耐心等待以後conda會自動完成前面全部咱們手動實現的步驟:

conda create -n new_python_spatial -f=C:\Users\hp\Desktop\python_spatial.yml

  以後只須要像前文中同樣執行python -m ipykernel install --user --name new_python_spatial --display-name "new spatial"從而爲jupyter lab添加新的虛擬環境的kernel信息,在new_python_spatial環境下啓動jupyter lab,這是咱們可以使用的環境變成了3個:

圖8

2.4 虛擬環境的移除

  使用conda remove -n 環境名稱 --all 來移除已經建立的環境,譬如咱們使用conda remove -n new_python_spatial -allnew_python_spatial移除以後,再次查看全部環境:

C:\Users\hp>conda env list
# conda environments:
#
base                  *  C:\Conda
python_spatial           C:\Conda\envs\python_spatial

  但這時會存在一個惱人的地方,咱們這裏只是移除了虛擬環境,但前面註冊到jupyter lab中的kernel還會顯示,但其實是沒有對應環境存在的,因此強行選擇已經移除的環境對應的kernel會報錯:

圖9

  控制檯中使用jupyter kernelspec list查看信息:

C:\Users\hp>jupyter kernelspec list
Available kernels:
  new_python_spatial    C:\Users\hp\AppData\Roaming\jupyter\kernels\new_python_spatial
  python_spatial        C:\Users\hp\AppData\Roaming\jupyter\kernels\python_spatial
  python3               C:\Conda\share\jupyter\kernels\python3

  接着使用jupyter kernelspec remove kernel名稱對其進行移除便可:

C:\Users\hp>jupyter kernelspec remove new_python_spatial
Kernel specs to remove:
  new_python_spatial    C:\Users\hp\AppData\Roaming\jupyter\kernels\new_python_spatial
Remove 1 kernel specs [y/N]: y
[RemoveKernelSpec] Removed C:\Users\hp\AppData\Roaming\jupyter\kernels\new_python_spatial

  以後在啓動jupyter lab就會發現殘餘的kernel跟着消失了。

  以上就是本文的所有內容,對應的yaml文件已上傳至文章開頭的Github倉庫中,你能夠直接基於它建立對應本文python_spatial的虛擬環境。

相關文章
相關標籤/搜索