我试图在Python2.7GUI上运行下面的代码:
python -m cProfile -s time abc.py但是,下面是我的错误:
>>> python -m cProfile -s time abc.py
>>> ^
>>> SyntaxError: invalid syntax知道我该怎么解决吗?
发布于 2016-12-22 20:58:46
您需要从命令行运行这个命令行,而不是一个GUI或交互式的提示符。看到>>>意味着您在交互式Python提示符上。
在命令行 a.k.a终端窗口上,切换到abc.py所在的目录,然后输入:
python -m cProfile -s time abc.py 我明白了:
python -m cProfile -s time abc.py
2 function calls in 0.000 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.000 0.000 0.000 0.000 abc.py:1(<module>)-m选项是这样做的:
-m模块:作为脚本运行库模块(终止选项列表)
Python版本为2.7.12。
编辑
如果您想从交互式提示符中执行此操作,可能最简单的方法是使用IPython或Jupyter笔记本。然后你就可以这样做:
[1] %run -m cProfile -s time abc.py发布于 2016-12-22 21:33:12
python -m ...本身并不是Python语法:它是从外部启动Python的语法。因此,Python解释器(GUI或非GUI)将无法处理该命令。(我们知道您是从Python解释器内部工作的,因为有telltale >>>提示符。)
“从外面”是什么意思?这意味着您需要在命令窗口中的>提示符(在$中)或在运行bash的终端窗口中的$提示符(在其他可能的OSes中)输入该命令。
https://stackoverflow.com/questions/41291410
复制相似问题