Question

I am reading the book Think Python by Allen Downey. For chapter 4, one has to use a suite of modules called Swampy. I have downloaded and installed it.

The problem is that the modules were written in Python 2 and I have Python 3 (in Windows 7 RC1). When I ran the TurtleWorld module from Swampy, I got error messages about the print and exec statements, which are now functions in Python 3. I fixed those errors by including parentheses with print and exec in the code of the GUI and World modules. I also got an error that the Tkinter module could not be found. It turned out that in Python 3, the module name is spelled with a lower case t.

The third error is more difficult: ImportError: No module named tkFont.

Does anyone have any idea, please? Thank you.

Was it helpful?

Solution

Many important third-party libraries have not yet been rewritten for Python 3; you'll have to stick to Python 2.x for now. There is no way around it. As it says on the official Python download page,

If you don't know which version to use, start with Python 2.6.4; more existing third party software is compatible with Python 2 than Python 3 right now.

OTHER TIPS

It looks like tkinter is finally catching up with Python 3 - tkFont has become tkinter.font

http://docs.pythonsprints.com/python3_porting/py-porting.html

#!/usr/bin/env python3.2
# -*- coding: utf-8 -*-
#
#       font_ex.py
#       

import tkinter

top = tkinter.Tk()

butt01 = tkinter.Button(top, text="Hello World", font=('Helvetica', 24,))

custom_font_serif = ('Times', 24, 'bold')
butt02 = tkinter.Button(top, text="Hello World", font=custom_font_serif)

custom_font_sans = ('Helvetica', 36, 'italic')
butt03 = tkinter.Button(top, text="Hello World", font=custom_font_sans)

butt01.pack()
butt02.pack()
butt03.pack()

top.mainloop()

FOR MAC USERS: I'm a Python newbie and came across the exact same problem. I'm writing this so others don't waste several hours trying to figure this out. Here's what you do:

  • Do NOT install Python 3 for the above reasons, i.e. to avoid having to change all the Swampy code. Instead, download the latest version of Python 2 (I used 2.7.5).
  • Then download the Swampy-2.1.1.tar.gz file from here: https://pypi.python.org/pypi/swampy/2.1.1. Extract this file by double-clicking on it in your Downloads folder.
  • Open the resulting folder, Swampy 2.1.1. You should see several items, including the folder, swampy.
  • Here's the key step: Drag this folder to the last one in this path: Macintosh HD/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. TO REPEAT: drag the folder "swampy" and drop it into the "site-packages" folder.
  • Once you do that, fire up Python in the terminal and type "import swampy.TurtleWorld." If you see the chevrons (>>>) and you don't see an error message, you're good to continue the lessons.

There is a conversion tool for converting Python 2 code to work with Python 3: http://svn.python.org/view/sandbox/trunk/2to3/

Not sure how this extends to 3rd party libraries but it might be worth passing this over the swampy code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top