IPython - 命令行选项
IPython - 命令行界面
Section titled “IPython - 命令行界面”虽然许多用户主要通过 Jupyter Notebook 或 JupyterLab 与 IPython 交互,但 ipython 命令本身提供了一个功能强大的基于终端的交互式 shell,并带有各种命令行选项。
启动 IPython 终端
Section titled “启动 IPython 终端”只需在终端中输入 ipython 即可启动增强的交互式 shell:
$ ipythonPython 3.9.7 (...)Type 'copyright', 'credits' or 'license' for more informationIPython 8.0.1 -- An enhanced Interactive Python.Type '?' for help.
In [1]:你可以直接使用 IPython 运行 Python 脚本。脚本执行完成后,IPython 会退出。
# 假设 hello.py 包含: print('Hello from script!')$ ipython hello.pyHello from script!要运行一个脚本然后进入交互式会话(对于检查脚本的状态很有用),请使用 -i 标志:
# 假设 script.py 包含: x = 100$ ipython -i script.py
In [1]: print(x)# 输出: 100
In [2]: exit使用 -c 标志执行特定的 Python 命令,然后退出:
$ ipython -c "import sys; print(sys.version)"# 输出: (打印 Python 版本信息)其他一些有用的命令行选项包括:
--no-banner:抑制 IPython 启动横幅。--no-confirm-exit:退出时跳过确认提示。--automagic / --no-automagic:启用/禁用在不加前导 ’%’ 的情况下调用 magic 命令。默认通常是开启的,但建议使用--no-automagic以提高清晰度。--matplotlib [backend]:启用 Matplotlib 集成,可选地指定后端(例如,qt5,tk)。通常更倾向于在会话中使用%matplotlibmagic 命令。--log-level:设置日志级别(例如,DEBUG,INFO,WARN,ERROR)。--profile=<name>:使用特定的配置 profile 启动 IPython。
IPython 子命令
Section titled “IPython 子命令”IPython 还提供子命令来管理其配置和组件:
ipython profile create [name]:创建新的配置 profile。ipython profile list:列出可用的 profile。ipython locate [profile <name>]:显示 IPython 目录或特定 profile 目录的位置。ipython kernel:启动一个 kernel 进程(不太常用直接使用,通常由前端管理,如 Jupyter)。ipython history trim / clear:管理命令历史数据库。
$ ipython profile create my_custom_settings# 输出: [ProfileCreate] 正在生成默认配置文件: .../.ipython/profile_my_custom_settings/ipython_config.py
$ ipython locate profile my_custom_settings# 输出: (profile 目录的路径)要获取完整的选项和子命令列表,请运行 ipython --help。