等同于 IPython 的 Spyder 运行文件?

Equivalent of Spyder's runfile for IPython?

如何在 IPython 中获得类似 Spyder 的 runfile 功能的东西? (我在 Python 2.7.9 下使用 IPython v. 4.1.2。)

Python 文件可以在 IPython 环境中使用魔术命令 %run.

执行

示例:

print_time.py
# #!/usr/bin/python
# 
# from datetime import datetime
# 
# print "The current time is {}".format(datetime.now().strftime("%H:%M:%S"))

In [1]: %run print_time.py
The current time is 21:37:46

有关如何 运行 工作目录外的文件的更多信息,请参见 this answer

FWIW,runfile 也作为内置函数包含在 PyCharm 中,另一个 IDE。 PyCharm 的 Python 控制台默认使用 IPython 如果你安装了它,并且 runfile 在那里工作。只是另一种选择。