0

I am trying to capture majic (%timeit) command results to text file, same executing individually in ipython but calling in .py file, it's throwing syntax error. please help me what exactly missing in my script.

from scripts.download_sftp_extract_fun import sftp_extract
import sys
import timeit
times = %timeit -o sftp_extract.download_sftp_extract(extract_name="RE_AMS_SUBD_SLS_WKLY_")
print(times,file=open("logs/log_re_ams_subd_sls_wkly.txt",'w'))

error message:-

In [26]: exec(open('scripts/extract_download_re_ams_subd_sls_wkly_.py').read())
Traceback (most recent call last):

  File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3417, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-26-21b570da7ee2>", line 1, in <module>
    exec(open('scripts/extract_download_re_ams_subd_sls_wkly_.py').read())

  File "<string>", line 4
    times=%timeit -o sftp_extract.download_sftp_extract(extract_name="RE_AMS_SUBD_SLS_WKLY_")
          ^
SyntaxError: invalid syntax
1
  • magics work in an ipython session, not in a regular python script. There are ways of importing and running them, but the %... syntax is not one of them. Commented Oct 21, 2020 at 2:53

1 Answer 1

0

The proper usage of timeit is as follows:

import timeit
timeit.timeit("sftp_extract.download_sftp_extract(extract_name=\"RE_AMS_SUBD_SLS_WKLY_\")")

Check out it's documentation for more details

Sign up to request clarification or add additional context in comments.

4 Comments

but below statement working fine in ipython3, i just copied that to .py file and calling again. Was there any difference calling python scripts from shell and .py file? ` In [48]: from scripts.download_sftp_extract_fun import sftp_extract In [49]: import sys In [50]: import timeit In [51]: times = %timeit -o sftp_extract.download_sftp_extract(extract_name="RE_AMS_SUBD_SLS_WKLY_") 14 s ± 2.33 s per loop (mean ± std. dev. of 7 runs, 1 loop each) In [52]: print(times,file=open("logs/log_re_ams_subd_sls_wkly.txt",'w')) `
@Koppula ipython's % is special, it's not actual python syntax.
Umm bit confused, however will try to work with your solution. Thank you very much for your inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.