Slide 1

Slide 1 text

Porting Django apps to Python 3 Jacob Kaplan-Moss [email protected]

Slide 2

Slide 2 text

“Django 1.5 is… the first release with Python 3 support! ... Everything’s in place for you to start porting your apps to Python 3.” — https://docs.djangoproject.com/en/1.5/releases/1.5/

Slide 3

Slide 3 text

Do I want to use Python 3?

Slide 4

Slide 4 text

ˑYes!

Slide 5

Slide 5 text

Python 3: now with 30% fewer warts!

Slide 6

Slide 6 text

ˑ Unicode no longer sucks!

Slide 7

Slide 7 text

Can I use Python 3?

Slide 8

Slide 8 text

✓ SQLite ✓ PostgreSQL ✘ MySQL

Slide 9

Slide 9 text

✓ modwsgi ✓ uWSGI ✓ gunicorn: sync ✘ gunicorn: async

Slide 10

Slide 10 text

✓ South ✓ Celery ✓ django-pipeline ✘ django-debug-toolbar ✘ django-registration ✘ django-extensions ✘ Haystack ✘ django-tagging ✘ Sentry ✘ django-compressor

Slide 11

Slide 11 text

Should I use Python 3?

Slide 12

Slide 12 text

Options 1. Python 3 only. 2. Translated source (2to3). 3. Single codebase.

Slide 13

Slide 13 text

Python 3 only?

Slide 14

Slide 14 text

2to3

Slide 15

Slide 15 text

ˑ Single source wins!

Slide 16

Slide 16 text

HOWTO

Slide 17

Slide 17 text

1. Choose an approach

Slide 18

Slide 18 text

2. Evaluate dependencies

Slide 19

Slide 19 text

3. Get the test suite running

Slide 20

Slide 20 text

4. Fix unicode handling

Slide 21

Slide 21 text

5. Iterate on test failures

Slide 22

Slide 22 text

Case study: a new website

Slide 23

Slide 23 text

1. Choose an approach Single source: Python 3.3 only.

Slide 24

Slide 24 text

2. Evaluate dependencies • Light CMS capabilities. • Moderately complex user / permissions / authentication system. • Heavy integration with social networks. • Moderate traffic with extreme “spikes.”

Slide 25

Slide 25 text

3. Get the test suite running ✓ django-discover-runner

Slide 26

Slide 26 text

4. Fix unicode handling django.utils.encoding

Slide 27

Slide 27 text

ˑIt works! ... but costs ~20% more.

Slide 28

Slide 28 text

Case study: django-sitetree Ported by Jeff Triplett (@webology) https://github.com/idlesign/django-sitetree/commit/c7475

Slide 29

Slide 29 text

1. Choose an approach Shared source: Python 2.6+, 3.3; Django 1.4+

Slide 30

Slide 30 text

2. Evaluate dependencies None (whew).

Slide 31

Slide 31 text

3. Get the test suite running Tox: http://tox.readthedocs.org/

Slide 32

Slide 32 text

Tox [tox] envlist  -­‐  py27-­‐django14,  py33-­‐django15 [py27-­‐django14] basepython  =  python2.7 deps  =  Django==1.4 [py33-­‐django15] basepython  =  python33 deps  =  Django==1.5

Slide 33

Slide 33 text

Syntax changes print  "foo"   print("foo") except  Exception,  ex:   except  Exception  as  ex: raise  Exception,  "msg"        raise  Exception("message”) class  C:                                    class  C(metaclass=M)      __metaclass__  =  M More: http://docs.python.org/3.0/whatsnew/3.0.html 2 3

Slide 34

Slide 34 text

4. Fix unicode handling django.utils.encoding

Slide 35

Slide 35 text

Models and unicode class  M(models.Model):   class  M(models.Model):        def  __unicode__(self):            def  __str__(self):                return  self.name                  return  self.name @python_2_unicode_compat class  M(models.Model):        def  __str__(self):                return  self.name 2 3

Slide 36

Slide 36 text

5. Iterate on test failures Six: http://pythonhosted.org/six/ (also django.utils.six)

Slide 37

Slide 37 text

Six class  C:                                    class  C(metaclass=M)      __metaclass__  =  M class  C(six.with_metaclass(M)): 2 3

Slide 38

Slide 38 text

Six isinstance(s,  str)   isinstance(s,  bytes) isinstance(s,  unicode)   isinstance(s,  str) isinstance(s,  six.binary_type) isinstance(s,  six.text_type) 2 3

Slide 39

Slide 39 text

Six if  six.PY3: ...

Slide 40

Slide 40 text

ˑ django.me/py3 is your new bicycle

Slide 41

Slide 41 text

Thanks! Jacob Kaplan-Moss [email protected] Questions? Follow me to room 201!