kernprof.py:NameError:未定义名称'profile'

Posted

技术标签:

【中文标题】kernprof.py:NameError:未定义名称\'profile\'【英文标题】: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)。

【问题讨论】:

如果问题已解决,请考虑接受您的回答。 【参考方案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

【讨论】:

以上是关于kernprof.py:NameError:未定义名称'profile'的主要内容,如果未能解决你的问题,请参考以下文章

python:NameError:全局名称'...'未定义[重复]

NameError:名称'unicode'未定义[重复]

NameError:名称'python'未定义[关闭]

NameError:名称“”未定义[关闭]

NameError:未定义名称“convertToBinaryData”

Buildozer:NameError:未定义名称'functools'