Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does Python magic timeit function return a syntax error?

I am trying to use the python magic function %timeit, but it returns a syntax error:

%timeit sum(range(100))
  File "<input>", line 1
    %timeit sum(range(100))
    ^
SyntaxError: invalid syntax

The error returns in both console and debug contexts.

It is like %timeit is an unrecognised module, but I can’t see anyway to import/add the magics and in any case I thought they were part of the standard Python library.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m using Python 3.10 in PyCharm 2022.1.2 (Community Edition)

I hope I’m not missing something obvious!

>Solution :

The % directives are an IPython convenience when running interactive Python code. They’re commands you directly give to the interpreter. They are not valid Python syntax and cannot be used in actual Python source files.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading