This documentation covers IPython versions 6.0 and higher. Beginning with version 6.0, IPython stopped supporting compatibility with Python versions lower than 3.3 including all versions of Python 2.7.

If you are looking for an IPython version compatible with Python 2.7, please use the IPython 5.x LTS release and refer to its documentation (LTS is the long term support release).

Module: utils.tokenize2

Patched version of standard library tokenize, to deal with various bugs.

Based on Python 3.2 code.


  • Gareth Rees’ patch for Python issue #12691 (untokenizing) - Except we don’t encode the output of untokenize - Python 2 compatible syntax, so that it can be byte-compiled at installation
  • Newlines in comments and blank lines should be either NL or NEWLINE, depending on whether they are in a multi-line statement. Filed as Python issue #17061.
  • Export generate_tokens & TokenError
  • u and rb literals are allowed under Python 3.3 and above.

Tokenization help for Python programs.

tokenize(readline) is a generator that breaks a stream of bytes into Python tokens. It decodes the bytes according to PEP-0263 for determining source file encoding.

It accepts a readline-like method which is called repeatedly to get the next line of input (or b”” for EOF). It generates 5-tuples with these members:

the token type (see the token (a string) the starting (row, column) indices of the token (a 2-tuple of ints) the ending (row, column) indices of the token (a 2-tuple of ints) the original line (string)

It is designed to match the working of the Python tokenizer exactly, except that it produces COMMENT tokens for comments and gives type OP for all operators. Additionally, all token lists start with an ENCODING token which tells you which encoding was used to decode the bytes stream.

4 Classes

class IPython.utils.tokenize2.TokenInfo

Bases: IPython.utils.tokenize2.TokenInfo

class IPython.utils.tokenize2.TokenError

Bases: Exception

class IPython.utils.tokenize2.StopTokenizing

Bases: Exception

class IPython.utils.tokenize2.Untokenizer

Bases: object


Initialize self. See help(type(self)) for accurate signature.

8 Functions*choices)

Convert tokens (an iterable) back into Python source code. Return a bytes object, encoded using the encoding specified by the last ENCODING token in tokens, or UTF-8 if no ENCODING token is found.

The result is guaranteed to tokenize back to match the input so that the conversion is lossless and round-trips are assured. The guarantee applies only to the token type and token string as the spacing between tokens (column positions) may change.

untokenize() has two modes. If the input tokens are sequences of length 2 (type, string) then spaces are added as necessary to preserve the round-trip property.

If the input tokens are sequences of length 4 or more (type, string, start, end), as returned by tokenize(), then spaces are added so that each token appears in the result at the position indicated by start and end, if possible.


The detect_encoding() function is used to detect the encoding that should be used to decode a Python source file. It requires one argument, readline, in the same way as the tokenize() generator.

It will call readline a maximum of twice, and return the encoding used (as a string) and a list of any lines (left as bytes) it has read in.

It detects the encoding from the presence of a utf-8 bom or an encoding cookie as specified in pep-0263. If both a bom and a cookie are present, but disagree, a SyntaxError will be raised. If the encoding cookie is an invalid charset, raise a SyntaxError. Note that if a utf-8 bom is found, ‘utf-8-sig’ is returned.

If no encoding is specified, then the default of ‘utf-8’ will be returned.

Open a file in read only mode using the encoding detected by detect_encoding().


The tokenize() generator requires one argument, readline, which must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as bytes. Alternately, readline can be a callable function terminating with StopIteration:

readline = open(myfile, 'rb').__next__  # Example of alternate readline

The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed is the logical line; continuation lines are included.

The first token sequence will always be an ENCODING token which tells you which encoding was used to decode the bytes stream.