No module error when module is installed

I’ve been using Think Python to learn programming over the last few days. Today I got to chapter 4 when it starts talking about needing Swampy, a package (correct term?) used to teach, in this chapter, interface design.

So, getting to my issue, the first bit of code I’m told to enter is

from swampy.TurtleWorld import * 
world = TurtleWorld()
bob = Turtle()
wait_for_user()

When I run it, I get the following error

Traceback (most recent call last):
File "/Users/dylanevans/Documents/Code/Python/TurtleWorld.py", line 1, in <module>
from swampy.TurtleWorld import *
ImportError: No module named swampy.TurtleWorld

I have installed and uninstalled swampy using pip and distutils, swampy is in site-packages and when I ask the interpreter what modules are installed, swampy shows up. I just don’t see why I’m getting the error.

Also, my PYTHONPATH has ‘/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/swampy’ at the end. Although it does have it twice, I don’t know whether that’s important.

Any help would be greatly appreciated.

Thanks

Answer

Open up a terminal and type env | grep ^PYTHONPATH hopefully you get something like this:

PYTHONPATH=/python/path/with/write/access:/another/python/path

Select one of the paths you have write access to and that will be OURPYPKGPATH=/python/path/with/write/access

If no such PYTHONPATH exists we will make our own in our home directory and ensure python can see it in future:

mkdir ~/.ourPyPkgPath
echo 'export PYTHONPATH=$PYTHONPATH:~/.ourPyPkgPath' >> ~/.profile

And in this case we will use OURPYPKGPATH=~/.ourPyPkgPath

Now to install swampy

easy_install -d $OURPYPKGPATH 'http://pypi.python.org/packages/source/s/swampy/swampy-2.1.1.tar.gz'

Now it should be just a case of either source ~/.profile or logging out and in again, in order to set the PYTHONPATH environmental variable.

Leave a Reply

Your email address will not be published. Required fields are marked *