Skip to main content
 首页 » 编程设计

python之ipython 中有没有办法始终在启动时运行导入像 'from __future__ import division' 吗

2025年05月04日10haluo1

原因是我有时将 shell/解释器用作计算器并且几乎总是想要真正的除法,而且我厌倦了总是输入它!

请您参考如下方法:

打开 ~/.ipython/profile_default/ipython_config.py 并搜索 c.InteractiveShellApp.exec_lines。我使用的是 IPython 1.2,它就在文件的顶部。像这样设置:

c.InteractiveShellApp.exec_lines = ["from __future__ import division", "import something", "another statement"] 

每个字符串都是您希望在 IPython 启动时执行的一行 Python 代码。保存文件,重新启动 IPython,一切就绪。

如果这个文件不存在,通过运行创建一个配置文件

ipython profile create 

在命令行中,它将自动为您生成。