我正在尝试通过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

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

10-04 10:15
查看更多