kernprof.py:NameError:name' profile'没有定义

时间:2016-03-09 20:04:23

标签: python profiling

当我运行使用@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)。

2 个答案:

答案 0 :(得分:3)

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

$ kernprof.py -l my_code.py

答案 1 :(得分:0)

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