【问题标题】:kernprof.py: NameError: name 'profile' is not definedkernprof.py:NameError:未定义名称'profile'
【发布时间】:2016-03-09 20:04:22
【问题描述】:

当我运行带有@profile 注释的代码时出现以下错误:

Wrote profile results to monthly_spi_gamma.py.prof
Traceback (most recent call last):
  File "/home/james.adams/anaconda2/lib/python2.7/site-packages/kernprof.py", line 233, in <module>
    sys.exit(main(sys.argv))
  File "/home/james.adams/anaconda2/lib/python2.7/site-packages/kernprof.py", line 223, in main
    prof.runctx('execfile_(%r, globals())' % (script_file,), ns, ns)
  File "/home/james.adams/anaconda2/lib/python2.7/cProfile.py", line 140, in runctx
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "monthly_spi_gamma.py", line 1, in <module>
    import indices
  File "indices.py", line 14, in <module>
    @profile
NameError: name 'profile' is not defined

谁能评论什么可以解决这个问题?我在 Windows 7 上使用 Python 2.7 (Anaconda)。

【问题讨论】:

  • 如果问题已解决,请考虑接受您的回答。

标签: python profiling


【解决方案1】:

我通过使用 -l 选项解决了这个问题,即

$ kernprof.py -l my_code.py

【讨论】:

    【解决方案2】:
    kernprof -l -b web_app.py
    

    如果我们看到,这对我有用

    kernprof --help
    

    我们看到了一个包含在内置命名空间中的选项

    usage: kernprof [-h] [-V] [-l] [-b] [-o OUTFILE] [-s SETUP] [-v] [-u UNIT]
                    [-z]
                    script ...
    
    Run and profile a python script.
    
    positional arguments:
      script                The python script file to run
      args                  Optional script arguments
    
    optional arguments:
      -h, --help            show this help message and exit
      -V, --version         show program's version number and exit
      -l, --line-by-line    Use the line-by-line profiler instead of cProfile.
                            Implies --builtin.
      -b, --builtin         Put 'profile' in the builtins. Use
                            'profile.enable()'/'.disable()', '@profile' to
                            decorate functions, or 'with profile:' to profile a
                            section of code.
      -o OUTFILE, --outfile OUTFILE
                            Save stats to <outfile> (default: 'scriptname.lprof'
                            with --line-by-line, 'scriptname.prof' without)
      -s SETUP, --setup SETUP
                            Code to execute before the code to profile
      -v, --view            View the results of the profile in addition to saving
                            it
      -u UNIT, --unit UNIT  Output unit (in seconds) in which the timing info is
                            displayed (default: 1e-6)
      -z, --skip-zero       Hide functions which have not been called
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2018-01-24
      • 2018-02-21
      • 2020-07-31
      相关资源
      最近更新 更多