4

I'd like to upgrade python version to 3.x for my Django project.

It's currently using python-2.7.

I failed to find a step by step guide on this.

Not sure how hard this is going to be...

eugene
  • 39,839
  • 68
  • 255
  • 489

1 Answers1

2

This is a community wiki post. Feel free to add to this post in case you find something missing.

Change Python path

You will need to change Python path from Python27 to Python34 (or whatever version of Python you're using).

This is necessary because whenever you'll do django-admin.py startproject mysite, command prompt will access only that script which is in your Path.

For editing Path variable: How to add Python path in windows 7?

Automatic Python2 to Python3 code translation using lib2to3

lib2to3 will take care of most of the things which need to be rewritten for Python3.

class MyModel(...):
    ...
    def __unicode__(self):
         ...

above code will be changed to:

class MyModel(...):
    ...
    def __str__(self):
        ...

Docs for lib2to3

NOTE: lib2to3 will only change keywords etc. You'll need to change which modules you're using manually. For example, you'll have to change import StringIO to import io manually.

Manual code translation

In case you want to do everything manually, here's a checklist for you:

  1. Replace __unicode__ with __str__
  2. Change print statement to print() function

    I don't think anybody uses print in Django. But if you are, do change it.

  3. Replace StringIO and cStringIO with io

    StringIO.StringIO becomes io.StringIO. There is no cStringIO in Python3.

Community
  • 1
  • 1
xyres
  • 20,487
  • 3
  • 56
  • 85