Howto Pyporting
Howto Pyporting
3.11.4
24, 2023
Python Software Foundation
Email: [email protected]
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
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 2 3 ** ** Python
3 Python 3 Python 3
Python 2
Python 2 Python 3
Python 3 API
Python 2 3
2
2.2 “setup.py“
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
/ // __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
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
:
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.9
! Python 2 3
Python 2 3
6
2.10
mypy pytype
Python 2
Python 3
Python
API Python