python-3.x - Python (c) 配置文件 : Error importing SortKey from pstats

标签 python-3.x

我正在尝试阅读 Python 配置文件(rs)的文档。我在 Win10 笔记本电脑上的 Anaconda 中使用 Python 3.6。

https://docs.python.org/3/library/profile.html

import cProfile
import re
cProfile.run('re.compile("foo|bar")')

根据文档,这执行没有问题。

然而,
import pstats
from pstats import SortKey

导致此错误消息:
ImportError: cannot import name 'SortKey'

有一个class SortKey(str, Enum)在这个版本的 pstats :
https://github.com/python/cpython/blob/master/Lib/pstats.py

但是,当我查看本地 pstats.py 时文件,我没有那个类,例如~\AppData\Local\Continuum\anaconda3\envs\py36\Lib\pstats.py与(cPython)非常不同pstats.py .

我想我错过了一些明显的东西......

最佳答案

问题是 Python 3.6 与 Python 3.7。

3.6,没有 SortKey :
https://docs.python.org/3.6/library/profile.html

3.7,与SortKey :
https://docs.python.org/3.7/library/profile.html

在文档 URL 中使用整数默认为最新版本,例如3.7,当我需要 3.6 的文档时。

关于python-3.x - Python (c) 配置文件 : Error importing SortKey from pstats,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53490248/

相关文章:

python - 用自定义消息替换错误

python - Python 中的 yield 问题 |使用辅助函数

python - 第二次 python 执行失败

python - 无法将大小为 921600 的数组 reshape 为形状 (1,128,128,1)

python - 根据计数器值对列表进行排序

python - 使 numpy 数组 JSON 可序列化

python - 更重要的是pythonic : trivial lambda or None?

python - 我如何将两个不同字典中的值相乘

Python将字节串写入文件

python - 对数据帧列表执行操作