Skip to content
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

%%time magic prevents assignment #11659

Closed
dwhswenson opened this issue Mar 22, 2019 · 13 comments

Comments

@dwhswenson
Copy link

commented Mar 22, 2019

The %%time cell magic is preventing me from binding names. I found this in notebook code on Travis (py 3.6) and reproduced it locally (py 3.7) in notebook and in ipython at the terminal. It doesn't seem to apply to all cell magics:

Python 3.7.2 | packaged by conda-forge | (default, Mar 19 2019, 20:46:22)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %%time
   ...: foo = "bar"
   ...:
   ...:
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 6.91 µs

In [2]: foo
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-f1d2d2f924e9> in <module>
----> 1 foo

NameError: name 'foo' is not defined

In [3]: %%prun
   ...: baz = "qux"
   ...:
   ...:
         3 function calls in 0.000 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 <string>:2(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

In [4]: baz
Out[4]: 'qux'

Confirmed that 7.3.0 worked fine locally.

@dwhswenson

This comment has been minimized.

Copy link
Author

commented Mar 26, 2019

Looking through the PRs merged as part of 7.4.0, my best guess is this is an unexpected consequence of #11542 -- at least, I can see how this (and perhaps #11665, if pos existed as a list before the cell ran) would be caused by changing how scope is handled in cell magics. And I don't see anything else that looks like a likely cause.

But I don't know anything about the internals of magics: @Carreau, could this be the problem?

The workaround is just to stay on 7.3.0.

@Carreau

This comment has been minimized.

Copy link
Member

commented Mar 26, 2019

You are right that this is due to #11542 , I'll see if I can come up with a fix.

@cdeil

This comment has been minimized.

Copy link

commented Apr 1, 2019

%%time is used a lot, this affects many users & notebooks.
@Carreau or anyone - is it possible to do a patch release for this soon?
(for now I'll go back to v7.3 and tell others to do the same)

@wanick

This comment has been minimized.

Copy link

commented Apr 2, 2019

"%time" and "%%time" behave differently
изображение

изображение

изображение

but run_call_magic && run_line_magic are identical
изображение

It is not clear how it ever worked before

@wanick

This comment has been minimized.

Copy link

commented Apr 2, 2019

изображение

By the way, in "%%timeit" the situation is exactly the same, but at least it behaves predictably

изображение

@dwhswenson

This comment has been minimized.

Copy link
Author

commented Apr 2, 2019

@wanick : I believe the %%timeit behavior was the same before 7.4.0 (and made sense to me: timeit does multiple runs -- which one should be the result?) Of course, there's no such ambiguity with %%time.

@Carreau

This comment has been minimized.

Copy link
Member

commented Apr 2, 2019

@wanick

This comment has been minimized.

Copy link

commented Apr 2, 2019

@dwhswenson , True... I meant the same decorators and difference behavior

@Carreau, I solved this problem like this.

изображение

If this is a solution, I can make a pull request

@filannim

This comment has been minimized.

Copy link

commented Apr 10, 2019

Thank you guys. It all makes sense to me, I'm just wondering whether I need to update/edit all my Jupyter notebook now, or you're planning to restore the expected/canonical behaviour.

Thanks!

@Carreau

This comment has been minimized.

Copy link
Member

commented Apr 10, 2019

We're planning to fix it as soon as we have time; it will likely make it to the 7.5 release that I'll try to make before end of month.

@Carreau Carreau added this to the 7.5 milestone Apr 10, 2019

Carreau added a commit to Carreau/ipython that referenced this issue Apr 23, 2019

Fix applying @needs_global_scope to cell magics.
There was some side effect in some magic to allow passing
needs_local_scope. We know pass the global_scope to cell magice when
using @needs_local_scope, but at the same time this means that we can't
rely on the existance of local_ns from within the magic to know wheter
we are cell-called or line called.

Fixes ipython#11659
@Carreau

This comment has been minimized.

Copy link
Member

commented Apr 23, 2019

I should stop using "We", I'm practically the only developer reviewing and managing IPython itself.
This should have been fixed in a patch release but I only got time today and we're close to a month since last release so this is going to be in the next minor (7.5) if the fix is correct.

Thus I would appreciate review and testing of #11698, and once merged I'll start the process to do a 7.5 for before PyCon tutorials.

JamesSample added a commit to NIVANorge/Mobius that referenced this issue Apr 23, 2019

Downgrade recommended iPython version to 7.3
Due to ipython/ipython#11659. When fixed, can remove iPython from the `'conda install'` command.

@minrk minrk closed this in #11698 Apr 23, 2019

@Carreau

This comment has been minimized.

Copy link
Member

commented Apr 23, 2019

Thanks @minrk, BTW the above was not a critic toward you, and you are supposed to be on leave, so go enjoy your sleepless nights :-)

@davidmakovoz

This comment has been minimized.

Copy link

commented Jul 9, 2019

I'm experiencing the same issue. I just finished a fresh install of Anaconda on windows and discovered this problem. However it only occurs with my base environment:
conda version : 4.7.5
conda-build version : 3.18.5
python version : 3.7.3.final.0

I created (for a different reason) an environment with python 3.6 and python 3.7 (conda create -n testspacy37 python=3.x) and %%time works there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.