Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Porting Django apps to Python 3
Search
Jacob Kaplan-Moss
March 16, 2013
Technology
880
9
Share
Porting Django apps to Python 3
PyCon 2013.
Jacob Kaplan-Moss
March 16, 2013
More Decks by Jacob Kaplan-Moss
See All by Jacob Kaplan-Moss
To ••• With Passwords
jacobian
4
1.1k
How to Ace a Technical Interview
jacobian
281
24k
Implementing Multi-factor Auth (dotSecurity 2016)
jacobian
10
1.6k
Heroku Under The Hood - Django Under The Hood 2015
jacobian
9
710
Django's request/response cycle - Django Under The Hood 2015
jacobian
9
1.5k
Minimum Viable Security - Wharton Web Conference 2015
jacobian
1
1.3k
Django minus Django (DJangoCon EU 2014)
jacobian
12
1.5k
Heroku 101 – PyCon 2014
jacobian
1
1k
Be Agile, Not Vulnerable: Security engineering in an agile world
jacobian
8
840
Other Decks in Technology
See All in Technology
ルールやカスタム機能、どう使う?理想の出力を引き出すために今知りたいIBM Bob 5つの機能
muehara
1
330
AI Adaptable なテストを整える工夫 / Ways to Make Your Tests AI-Adaptable
bitkey
PRO
3
210
そのPoC、何を検証したつもりでしたか? AIプロダクトの価値検証で陥った落とし穴
techtekt
PRO
0
150
先取りMaven4 ~16年ぶりのメジャーアップデート、その進化とは?~
ogiwarat
0
140
Javaコミュニティをもっと楽しむための9箇条
takasyou
0
1.3k
BigQuery の Cross-cloud Lakehouse への歩み
phaya72
2
550
AIガバナンス実践 - 生成AIコネクタのデータ漏洩リスクと実務対策
knishioka
0
180
【Gen-AX】20260530開催_JJUG CCC 2026 Spring
genax
0
420
MIERUNE JCT 発表資料「宇宙から伊能忠敬ごっこ」
syuchimu
0
180
AI-DLCを活用した高品質・安全なAI駆動開発実践 / AI Driven Development with AI-DLC
yoshidashingo
0
130
AI Engineering Summit Tokyo 2026 AIの前に、やることがある 〜医療データ企業の4フェーズ〜
dtaniwaki
0
1.8k
Ruby::Boxでできること、Refinementsでできること
joker1007
3
390
Featured
See All Featured
Speed Design
sergeychernyshev
33
1.8k
A designer walks into a library…
pauljervisheath
211
24k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
440
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
190
Statistics for Hackers
jakevdp
799
230k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
520
Faster Mobile Websites
deanohume
310
31k
From π to Pie charts
rasagy
0
200
The untapped power of vector embeddings
frankvandijk
2
1.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Deep Space Network (abreviated)
tonyrice
0
160
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Transcript
Porting Django apps to Python 3 Jacob Kaplan-Moss
[email protected]
“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/
Do I want to use Python 3?
ˑYes!
Python 3: now with 30% fewer warts!
ˑ Unicode no longer sucks!
Can I use Python 3?
✓ SQLite ✓ PostgreSQL ✘ MySQL
✓ modwsgi ✓ uWSGI ✓ gunicorn: sync ✘ gunicorn: async
✓ South ✓ Celery ✓ django-pipeline ✘ django-debug-toolbar ✘ django-registration
✘ django-extensions ✘ Haystack ✘ django-tagging ✘ Sentry ✘ django-compressor
Should I use Python 3?
Options 1. Python 3 only. 2. Translated source (2to3). 3.
Single codebase.
Python 3 only?
2to3
ˑ Single source wins!
HOWTO
1. Choose an approach
2. Evaluate dependencies
3. Get the test suite running
4. Fix unicode handling
5. Iterate on test failures
Case study: a new website
1. Choose an approach Single source: Python 3.3 only.
2. Evaluate dependencies • Light CMS capabilities. • Moderately complex
user / permissions / authentication system. • Heavy integration with social networks. • Moderate traffic with extreme “spikes.”
3. Get the test suite running ✓ django-discover-runner
4. Fix unicode handling django.utils.encoding
ˑIt works! ... but costs ~20% more.
Case study: django-sitetree Ported by Jeff Triplett (@webology) https://github.com/idlesign/django-sitetree/commit/c7475
1. Choose an approach Shared source: Python 2.6+, 3.3; Django
1.4+
2. Evaluate dependencies None (whew).
3. Get the test suite running Tox: http://tox.readthedocs.org/
Tox [tox] envlist -‐ py27-‐django14, py33-‐django15 [py27-‐django14] basepython = python2.7
deps = Django==1.4 [py33-‐django15] basepython = python33 deps = Django==1.5
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
4. Fix unicode handling django.utils.encoding
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
5. Iterate on test failures Six: http://pythonhosted.org/six/ (also django.utils.six)
Six class C:
class C(metaclass=M) __metaclass__ = M class C(six.with_metaclass(M)): 2 3
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
Six if six.PY3: ...
ˑ django.me/py3 is your new bicycle
Thanks! Jacob Kaplan-Moss
[email protected]
Questions? Follow me to room 201!