5

How can I get something like Spyder's runfile function in IPython? (I'm using IPython v. 4.1.2 under Python 2.7.9.)

kjo
  • 33,683
  • 52
  • 148
  • 265
  • Perhaps you're looking for [`%run`](https://stackoverflow.com/questions/11744181/running-python-script-inside-ipython)? – cmaher May 06 '18 at 01:24
  • @cmaher: that works for me; thanks! Care to post your comment as the answer? – kjo May 06 '18 at 01:32

2 Answers2

5

Python files can be executed within the IPython environment using the magic command %run.

Example:

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

More information on how to run files outside your working directory can be found in this answer.

cmaher
  • 5,100
  • 1
  • 22
  • 34
2

FWIW, runfile is also included as a builtin function in PyCharm, another IDE. PyCharm's Python Console use IPython by default if you have it installed, and runfile works there. Just another option.

flow2k
  • 3,999
  • 40
  • 55