title: %timeit is working individually but failing with .py file
body: <p>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.</p>
  <pre><code>from scripts.download_sftp_extract_fun import sftp_extract
  import sys
  import timeit
  times = %timeit -o sftp_extract.download_sftp_extract(extract_name=&quot;RE_AMS_SUBD_SLS_WKLY_&quot;)
  print(times,file=open(&quot;logs/log_re_ams_subd_sls_wkly.txt&quot;,'w'))
  </code></pre>
  <p>error message:-</p>
  <pre><code>In [26]: exec(open('scripts/extract_download_re_ams_subd_sls_wkly_.py').read())
  Traceback (most recent call last):
  
    File &quot;/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py&quot;, line 3417, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
  
    File &quot;&lt;ipython-input-26-21b570da7ee2&gt;&quot;, line 1, in &lt;module&gt;
      exec(open('scripts/extract_download_re_ams_subd_sls_wkly_.py').read())
  
    File &quot;&lt;string&gt;&quot;, line 4
      times=%timeit -o sftp_extract.download_sftp_extract(extract_name=&quot;RE_AMS_SUBD_SLS_WKLY_&quot;)
            ^
  SyntaxError: invalid syntax
  </code></pre>