1

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

Whonut
  • 288
  • 2
  • 12

3 Answers3

2

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.

Gareth A. Lloyd
  • 1,774
  • 1
  • 16
  • 26
  • Another solution is if you have write access to the python site-packages directory is to use that instead `OURPYPKGPATH=\`python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())\`"` – Gareth A. Lloyd Jul 14 '12 at 14:09
  • I too have a similar problem . I have installed stats using pip and I can see that find / -name "statsd" /usr/local/lib/python2.7/dist-packages/statsd Can you explain to why this is happening ? But when i run a import statsd in my code, I get an import error. – The_Lost_Avatar Jun 03 '15 at 11:44
1

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:~$
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

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.

Cianan Sims
  • 3,419
  • 1
  • 21
  • 30
  • Sorry, I accidentally saved the question before I finished. My installation of swampy is explained in it. Thanks for the speedy reply. – Whonut Jun 28 '12 at 19:37
  • @user1485047 Are you using a virtualenv or do you have any other versions of python installed? Open python in the console and enter: `from distutils.sysconfig import get_python_lib` and then `print(get_python_lib())`. What does it say? – Cianan Sims Jun 28 '12 at 20:02
  • It says:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages – Whonut Jun 28 '12 at 21:21
  • I'm on Mac so there might be some outdated version preinstalled – Whonut Jun 28 '12 at 21:22
  • @user1485047 Mac does have python preinstalled. On your path, this previous version will take precedence. It's probably something like `/usr/bin/python`. I think you can alter it, like the answer to this [question](http://stackoverflow.com/a/6954782/875127). – Cianan Sims Jun 28 '12 at 22:51
  • I see no mention of usr/bin/python in my path, but the python executable IS in usr/bin/. At some point I may have tried to fiddle with it before (bad idea, I know :/). Can't quite remember though. – Whonut Jun 29 '12 at 08:30
  • @user1485047 did you figure this out? I'm sorry I dropped off for awhile. This is heading out of my field of knowledge... – Cianan Sims Jul 05 '12 at 19:20
  • Not as yet. I'll see if I can bend a friend's ear about it. Will post the solution if one arises. Thanks for the help. – Whonut Jul 12 '12 at 14:20
  • the code I was using to print the PYTHONPATH variable in the interpreter – Whonut Jul 14 '12 at 13:14