在 ipython 的意义上,您可以通过创建一个新的 iPython 配置文件来做到这一点
ipython profile create <name>
编辑 ipython_config.py 目录。在 linux 下较新安装的 ipython 中,此文件将位于 ~/.config/ipython/profile_<name> 目录中,但如果您不确定,可以找到它:
ipython profile locate <name>
对于我来说,我可以使用 bash 命令编辑相应的文件:
vim `ipython profile locate <name>`/ipython_config.py
编辑该文件中的适当变量(参见 ipython 文档)。
在更一般的意义上,您可以通过设置PYTHONSTARTUP 环境变量并将其指向包含命令的python 文件来强制python 在启动时运行任意代码。要等效于--pylab,该文件需要具有以下内容(等效于 ipython 1.1)。
# See: http://ipython.org/ipython-doc/rel-1.1.0/api/generated/IPython.core.magics.pylab.html
import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs
from pylab import *
from numpy import *
根据过去的经验,设置您的 PYTHONSTARTUP 变量可能是个坏主意。你所有的 Python 代码都必须有一个 x-session(或等效的),除非你正在设置一个不弹出绘图窗口的后端。它还会污染你运行的所有东西的全局命名空间,并且会减慢解释器的速度(尤其是 matplotlib 在我的机器上导入需要相当长的时间)。