New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"run -i" no longer works after %reset in terminal #693

Closed
rabbitmcrabbit opened this Issue Aug 11, 2011 · 4 comments

Comments

Projects
None yet
4 participants
@rabbitmcrabbit
Contributor

rabbitmcrabbit commented Aug 11, 2011

this appears to be a result of __name__ being lost:

/usr/local/lib/python2.6/dist-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s, next_input)
   1892                 self._magic_locals = sys._getframe(1).f_locals
   1893             with self.builtin_trap:
-> 1894                 result = fn(magic_args)
   1895             # Ensure we're not keeping object references around:

   1896             self._magic_locals = {}

/usr/local/lib/python2.6/dist-packages/IPython/core/magic.pyc in magic_run(self, parameter_s, runner, file_finder)
   1587             # Run in user's interactive namespace

   1588             prog_ns = self.shell.user_ns
-> 1589             __name__save = self.shell.user_ns['__name__']
   1590             prog_ns['__name__'] = '__main__'
   1591             main_mod = self.shell.new_main_mod(prog_ns)

KeyError: '__name__'


In [1]: dir()
Out[1]: 
['In',
 'Out',
 '_',
 '__',
 '___',
 '__builtin__',
 '__builtins__',
 '__name__',
 '_dh',
 '_i',
 '_i1',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 '_sh',
 'exit',
 'get_ipython',
 'help',
 'quit']

In [2]: __name__
Out[2]: '__main__'

In [3]: %reset
Once deleted, variables cannot be recovered. Proceed (y/[n])?  y

In [4]: dir()
Out[4]: 
['In',
 'Out',
 '__builtin__',
 '__builtins__',
 '_dh',
 '_i',
 '_i4',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 '_sh',
 'exit',
 'get_ipython',
 'help',
 'quit']

In [5]: __name__
Out[5]: '__builtin__'
@rabbitmcrabbit

This comment has been minimized.

Show comment
Hide comment
@rabbitmcrabbit

rabbitmcrabbit Aug 11, 2011

Contributor

oops -- github markup has chewed up the variable names here. they should be

__name__
Contributor

rabbitmcrabbit commented Aug 11, 2011

oops -- github markup has chewed up the variable names here. they should be

__name__
@rabbitmcrabbit

This comment has been minimized.

Show comment
Hide comment
@rabbitmcrabbit

rabbitmcrabbit Aug 11, 2011

Contributor

nope, i'm still not getting the hang of this yet...
__name__ is the problem variable above.

Contributor

rabbitmcrabbit commented Aug 11, 2011

nope, i'm still not getting the hang of this yet...
__name__ is the problem variable above.

@minrk

This comment has been minimized.

Show comment
Hide comment
@minrk

minrk Aug 12, 2011

Member

Thanks for the catch - reset is definitely being too aggressive here.

When doing a big code paste, you can just wrap it in a <pre> tag, so it won't interpret anything as formatting.

Member

minrk commented Aug 12, 2011

Thanks for the catch - reset is definitely being too aggressive here.

When doing a big code paste, you can just wrap it in a <pre> tag, so it won't interpret anything as formatting.

@takluyver

This comment has been minimized.

Show comment
Hide comment
@takluyver

takluyver Oct 19, 2011

Member

I think this should be fixed in my big Usermod PR (#648): https://github.com/ipython/ipython/pull/648/files#L2R1103

Member

takluyver commented Oct 19, 2011

I think this should be fixed in my big Usermod PR (#648): https://github.com/ipython/ipython/pull/648/files#L2R1103

@fperez fperez closed this in a1e4911 Nov 27, 2011

stefanv pushed a commit to stefanv/ipython that referenced this issue Nov 30, 2011

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014

Merge pull request #648 from takluyver/usermod
Clean up handling of global namespaces with the proper semantics.

A global namespace should always be tied to a module: pickle accesses classes via the module in which they're defined. So I've changed the arguments for instantiating an InteractiveShell to include `user_module` in place of `user_global_ns`. The global namespace simply becomes a reference to `user_module.__dict__`.

For instantiating InteractiveShell, there are four possibilities:

* Neither `user_ns` nor `user_module` is given. A new (real) module is created named `__main__`, and its `__dict__` becomes the global and local namespace. This is what happens when starting IPython normally.
* Only `user_module` is given. Its `__dict__` becomes the global and local namespace.
* Both `user_ns` and `user_module` are given. `user_module.__dict__` is the global namespace, and `user_ns` is the local namespace. Note that we can't interactively define closures over variables in the local namespace (this seems to be a limitation of Python).
* Only `user_ns` is given. It is treated as the global and local namespace, and a `DummyMod` object is created to refer to it. This is intended as a convenience, especially for the test suite. The recommended way to pass in a single global namespace is as a reference to the module.

`embed()` digs out the locals and the module from the frame in which it's called.

Closes gh-29, closes gh-693.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment