No module error when module is installed

4.5k Views Asked by At

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

3

There are 3 best solutions below

2
Gareth A. Lloyd On BEST 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.

9
Cianan Sims On

Have you installed the swampy module? If not, follow these instructions.

To run a standalone script, you can put your code inside the following block:

if __name__ == '__main__':
    world = TurtleWorld()
    bob = Turtle()
    wait_for_user()

This tells python to execute the code if the script is being directly called. So if you instead had this in a function like swampFunction and you called it from a different script, the above block wouldn't run.

0
Burhan Khalid On

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.

Did you add it there manually? You should modify PYTHONPATH if you install packages in non-standard locations - in other words, if you don't use pip or easy_install but want the package to be available to Python globally.

To eliminate such headaches, it is recommended to use a virtual python environment, which you can create using virtualenv. Once you have virtualenv installed:

burhan@lenux:~$ virtualenv --no-site-packages swampy
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in swampy/bin/python
Installing distribute.............................................................................................................................................................................................done.
Installing pip...............done.
burhan@lenux:~$ source swampy/bin/activate
(swampy)burhan@lenux:~$ pip install swampy
Downloading/unpacking swampy
  Downloading swampy-2.1.1.tar.gz (46Kb): 46Kb downloaded
  Running setup.py egg_info for package swampy

Installing collected packages: swampy
  Running setup.py install for swampy

Successfully installed swampy
Cleaning up...
(swampy)burhan@lenux:~$ python
Python 2.7.3 (default, Apr 20 2012, 22:44:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from swampy.TurtleWorld import *
>>> quit()
(swampy)burhan@lenux:~$