Anaconda介绍、安装及使用教程

前言

Python是一种面向对象的解释型计算机程序设计语言,具有跨平台的特点,可以在LinuxmacOS以及Windows系统中搭建环境并使用,其编写的代码在不同平台上运行时,几乎不需要做较大的改动就能运行。

Python的应用遍及人工智能、科学计算Web开发、系统运维、大数据及云计算、金融、游戏开发等,具有数量庞大且功能相对完善的标准库和第三方库。

一、Anaconda

1、简介

Anaconda是大型的科学计算平台,是Python集成开发环境,包含了大量的科学包,能够方便快捷的对程序包进行管理和部署。

2、特点

  • 安装简单
  • 包含了数百个科学模块,高效运用于各种项目
  • 免费社区支持
  • Anaconda 是跨平台的,有 WindowsmacOSLinux 版本
  • 支持多种语言:PythonRJava

3、包管理工具——conda

3.1 升级

升级Anaconda需要先升级conda

conda update conda
conda update anaconda
conda update anaconda-navigator # 最新版本的anaconda-navigator
3.2 卸载

LinuxMac 删除安装路径

rm -rf anaconda
3.3 基本命令
conda --version                   # 查看 conda 版本
conda update -n base conda        # update最新版本的conda
conda create -n xxxx              # 创建名为 xxxx 的虚拟环境
conda create -n xxxx python=3.5   # 创建包含 python3.5 的名为 xxxx 的虚拟环境
conda remove -n xxxx --all        # 删除名为 xxxx 的虚拟环境
conda activate xxxx               # 进入 xxxx 环境
conda deactivate                  # 退出环境
conda env list                    # 显示所有的虚拟环境

conda clean -p                    # 删除没有用的包
conda clean -t                    # tar打包
conda clean -y -all               # 删除所有的安装包及cache
3.4 对包的操作
conda list                  # 查看所有已经安装的包
conda list -n xxxx          # 指定查看 xxxx 虚拟环境下安装的 package
conda install -n xxxx xxx   # 在指定环境中安装 xxx 包
conda update xxx            # 更新包 xxx
conda 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/
  - defaults
show_channel_urls: true

查看是否生效

conda info

4、pip

4.1 基本命令
conda --version                   # 查看 conda 版本
conda update -n base conda        # update最新版本的conda
conda create -n xxxx              # 创建名为 xxxx 的虚拟环境
conda create -n xxxx python=3.5   # 创建包含 python3.5 的名为 xxxx 的虚拟环境
conda create -n aaaa --clone bbbb # 创建一个与 bbbb 一模一样的环境 aaaa
conda remove -n xxxx --all        # 删除名为 xxxx 的虚拟环境
conda activate xxxx               # 进入 xxxx 环境
source activate xxxx 
conda deactivate                  # 退出环境
source deactivate
conda env list                    # 显示所有的虚拟环境
conda info --envs                 # 查看当前环境

conda clean -p                    # 删除没有用的包
conda clean -t                    # tar打包
conda clean -y -all               # 删除所有的安装包及cache
4.2 升级

LinuxMac

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

windowsC:\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

二、Anaconda安装

官网地址:https://www.anaconda.com/

1、Windows

安装时一般默认就行,在Advanced Installation Options这一步,不要勾选Add Anaconda to my PATH environment variabled8f987cebe814ed5c1f6721e2ceaa79c.png

efa09eb9cee2315990a7e25179abe1de.png

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//anaconda<2 or 3> 并且继续安装。
  • 出现提示Do you wish the installer to prepend the Anaconda install location to PATH in your /home//.bash_profile ?
  • 建议输入yes

3、Mac

前面直接下一步

77d135499cf8f8a74125da985cf84256.png

在这,可以点击Change Install Location来改变安装位置。

d9d1be401f5f40eda7d46bc4c5d1a13a.png

7c255359c9a818dc267c55b6e745246d.png

完成!

点击Anaconda-Navigator的图标,打开

9d3b255dc2a6f2d5b296df8046015631.png

启动成功,说明安装完成。

三、jupyter

1、notebook环境配置

1.1 Windows

打开控制面板->系统->高级系统设置->环境变量,选中系统变量中的path进行添加

f72587377fe7cac7f59715d3ba35132c.png

在Anaconda安装位置下的的Script目录及Library\bin目录复制进去,如

c736d7068441e28d4f6598c1527a77f6.png

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之后在浏览器中输入启动是打印的网址或者:

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.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_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后可在浏览器菜单栏中多了一栏

d288d9ee3569043c95c07cc31c01fcf7.png

选中想要的插件即可添加

1.4 插件选择

Variable Inspector :这是一个查看变量的插件

Table of Contents :自动生成目录插件

Table of Contents :自动生成目录插件

Codefolding: 代码折叠

Autopep8: 自动代码格式优化

AutoSaveTime: 控制脚本的自动保存时间

Hide Input All: 隐藏所有的代码单元,保持所有的输出和 markdown 单元可见

Spellchecker:markdown 单元中的内容进行拼写检查

插件很多,可根据需要添加

2、jupyter lab插件

b610eebf7399134918062dc8df5e6a16.png

  • 点击菜单栏Settings下拉框中的Advanced Settings Editor选项,进入设置页面

3ce942292b215f29ae7d47400379eed9.png

  • 接着,点击Extension Manager,并且在右边的空白框里填上{'enabled':true},并且按右上角的保存按钮。

23b71c4faf25b8b78bd22b5e0703c292.png

  • 最后,你会看到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-forge
jupyter labextension install @jupyterlab/debugger
https://github.com/jupyterlab/debugger
2.9 matplotlib、dash、plotly等可交互式绘图

帮助我们在notebook界面配合matplotlib实现交互式的作图,只需要在绘图之前执行魔法命令%matplotlib widget

matplotlibhttps://github.com/matplotlib/ipympl

plotlyhttps://github.com/plotly/plotly.py

2.10 kite

jupyter lab使用kite代码补全服务

https://github.com/kiteco/jupyterlab-kite
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐