Python 2 Python 3
3.11.4
Guido van Rossum and the Python development team
24, 2023
Python Software Foundation
Email: docs@python.org
Contents
1 2
2 2
2.1 Python 2.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 “setup.py“ . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 Python 2 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.8 “setup.py“ Python 3 . . . . . . . . . . . . . . . . . . . . . . . . 6
2.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Brett Cannon
Python 3 Python Python 2 Python
Python 2 3
Python cporting-howto
Python Python 3 Nick Coghlan Python 3
Q&A Brett Cannon Python 3.
python-porting
1
1
Python 2/3 :
1. Python 2.7
2. coverage.py python -m pip install coverage
3. Python 2 3
4. Futurize ( Modernize) ( “python -m pip install future“)
5. Pylint Python 3 (python -m pip install pylint)
6. caniusepython3 Python 3 (python -m pip
install caniusepython3)
7. Python 2 3 (tox
Python python -m pip install tox)
8. Python 2 3 ( mypy
Python 2 Python 3 python -m pip install mypy)
: python -m pip install pip Python
pip
Python 2 3 ** ** Python
3 Python 3 Python 3
Python 2
Python 2 Python 3
Python 3 API
Python 2 3
2.1 Python 2.6
Python 2.5 Python 3 Python 2.7
Python 2.5 six Python 2.5 3 (python -m
pip install six) HOWTO
Python 2.5
Python
Python 2.7 Python 2.6
** ** Python 2.6 HOWTO Python
2.6 ( Pylint) Python
2
2.2 “setup.py“
“setup.py“ trove classifier Python
Python 3 “Programming Language :: Python :: 2 :: Only“
Python / Programming Language :: Python :: 2.7
.
2.3
Python 2
80%
90%
coverage.py
2.4 Python 2 3
Python 3
Python 3 Python 2
Python 3 ”What’s New” Porting to Python 3
Python-Future cheat sheet
2.5
Python 3 Python 2
: Futurize Modernize Python
3 Futurize Python 3 Python 2 Python 3 bytes
Python Modernize
Python 2/3 six Python 3 Futurize
Python 3
Python 3 Python 2
Python 3
Python 3
Modernize “io.open()“ “open()“
Python 3 5 / 2 == 2.5`` ``2 “int“ “float“
2002 Python 2.2 “/ //
``from __future__ import division “-Q“
1. from __future__ import division
2. // /
/ // __truediv__
__floordiv__ /
//
3
Python 2 “str“
“str“
API
API “unicode“
Python 3
Python
API
API Python 2
API unicode API Python 3 bytes
Python 2 str Python 2 bytes
Python 2 3 ( Python 2 unicode Python 3
str Python 2 str/bytes Python 3 “bytes“)
Python 2 3 ( decode() Python 2 3
Python 2 3 Python 3 str
) Python 3.5 __mod__ bytes
decode
encode
format
isdecimal
isnumeric
“b“ “u“
__future__ Unicode
“b“ “u“
Windows
“b“ “rb“ Python 3
io ** **
/ /
io.open() open() io Python 2 3
open() ( Python 3 io.open()) codecs.open()
Python 2.5
Python 2 3 str`` ``bytes`` Python 2
``bytes bytes(3) == '3' Python 3
“bytes“ bytes bytes(3)
== b'\x00\x00\x00' bytes “str“ Python 2
bytes str(b'3') == b'3' Python 3 bytes
str(b'3') == "b'3'"
Python 2 b'123'[1]
== b'2' Python 3 “b’123’[1] == 50“ Python 3
Python 2 bytes == str
4
six six.indexbytes() Python 3 : six.
indexbytes(b'123', 1)
1. API
2. “unicode“ Python 2 “bytes“
3. “b“ “u“
4.
5. io.open() b
6.
Python
Python
Python 2 Python 3
importlib Python 3.3 Python
PyPI importlib2 Python 2 importlib.abc
:
import sys
if sys.version_info[0] == 3:
from importlib import abc
else:
from importlib2 import abc
Python 4 Python 2 Python
3 Python Python 3 Python 2 :
import sys
if sys.version_info[0] > 2:
from importlib import abc
else:
from importlib2 import abc
try:
from importlib import abc
except ImportError:
from importlib2 import abc
5
2.6
Python 3 Python 3
Python 3
:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
Python 2 “-3“
“-Werror“
Pylint “--py3k“ Python 3
Modernize Futurize
Python 2.7 Python 3.4 Pylint Python
2.7
Python 3 ** ** ca-
niusepython3 Python 3
https://caniusepython3.com
Python 3 Python 3
2.8 “setup.py“ Python 3
Python 3 “setup.py“ “Programming Language
:: Python :: 3“ Python 2 Python 2 3
Python /
2.9
Python 3 Python 2 3 Python
tox tox
Python 2 3
Python 3 “-bb“ bytes string bytes int
Python 3.5 “False“
/
! Python 2 3
Python 2 3
6
2.10
mypy pytype
Python 2
Python 3
Python
API Python