Notebook: Cannot run %%time magic on a cell that contains a "leading" comment

Created on 9 Feb 2019  路  6Comments  路  Source: jupyter/notebook

While Using %%time for the first time, I had entered that magic into a cell which
contained a comment, resulting in this error message.

this is a python comment

%%time
time.sleep(.5)
"UsageError: Line magic function %%time not found."

                         <--- Blank line before %%time also causes the problem

%%time
time.sleep(.5)
"UsageError: Line magic function %%time not found."

Removing the comment made the error go away.
%%time
time.sleep(.5)
Wall time: 500 ms

Is it normal that the CPU time is not displayed on Windows?

My system is running Windows 7 Pro, 64-Bit

jupyter 1.0.0 py_1 conda-forge
jupyter_client 5.2.4 py_1 conda-forge
jupyter_console 6.0.0 py_0 conda-forge
jupyter_core 4.4.0 py_0 conda-forge

(py36sci) C:>conda list conda

Name Version Build Channel

msys2-conda-epoch 20160418 1

(py36sci) C:>conda list python

Name Version Build Channel

ipython 7.2.0 py36h39e3cac_1000 conda-forge
ipython_genutils 0.2.0 py_1 conda-forge
python 3.6.6 he025d50_0 conda-forge
python-dateutil 2.7.5 py_0 conda-forge

Most helpful comment

I think cell magics %% should start from the first line by convention. That's why they are called cell magics. For example, the line magic would work just fine and probably is preferred for usage

%time time.sleep(5)

All 6 comments

I think cell magics %% should start from the first line by convention. That's why they are called cell magics. For example, the line magic would work just fine and probably is preferred for usage

%time time.sleep(5)

If that's the case, it needs to be documented. However, a leading comment IS valid Python code, so it has to work for valid Python. Will wait until the developers comment

Encountering this same issue

UsageError: Line magic function %%timeit not found.

Code:

2 - Parallel computing feature (functional programming)

%%timeit -n 100
summary = np.sum(sprice)

@bencarpena Whenever using magic functions, you must keep it as the first line in your code. Try it this way and it should work:

%%timeit -n 100
#2 - Parallel computing feature (functional programming)
summary = np.sum(sprice)

I think this issue can be closed.

As an fyi for anyone, I have a real world example where i'm using nbdev and have a cell like:

# hide 
%%timeit -r2 -n5
df = do_stuff(df)
print(df.shape)

the "# hide" here is telling nbdev to not include the cell in my docs. I am just using the %%timeit under my function definition to make sure changes I make don't slow it down significantly.

Not sure what a good solution would be and probably more up to nbdev to handle itself maybe (ignore cell magics when looking for "# hide").

But just wanted to share my example here in case interesting or relevant to anyone else coming along and reading this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uolter picture uolter  路  3Comments

jonatanblue picture jonatanblue  路  3Comments

pylang picture pylang  路  3Comments

harshinielath picture harshinielath  路  3Comments

Foadsf picture Foadsf  路  3Comments