Migrate Django applications running on Python 2.7 to Python 3.5

What is this?

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.

Overwrite the original file with the -w option to perform the conversion.

$ 2to3 -x future -x print -x callable -w .

Libraries that needed to be updated

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'

Libraries that need to be transferred

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

Library that doesn't work

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'

Code that needs correction

You have to pass binary as an argument of hashlib.sha1

Bad

>>> 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'

urllib.basejoin is gone

Where did you go

The argument of sort has changed

Bad

>>> 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

Migrate Django applications running on Python 2.7 to Python 3.5
Update applications running on Django 1.7 to Django 1.8
Introduction to Python Django (2) Win
From python to running instance on google cloud platform
How to build a Django (python) environment on docker
Update python on Mac to 3.7-> 3.8
How to use Django on Google App Engine / Python
Don't lose to Ruby! How to run Python (Django) on Heroku
Python 3.6 on Windows ... and to Xamarin.
Introduction to Python Hands On Part 1
python + django + scikit-learn + mecab (1) on heroku
python + django + scikit-learn + mecab (2) on heroku
Steps to install python3 on mac
Introduction to Python Django (2) Mac Edition
Update Python on Mac from 2 to 3
Things to note when running Python on EC2 from AWS Lambda
Everything from building a Python environment to running it on Windows
How to read pydoc on python interpreter
[Kivy] How to install Kivy on Windows [Python]
Build CGI Server running on Python 3 on Docker
Follow active applications on Mac with Python
Steps to install Python environment on Ubuntu
How to erase Python 2.x on Mac.
Connecting from python to MySQL on CentOS 6.4
Memorandum on how to use gremlin python
Install python on xserver to use pip
[Python] Introduce UIKit3 to your Django project
Work memo to migrate and update Python 2 series scripts on the cloud to 3 series
A memo of a tutorial on running python on heroku
Updated to Python 2.7.9
Run django applications on Windows + Apache + mod_wsgi + services.
Put MicroPython on Windows to run ESP32 on Python
[2020 version] How to install Python3 on AWS EC2
Easy way to use Python 2.7 on Cent OS 6
Strategy on how to monetize with Python Java
How to install OpenCV on Jetson Nano Python
Python on Windows
Python Django Tutorial (5)
Python Django Tutorial (2)
twitter on python3
Introducing 4 ways to monitor Python applications using Prometheus
How to use Python Kivy ④ ~ Execution on Android ~
How to run MeCab on Ubuntu 18.04 LTS Python
[Django] Redo migrate
Python Django Tutorial (8)
Python Django Tutorial (6)
python on mac
A story about running Python on PHP on Heroku
Introduction to Python with Atom (on the way)
Summary of points I was addicted to running Selenium on AWS Lambda (python)
To write to Error Repoting in Python on GAE
What Python beginners got hooked on with Django
Python on Windbg
[Python] How to install OpenCV on Anaconda [Windows]
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
[Python] Beginners troubleshoot while studying Django web applications
Run Python on Apache to view InfluxDB data
Python Django Tutorial (3)
Connect to MySQL with Python on Raspberry Pi