This document is a note from investigating the cost of migrating Django applications running on Python 2.7 to Python 3.5.
2to3
Change xrange
and [keys ()
,values () Change
, ʻitems ()](http://diveintopython3-ja.rdy.jp/porting-code-to-python-3-with-2to3.html#dict) with
2to3`.
Since it is possible to specify conversions that are not applied with -x
, the following conversions are excluded.
future
--from __future__ import unicode_literals
can be left, so it is excluded to reduce diff.print
--I've been thorough in using the print
function and excluded the print
statement as it shouldn't existcallable
--callable
is not available only for Python 3.0 / 3.1 and is not required as it is expected to migrate to Python 3.5.x.Overwrite the original file with the -w
option to perform the conversion.
$ 2to3 -x future -x print -x callable -w .
python-memcached
However, I think the behavior when putting the binary in Memcached is suspicious.
tweepy
google-api-python-client
Django Requires update to 1.8.x.
File "/home/csakatoku/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in <module>
HTMLParseError = _html_parser.HTMLParseError
AttributeError: module 'html.parser' has no attribute 'HTMLParseError'
dnspython
You need to change to dnspython3
released under a different package name. If you specify dnspython3
when installing with pip
, it's OK and you don't need to change the code.
MySQL-python
mysqlclient
instead of MySQL-python
.bitly-api Does not work with ʻImport Error`. However, it seems that it has been fixed in master.
>>> import bitly_api
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/csakatoku/.pyenv/versions/3.5.0/lib/python3.5/site-packages/bitly_api/__init__.py", line 1, in <module>
from bitly_api import Connection, BitlyError, Error
ImportError: cannot import name 'Connection'
django_ses
try
... ʻexcept` statement I tried to import It falls in an instant. However, it seems that it has been corrected in master.
>>> import django_ses
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/csakatoku/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django_ses/__init__.py", line 173
except SESConnection.ResponseError, err:
^
SyntaxError: invalid syntax
pynliner
SyntaxError
by the print
statement. Pull Request for Python 3 has been issued.
Collecting pynliner
Using cached pynliner-0.5.2.tar.gz
Collecting BeautifulSoup<4.0,>=3.2.1 (from pynliner)
Using cached BeautifulSoup-3.2.1.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "/tmp/pip-build-7je9i10s/BeautifulSoup/setup.py", line 22
print "Unit tests have failed!"
^
SyntaxError: Missing parentheses in call to 'print'
binary
as an argument of hashlib.sha1Bad
>>> import hashlib
>>> hashlib.sha1("SPAM").hexdigest()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Unicode-objects must be encoded before hashing
>>> hashlib.sha1("SPAM").hexdigest()
Good
>>> import hashlib
>>> hashlib.sha1("spam".encode("utf-8")).hexdigest()
'ded982e702e07bb7b6effafdc353db3fe172c83f'
Good
>>> import hashlib
>>> hashlib.sha1(b"spam").hexdigest()
'ded982e702e07bb7b6effafdc353db3fe172c83f'
Where did you go
sort
has changedBad
>>> data = [{"value": 1}, {"value": 3}, {"value": 2}]
>>> data.sort(lambda a, b: cmp(a["value"], b["value"]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must use keyword argument for key function
Good
>>> data = [{"value": 1}, {"value": 3}, {"value": 2}]
>>> data.sort(key=lambda x: x["value"])
>>> print(data)
[{'value': 1}, {'value': 2}, {'value': 3}]
This change is the tightest.
Recommended Posts