anaconda、pip及jupyter常见使用

编程知识 更新时间:2023-05-02 22:58:11

文章目录

  • 1. environment环境相关
  • 2. pip包相关
    • 2.1 常见用法
    • 2.2 python生成requirements.txt的两种方法
    • 2.3 修改下载源以及.condarc文件
      • 2.3.1 channel相关操作
      • 2.3.2 .condarc
  • 3. jupyter lab相关
    • 3.0 jupyter lab添加pep8格式化插件
    • 3.1 jupyter添加目录等小插件
    • 3.2 代码提示和代码格式化
    • 3.3 给jupyter添加内核
    • 3.4 jupyter取消科学计数法显示
    • 3.5 jupyter lab添加目录插件
      • 3.5.2 当前可行的方式
      • 3.5.1 旧的方式
    • 3.6 jupyter lab中显示plotly
    • 3.7 jupyter lab中使用 ipywidgets 插件
    • 3.8 ipywidgets与matplotlib显示动图
    • XX 其他jupyter lab插件
    • 3.9 jupyter配置代理
      • 3.9.1 配置
      • 3.9.2 删除配置
  • 4. 报错解决
  • 5. 参考链接

1. environment环境相关

创建新环境 -n 后跟新环境的名称
>conda create -n py36(环境名称) python=3.6(需要的python版本 一个等号即可)

 激活某个环境
>conda activate 环境名称

取消激活
>conda deactivate

删除某个环境
>conda env remove -n 环境名称 --all

查看当前conda环境列表
conda env list

查看当前channels
conda config —show channels
>conda clean -p      
//删除没有用的包 会检查哪些包没有在包缓存中被硬依赖到其他地方,并删除它们。
>conda clean -t     
//tar打包 可以将conda保存下来的tar包。

2. pip包相关

2.1 常见用法

搜素某个包可用的版本
打开anaconda-prompt
查看tensorflow各个版本:(查看会发现有一大堆TensorFlow源,但是不能随便选,选择可以用查找命令定位)
>anaconda search -t conda tensorflow

找到自己安装环境对应的最新TensorFlow后(可以在终端搜索anaconda,定位到那一行),然后查看指定包
>anaconda show <USER/PACKAGE>

查看tensorflow版本信息
>anaconda show anaconda/tensorflow

第4步会提供一个下载地址,使用下面命令就可安装1.8.0版本tensorflow
>conda install --channel https://conda.anaconda/anaconda tensorflow=1.8.0 

查看某个包的信息
>pip show numpy

更新某个包到特定版本
>pip install upgrade numpy=1.3

如果不确定有哪些版本可以安装,可以以如下方式:install一个错误的版本号,然后机会提示现有可用的版本有哪些了。

2.2 python生成requirements.txt的两种方法

参考:

  • 使用 pip freeze 获取安装的 Python 包
  • python生成requirements.txt的两种方法

方式1

适用于 单虚拟环境的情况。
这种方式,会将环境中的依赖包全都加入,如果使用的全局环境,则下载的所有包都会在里面,不管是不时当前项目依赖的.
使用 pip freeze 会输出所有在本地已安装的包(但不包括 pip、wheel、setuptools 等自带包),若需要输出内容与 pip list 一致,需使用 pip freeze -all。

pip freeze > requirements.txt

2.3 修改下载源以及.condarc文件

2.3.1 channel相关操作

查看当前channels
conda config —show channels

添加/删除channels
conda config --add channels https://mirrors.tuna.tsinghua.edu/anaconda/pkgs/free/
conda config --set show_channel_urls yes
conda config --remove channels 'https://mirrors.tuna.tsinghua.edu/anaconda/pkgs/free/

添加镜像

conda config --add channels https://mirrors.tuna.tsinghua.edu/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes

conda config --show channels

2.3.2 .condarc

2019年4月的时候,清华源不能用,后来又能用了。。。
参考:Anaconda 镜像使用帮助

  • conda命令下载使用channel,参数是 -c
  • pip命令下载使用channel,参数是-i
    • 例如,使用阿里云的镜像来pip
    pip install jupyter_contrib_nbextensions -i https://mirrors.aliyun/pypi/simple/
    

参考anaconda文档:

  • Managing channels
  • Using the .condarc conda configuration file:这里有channel的大部分管理内容

关于.condarc文件的信息查看

conda info
conda config --get channels #查看channel信息
conda config --add channels http://conda.anaconda/mutirri # 添加channel
conda config --remove channels http://conda.anaconda/mutirri # 移除channel
conda config --remove-key channels# 删除所有channels,再去调用时恢复默认源的命令

可以通过修改用户目录下的 .condarc 文件来修改channel。Windows 用户无法直接创建名为 .condarc 的文件,可先执行 conda config --set show_channel_urls yes 生成该文件之后再修改。
例如,添加上述channel后的.condarc文件类似:

3. jupyter lab相关

  • jupyterLab的github:https://github/jupyterlab/jupyterlab
  • jupyterLab的官网:https://jupyter/install

一开始以为python3.9不支持jupyter,参考Can’t run Jupyter Notebook with python 3.9 env

后来发现,直接用conda安装就可以conda install -c conda-forge jupyterlab

3.0 jupyter lab添加pep8格式化插件

参考:

  • https://github/ryantam626/jupyterlab_code_formatter

3.1 jupyter添加目录等小插件

Jupyter Notebook 扩展插件是扩展 notebook 环境基本功能的简单插件。它们用 Java 语言编写,会自动套用代码格式或者在单元格完成后发送浏览器通知。扩展插件目前仅支持 Jupyter Notebook(不支持 Jupyter Lab)

目录这些属于jupyter的插件,

  • 官方网站-jupyter-contrib-nbextensions
  • 官方网站-jupyterlab-extension
  1. 安装jupyter扩展
    pip install jupyter_contrib_nbextensions
  2. 安装 javascript和css文件
    jupyter contrib nbextension install --user
  3. 开启/禁止extension(这步不用执行,1、2执行完就可以在jupyter界面看到nbextension的界面了)
    jupyter nbextension enable <nbextension require path>
    然后就可以看到了

    就可以看到目录插件的图标了(会帮你加上序号的,你可以不自己在标题上加)

3.2 代码提示和代码格式化

参考Jupyter Notebook 代码提示自动补全&代码格式化:用到的两个插件是Hinterland(代码提示/自动补全)Autopep8(代码格式化)
还是在上面的扩展管理中勾选,回到jupyter notebook 重新创建新项目,代码提示和自动补全的设置就完成了!
点击工具栏右边的小锤子就可以实现代码格式化了!

3.3 给jupyter添加内核

参考之前另一个博客的这部分:1.4 给jupyter notebook增加kernel

3.4 jupyter取消科学计数法显示

偶尔会遇到这种反人类的显示,

如果是pandas的Dataframe中显示

参考Stack Overflow的回答:Format / Suppress Scientific Notation from Python Pandas Aggregation Results

使用:

pd.options.display.float_format = '{:.2f}'.format

如果是numpy显示

参考Stack Overflow的回答:Suppress Scientific Notation in Numpy When Creating Array From Nested List

使用:

np.set_printoptions(suppress=True)

3.5 jupyter lab添加目录插件

直接升级jupyter lab版本就行

pip install --upgrade jupyterlab

3.5.2 当前可行的方式

jupyterlab自从3.0之后,默认集成了jupyterlab-toc插件。

  • 如果你用的jupyterlab版本是低于3.0的,则可以手动安装一下。
  • jupyter labextension install @jupyterlab/toc
    
  • 然后刷新一下jupyter lab页面就可以了

参考:

  • Documents-Table Of Contents
  • Github-jupyterlab-toc

3.5.1 旧的方式

尝试过使用jupyter notebook一样的方式去添加目录,无效。

参考Stack Overflow的回答:
How can I add a table of contents to a Jupyter / JupyterLab notebook?

可知:jupyter lab有一个插件 jupyterlab-toc,这个插件在jupyter lab3.0以上的版本默认安装了,如果jupyer lab版本不够高(可以用下面这个的pip show jupyterlab来查看当前的jupyter lab版本)

参考jupyterlab-toc的github主页来进行安装:
https://github/jupyterlab/jupyterlab-toc

可知,要求jupyter lab的版本在2.0以上,直接升级就好了

# pip install  -i https://pypi.tuna.tsinghua.edu/simple jupyerlab -U
pip install jupyerlab -U

根据CSDN博客:不吹不黑,jupyter lab 3.0客观使用体验可知,这个3.0版本是2020年5月9号出来的,但是目前清华源镜像还下载不到。只能凭运气了,下载的时候可以看到版本是jupyterlab3.0.7


安装好之后,启动jupyter lab可以看到,侧边

目录就有啦

3.6 jupyter lab中显示plotly

// 输入 jupyter labextension list 查看当前jupyterlab的插件
> jupyter labextension list
JupyterLab v1.2.14
Known labextensions:
   app dir: /opt/conda/share/jupyter/lab
        @jupyterlab/toc v2.0.0  enabled  OK
        jupyterlab-datawidgets v7.1.1  enabled  OK
        jupyterlab-jupytext v1.1.1  enabled  OK
        jupyterlab_tensorboard v0.1.6  enabled  OK

// 如果没有plotly的插件,则安装上
> jupyter labextension install jupyterlab-plotly
Building jupyterlab assets (build:prod:minimize)

如果提示需要安装nodejs,则可以

conda install -c conda-forge nodejs

参考:

  • plot.ly offline mode in jupyter lab not displaying plots
  • plotly-Getting Started with Plotly in Python

3.7 jupyter lab中使用 ipywidgets 插件

根据matplotlib/ipympl:

  • 如果想要实现jupyterlab中matplotlib可以通过 ipywidgets 插件进行交互,则需要安装ipympl这个扩展
  • 注意,如果安装错了版本,可能不会提示报错,但是不会有交互效果。

如果用的是jupyter 1.X版本,则需要

conda install -c conda-forge ipympl==0.5.1
jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-matplotlib@0.7.0

如果用的是2.X版本,可以

conda install -c conda-forge nodejs
jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-matplotlib

> jupyter labextension list
// 如果在这个输出结果里没有看到
@jupyter-widgets/jupyterlab-manager v3.0.0 enabled OK (python, jupyterlab_widgets)

// 那么就需要手动安装这个插件了
jupyter labextension install @jupyter-widgets/jupyterlab-manager

参考:

  • Ipywidgets with matplotlib
  • Create interactive chart with matplotlib and ipywidgets
  • stackoverflow:How to get ipywidgets working in Jupyter Lab?
  • stackoverflow:Interactive matplotlib using ipywidgets
  • How to link ipywidgets widget with matplotlib chart to dynamically update charts?

使用过程中报错:Error displaying widget: model not found

参考:

  • “Error displaying widget: model not found” with Jupyter lab #112

3.8 ipywidgets与matplotlib显示动图

  • How to link ipywidgets widget with matplotlib chart to dynamically update charts?
  • matplotlib update plot
  • ✅ Animations with matplotlib 1: Making waves
  • ✅ Live Updating Graphs with Matplotlib Tutorial
  • Dynamically updating a bar plot in Matplotlib

XX 其他jupyter lab插件

很久没有注意过这方面的东西,但是它真的是越来越强大了!!!!

参考:

  • 15款好用到爆炸的Jupyter Lab插件
  • 3s安装JupyterLab,4步完成JupyterLab插件安装(附多款高生产力插件推荐)
> jupyter labextension install @jupyterlab/toc
> jupyter labextension install jupyterlab-plotly

https://github/QuantStack/jupyterlab-drawio

3.9 jupyter配置代理

3.9.1 配置

  • https://jupyterlab.readthedocs.io/en/stable/getting_started/installation.html
  • https://stackoverflow/questions/36629707/using-jupyter-behind-a-proxy

两个讲的方法差不多,一种是直接在命令行里设置,一种是在python环境里使用os设置,即:

# For Windows
set HTTP_PROXY=http://USER:PWD@proxypany:PORT
set HTTPS_PROXY=https://USER:PWD@proxypany:PORT

# For Linux / MacOS
export HTTP_PROXY=http://USER:PWD@proxypany:PORT
export HTTPS_PROXY=https://USER:PWD@proxypany:PORT

# 如果不幸用户名里包含邮箱地址,可以使用转义符, @的转义符是 %40

In [1]: import os
        os.environ['http_proxy'] = "http://user:passwd@host:port" 
        os.environ['https_proxy'] = "https://user:passwd@host:port" 

参考:

  • Set/Export: http_proxy With Special Characters In Password Under Unix / Linux

3.9.2 删除配置

参考: https://superuser/questions/196166/linux-command-line-to-turn-off-proxy

$ export {http,https,ftp}_proxy="http://proxy-server:port"
$ unset {http,https,ftp}_proxy

$ export {HTTP,HTTPS,FTP}_PROXY="http://proxy-server:port"
$ unset {HTTP,HTTPS,FTP}_PROXY

用命令行临时设置,或者去bashrc里永久设置(设置完记得reload)

$vi ~/.bashrc
$ source ~/.bashrc

4. 报错解决

  1. anaconda prompt报错:系统找不到指定路径 已解决
    英文搜索:
    anaconda prompt system cannot find the path specified

    根据github上的issue回答以及stackoverflow的回答,还有自我尝试

    1. 先将anaconda的bin路径添加到系统环境变量path中,类似D:\anaconda\condabin(我安装的时候没有勾选 add path,勾选的就不用了做这步了)
    2. 尝试使用 conda init 然后重启anaconda prompt,无效。
    3. 重启电脑,发现桌面上的 anaconda prompt 快捷方式图标变灰,删除,再次打开anaconda prompt,命令行界面等待一会,就正常了,解决。(自己解决的,不要把问题想得太复杂)

5. 参考链接

1.Anaconda下安装tensorflow1.15过程记录
2. conda常用命令

更多推荐

anaconda、pip及jupyter常见使用

本文发布于:2023-04-29 05:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/a1db79ac7c60d118d7990c3c69a72d59.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常见   anaconda   pip   jupyter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 110271文章数
  • 28044阅读数
  • 0评论数