Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python (c)profile: Error importing SortKey from pstats

Tags:

python-3.x

I'm trying to work through the docs for Python's profile(rs). I'm using Python 3.6 in Anaconda on a Win10 laptop.

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

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

This executes no problem, per the docs.

However,

import pstats
from pstats import SortKey

results in this error message:

ImportError: cannot import name 'SortKey'

There is a class SortKey(str, Enum) in this version of pstats: https://github.com/python/cpython/blob/master/Lib/pstats.py

However, when I look through local pstats.py files, I do not have that class, e.g. ~\AppData\Local\Continuum\anaconda3\envs\py36\Lib\pstats.py is very different than the (cPython) pstats.py.

I assume I am missing something obvious...

like image 319
Evan Avatar asked Nov 26 '18 22:11

Evan


1 Answers

The issue was Python 3.6 vs Python 3.7.

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

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

Using an integer in the docs URL defaults to the latest version, e.g. 3.7, when I needed the docs for 3.6.

like image 114
Evan Avatar answered Oct 14 '22 22:10

Evan