本文示例yaml文件已上传至个人
Github
仓库https://github.com/CNFeffery/DataScienceStudyNoteshtml
咱们在使用Python
进行数据分析时,不少时候都在解决环境搭建的问题,不一样版本、依赖包等问题常常给数据科学工做流的搭建和运转带来各类各样使人头疼的问题,本文就将基于笔者本身摸索出的经验,以geopandas
环境的搭建为例,教你使用conda
+jupyter
轻松搞定环境的搭建、管理与拓展。node
以Windows
操做系统为例,由于全程主要使用命令行,因此其余系统方法相似,有少量语句有差别的地方遇到问题时能够自行查找解决。首先咱们要解决的是环境的建立,第一步须要安装conda
服务,这里咱们有Anaconda
和miniconda
两种方式,本文选择miniconda
体积小巧,不会像Anaconda
那样自带数量众多的科学计算相关包而显得臃肿。python
有条件上外网的读者朋友能够在官网( https://docs.conda.io/en/latest/miniconda.html )下载与你的操做系统对应的安装包,也能够在清华大学镜像站-获取下载连接-应用软件-Conda( https://mirrors.tuna.tsinghua.edu.cn/ )中下载对应的最新的安装包:git
本文选择的是从官网下载的最新版本4.8.2
,由于miniconda
自带Python
,以后全部新环境的建立咱们均可以经过conda
来实施,因此建议你在安装以前系统中不要保有其余Python
环境。下载完成以后直接打开安装,一路能够按照默认的选项继续,到图3显示的步骤时为了方便以后的使用建议都勾选上:github
完成安装后咱们进入控制台输入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.7
:this
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
。
相似conda
,jupyter
也分为jupyter notebook
和jupyter 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
),能够看到出现了交互式的进度条:
目录插件
在ipynb
文件中能够用markdown
编写各级别标题,在使用下列插件自动生成目录:
jupyter labextension install @jupyterlab/toc
matplotlib交互式绘图
使用matplotlib
交互式绘图模式:
pip install ipympl jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-matplotlib
安装完成后就可使用%matplotlib widget
开启交互式绘图模式(请提早安装好geopandas
绘图依赖包descartes
):
你也能够在侧边栏中发现更多的实用插件:
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个:
使用conda remove -n 环境名称 --all
来移除已经建立的环境,譬如咱们使用conda remove -n new_python_spatial -all
将new_python_spatial
移除以后,再次查看全部环境:
C:\Users\hp>conda env list # conda environments: # base * C:\Conda python_spatial C:\Conda\envs\python_spatial
但这时会存在一个恼人的地方,咱们这里只是移除了虚拟环境,但前面注册到jupyter lab
中的kernel还会显示,但其实是没有对应环境存在的,因此强行选择已经移除的环境对应的kernel会报错:
控制台中使用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
的虚拟环境。