运行 cProfile 时语法无效

invalid syntax when run cProfile

我尝试 运行 python -m cProfile simple_test_script.py。我在 Windows 7,Python 2.7.10.

simple_test_script.py:

import numpy as np
from numpy.linalg import eigvals

def run_experiment(niter=100):
    K = 100
    results = []
    for _ in xrange(niter):
        mat = np.random.randn(K, K)
        max_eigenvalue = np.abs(eigvals(mat)).max()
        results.append(max_eigenvalue)
    return results
some_results = run_experiment()
print 'Largest one we saw: %s' % np.max(some_results)

我收到这个错误:

File "<ipython-input-13-6634cb53f497>", line 1
    python -m cProfile simple_test_script.py
                     ^
SyntaxError: invalid syntax

我阅读了这份文档:https://docs.python.org/2/library/profile.html

(Use profile instead of cProfile if the latter is not available on your system.)

我尝试使用 profile 而不是 cProfile,但出现了同样的错误。有什么线索可以调用 cProfile 吗?

您似乎在 运行 在 IPython 中执行以下命令:

python -m cProfile simple_test_script.py

你应该 运行 它在你的 shell。

正如 satoru 所建议的,您通常会在 shell/terminal/console 中 运行 这样的命令(对于日常使用,这些基本上意味着相同的事情)。但是,您 可以 也可以 运行 从内部 IPython,例如:

%run -m cProfile simple_test_script.py

(%符号是命令的一部分,IPython有一些以%开头的特殊命令