It seems this is impossible with the built-in %run magic function. Your question led me down a rabbit hole, though, and I wanted to see how easy it would be to do something similar. At the end, it seems somewhat pointless to go to all this effort to create another magic function that just uses execfile(). Maybe this will be of some use to someone, somewhere.
# custom_magics.py
from IPython.core.magic import register_line_magic, magics_class, line_magic, Magics
@magics_class
class StatefulMagics(Magics):
def __init__(self, shell, data):
super(StatefulMagics, self).__init__(shell)
self.namespace = data
@line_magic
def my_run(self, line):
if line[0] != "%":
return "Not a variable in namespace"
else:
filename = self.namespace[line[1:]].split('.')[0]
filename += ".py"
execfile(filename)
return line
class Macro(object):
def __init__(self, name, value):
self.name = name
self._value = value
ip = get_ipython()
magics = StatefulMagics(ip, {name: value})
ip.register_magics(magics)
def value(self):
return self._value
def __repr__(self):
return self.name
Using this pair of classes, (and given a python script tester.py) it's possible to create and use a "macro" variable with the newly created "my_run" magic function like so:
In [1]: from custom_magics import Macro
In [2]: Macro("somename", "tester.py")
Out[2]: somename
In [3]: %my_run %somename
I'm the test file and I'm running!
Out[3]: u'%somename'
Yes, this is a huge and probably wasteful hack. In that vein, I wonder if there's a way to have the name bound to the Macro object be used as the macro's actual name. Will look into that.
If by 'variable' you mean a variable available in the user namespace, then I'd suggest to use get_ipython().ev(varname) (docs) to get your variable in your function:
from __future__ import print_function
import argparse
import shlex
from IPython import get_ipython
from IPython.core.magic import register_cell_magic
@register_cell_magic
def magical(line, cell):
parser = argparse.ArgumentParser()
parser.add_argument("varname")
# using shlex to split the line in a sys.argv-like format
args = parser.parse_args(shlex.split(line))
ipython = get_ipython()
v = ipython.ev(args.varname)
# do whatever you want here
print(v)
This template above served me well - I used it to make a jinja2 cell magic that also prints to file whenever the cell is run.