Jupyter 简明教程

Setting IPython as Default Python Environment

不同的环境变量会影响 Python 的行为。PYTHONSTARTUP 环境变量被分配给一个 Python 脚本。实际上,该脚本将在 Python 提示符出现之前执行。如果在每次新的 Python 会话启动时都要默认加载某些模块,此方法就很有用。

Different environment variables influence Python’s behaviour. PYTHONSTARTUP environment variable is assigned to a Python script. As an effect, this script gets executed before Python prompt appears. This is useful if certain modules are to be loaded by default every time a new Python session starts.

以下脚本 (start.py) 导入 IPython 模块,并执行 start_ipython() 函数,以便在调用 Python 可执行文件时用 IPython shell 替换默认 Python shell with prompt (>>>)

The following script (start.py) imports IPython module and executes start_ipython() function to replace default Python shell with prompt (>>>) by IPython shell when Python executable is invoked.

import os, IPython
os.environ['PYTHONSTARTUP'] = ''
IPython.start_ipython()
raise SystemExit

假设此文件存储在 Python 的安装目录 (c:\python36) 中,请设置 PYTHONSTARTUP 环境变量并从命令行启动 Python。然后,IPython shell 就会像下面所示出现 −

Assuming that this file is stored in Python’s installation directory (c:\python36), set PYTHONSTARTUP environment variable and start Python from command line. Then IPython shell appears as shown below −

python installation directory

请注意,可以在 Windows 中使用系统属性对话框,在 Linux 上使用 export 命令永久设置环境变量。

Note that the environment variable can be permanently set using System Properties dialog in Windows and using export command on Linux.