Ipython "source" command? #1001

Closed
antoine-levitt opened this Issue Nov 15, 2011 · 6 comments

Projects

None yet

3 participants

@antoine-levitt

If I have a file named test.py with

print a

in it, and if I do this in ipython:

a = 1
%run "test.py"

it'll complain that it doesn't know a, because the environment is different. Any way Ipython could implement a bash-like "source" command that'll treat the script just as if it was entered in the interpreter? That kind of stuff is especially useful when using ipython as a matlab-like environment for scientific computing (do some computation, and then some plots of it or something)

@takluyver
Member

It's already there: %run -i test.py.

There's a long paragraph in the docstring describing the normal behaviour before we explain the -i option, but I'm not sure if it can easily be improved.

@takluyver takluyver closed this Nov 15, 2011
@antoine-levitt

15/11/11 14:53, Thomas

It's already there: %run -i test.py.

There's a long paragraph in the docstring describing the normal
behaviour before we explain the -i option, but I'm not sure if it can
easily be improved.

Oh right, thanks! I actually didn't see the docstring, because I did

%run??

which didn't have anything useful. %run? does. Is this expected, or is
it a bug?

@takluyver
Member

You're welcome!

%run?? shows the source of the function. We then don't show the docstring
separately, because it's there in the source.This is working as expected
for me in 0.11 and in master.

@antoine-levitt

15/11/11 15:27, Thomas

You're welcome!

%run?? shows the source of the function. We then don't show the docstring
separately, because it's there in the source.This is working as expected
for me in 0.11 and in master.

I don't have the docstring in the source:

$ %run??
Type: Magic function
Base Class: <type 'instancemethod'>
String Form:<bound method TerminalInteractiveShell._pylab_magic_run of <IPython.frontend.terminal.interactiveshell.TerminalInteractiveShell object at 0x2465c50>>
Namespace: IPython internal
File: /usr/local/lib/python2.7/dist-packages/IPython/core/magic.py
Definition: %run(self, parameter_s='')
Source:
@skip_doctest
def _pylab_magic_run(self, parameter_s=''):
Magic.magic_run(self, parameter_s,
runner=mpl_runner(self.shell.safe_execfile))

$

@takluyver
Member

Ah, I didn't know we overrode magic_run for pylab. I don't think there's
anything we actually need to fix there, since %run? still works correctly.

@fperez
Member
fperez commented Nov 15, 2011

Agreed, I think it's OK to leave it closed, the behavior is completely consistent: run?? is showing the correct sources, it's just that in this case the source is a wrapper around the original. But with run? showing the true docstring, it's working as desigined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment