0
点赞
收藏
分享

微信扫一扫

Python 编程辅助工具--ipython

Mhhao 2022-03-18 阅读 143

ipython

使用IPython交互式编程

更好的编辑器
语法高亮
自动缩进
Tab补全
快速获取帮助信息
搜索历史
执行shell命令


1.安装基于python3的ipython

[root@localhost ~]# pip3 install ipython
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting ipython
………………省略部分输出信息…………
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

2.打开ipython

[root@localhost ~]# ipython
Python 3.8.10 (default, Mar  2 2022, 16:52:41) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: 

Magic函数

魔法函数分为两种,一种是 line magics,另外一种cell magics
Line magic 是通过在前面加 %,表示magic只在本行有效
Cell magic 是通过在前面加 %%,表示 magic 在整个cell单元有效

In[1]: %lsmagic 或者 In[1]: %<tab> 获取所有的magic函数

[root@localhost ~]# ipython
Python 3.8.10 (default, Mar  2 2022, 16:52:41) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %ls
anaconda-ks.cfg  initial-setup-ks.cfg  Python-3.8.6.tgz  下载/  文档/  视频/
dead.letter      PycharmProjects/      requirements.txt  公共/  桌面/  音乐/
Python-3.8.6/         test/             图片/  模板/

In [2]:  !cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

jupyter

由于jupyter 丰富的可视化输出,其广泛应用于以下场景:
编程教学
数据分析
科学计算
幻灯片演示

1.安装jupyter notebook

[root@localhost ~]# pip3 install jupyter
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting jupyter
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/83/df/0f5dd132200728a86190397e1ea87cd76244e42d39ec5e88efd25b2abd7e/jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB)
Collecting nbconvert
………………省略部分输出信息…………
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

2.运行jupyter notebook
注意:不建议root账户运行

[root@localhost ~]# su - user1
[user1@localhost ~]$ jupyter notebook --no-browser --ip=0.0.0.0
[I 15:45:25.104 NotebookApp] Writing notebook server cookie secret to /home/djp/.local/share/jupyter/runtime/notebook_cookie_secret
[I 15:45:25.356 NotebookApp] Serving notebooks from local directory: /home/user1
[I 15:45:25.356 NotebookApp] Jupyter Notebook 6.4.10 is running at:
[I 15:45:25.356 NotebookApp] http://dingjianpeng:8888/?token=babc2e7f848733dbe5bcf0e42d2157c1c58279bac3da06d7
[I 15:45:25.356 NotebookApp]  or http://127.0.0.1:8888/?token=babc2e7f848733dbe5bcf0e42d2157c1c58279bac3da06d7
[I 15:45:25.356 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 15:45:25.361 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///home/user1/.local/share/jupyter/runtime/nbserver-96669-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=babc2e7f848733dbe5bcf0e42d2157c1c58279bac3da06d7
     or http://127.0.0.1:8888/?token=babc2e7f848733dbe5bcf0e42d2157c1c58279bac3da06d7

3.浏览器输入http://localhost:8888


pdb

pdb 是 Python 自带的一个库,为 Python 程序提供了一种交互式的源代码调试功能
包含了现代调试器应有的功能,包括设置断点、单步调试、查看源码、查看程序堆钱等


ipdb

ipdb是一个开源的Python调试器,它和 pdb 有相同的接口。但是,相对于 pdb 它具有语法高亮、tab 补全、更友好的堆栈信息等高级功能

pdb是标准库,ipdb是第三方库,需要安装

[root@localhost ~]# pip3 install ipdb

pyenv-virtualenv

pyenv与virtualenv的区别
pyenv 用以管理不同的 Python 版本,例如,你的系统工作时使用 Python 2.7.13 ,学习时使用 Python 3.6.0 。
virtualenv 用以隔离项目的工作环境,例如,项目 A 和项目 B 都是使用 Python 2.7.13 ,但是 ,项目 A 需要使用 Flask0.8 版本,项目 B 需要使用 Flask 0.9 版本


1.安装pyenv-virtualenv

[root@localhost ~]# git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
Cloning into '/root/.pyenv/plugins/pyenv-virtualenv'...
remote: Enumerating objects: 2131, done.
remote: Counting objects: 100% (64/64), done.
remote: Compressing objects: 100% (54/54), done.
remote: Total 2131 (delta 28), reused 17 (delta 8), pack-reused 2067
Receiving objects: 100% (2131/2131), 611.10 KiB | 560.00 KiB/s, done.
Resolving deltas: 100% (1444/1444), done.

2.编辑配置文件(~/.bashrc)

[root@localhost ~]# vim ~/.bashrc 
 18 eval "$(pyenv virtualenv-init -)"

3.使用pyenv-virtualenv

新建工作环境

[root@localhost ~]# pyenv virtualenv 3.8.6 localhost1
Looking in links: /tmp/tmps4j5qyju
Requirement already satisfied: setuptools in /root/.pyenv/versions/3.8.6/envs/localhost1/lib/python3.8/site-packages (49.2.1)
Requirement already satisfied: pip in /root/.pyenv/versions/3.8.6/envs/localhost1/lib/python3.8/site-packages (20.2.1)

查看工作环境

[root@localhost ~]# pyenv virtualenvs
  3.8.6/envs/localhost1 (created from /root/.pyenv/versions/3.8.6)
  localhost1 (created from /root/.pyenv/versions/3.8.6)

在不同的工作环境安装不同的 Flask 版本

[root@localhost ~]# pyenv activate localhost1
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(localhost1) [root@localhost ~]# pip install flask==1.1.1
#退出环境
(localhost1) [root@localhost ~]# pyenv deactivate 

删除虚拟环境

[root@localhost ~]# pyenv virtualenv-delete localhost1
pyenv-virtualenv: remove /root/.pyenv/versions/3.8.6/envs/localhost1? (y/N) y
举报

相关推荐

0 条评论