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.” — http://django.me/1.5 “As of Django 1.6, Python 3 support is considered stable and you can safely use it in production. — http://django.me/faq

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* ? Oracle

Slide 9

Slide 9 text

✓ modwsgi ✓ uWSGI ✓ gunicorn: sync ? gunicorn: async

Slide 10

Slide 10 text

✓ South ✓ Celery ✓ Raven (Sentry) ✓ django-extensions ✘ django-compressor ✘ django-social-auth ✘ django-tagging ✘ django-debug-toolbar ✘ django-registration ✘ Haystack

Slide 11

Slide 11 text

✓ South ✓ Celery ✓ Raven (Sentry) ✓ django-extensions ✓ django-pipeline ✓ django-allauth ✓ django-taggit ✘ django-debug-toolbar ✘ django-registration ✘ Haystack

Slide 12

Slide 12 text

Should I use Python 3?

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Python 3 only?

Slide 15

Slide 15 text

2to3

Slide 16

Slide 16 text

ˑ Single source wins!

Slide 17

Slide 17 text

HOWTO

Slide 18

Slide 18 text

1. Choose an approach

Slide 19

Slide 19 text

2. Evaluate dependencies

Slide 20

Slide 20 text

3. Get the test suite running

Slide 21

Slide 21 text

4. Fix unicode handling

Slide 22

Slide 22 text

5. Iterate on test failures

Slide 23

Slide 23 text

Case study: a new website

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

4. Fix unicode handling django.utils.encoding

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

2. Evaluate dependencies None (whew).

Slide 32

Slide 32 text

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

Slide 33

Slide 33 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 34

Slide 34 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 35

Slide 35 text

4. Fix unicode handling django.utils.encoding

Slide 36

Slide 36 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 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 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 40

Slide 40 text

Six if  six.PY3: ...

Slide 41

Slide 41 text

More info: django.me/py3

Slide 42

Slide 42 text

ˑ We’ve done our job…

Slide 43

Slide 43 text

ˑ We’ve done our job… now it’s your turn!

Slide 44

Slide 44 text

Thanks! Jacob Kaplan-Moss [email protected]