Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Importing Graphics Python

I'm typing at the console

from graphics import * 

and I get this error

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
from graphics import *
ImportError: No module named graphics
like image 898
Sneaks McGurk Avatar asked Dec 16 '22 21:12

Sneaks McGurk


2 Answers

I think your question comes from trying to work through Python Programming by John Zelle, with a Macintosh.

The first 4 chapters are great. But in chapter 5, where I tended to get stuck, he introduces Objects. The way he does that is kind of interesting. He has created a python module which he calls “graphics.py” which you can download from his website.

This module (I’ve attached a link to it below) is a python program written by Mr. Zelle. It creates the tools for making very simple shapes and getting used to the basic concepts with graphics and it also serves as a more tangible way of introducing Objects.

However I was confused, it took me a while to realize that “graphics.py” was a pedagogical program Mr. Zelle created, and not something that comes bundled with MacPython. This confusion stems from the fact that the programs in Chapter 5 all begin with “import graphics” which looks very similar to the “import math” command at the beginning of every program in the 3rd chapter.

The key difference is that “import math” imports the standard mathematics library that came pre-bundled with MacPython. But “import graphics” refers to John Zelle’s own “graphics.py” module, which you have to download and instal first.

It took me a while to figure that out….

Then once I did, I went to his website, copied the program from this website:

http://mcsp.wartburg.edu/zelle/python/graphics.py

...into IDLE and then saved it as graphics.py

This is where it got maddening...

On Windows if you just put the graphics.py file in the same folder as Python, it can find the file and use it without a problem.

Here is what the book said, that made me feel so crazy:

“To use the graphics module (graphics.py) you need to place this file where Python can locate it. >One simple approach is to put it in the same folder where you keep your Python programs. Starting >Python in this folder will also let you import the graphics library to experiment interactively. Alternatively, you can place the graphics.py file in a system-wide directory so that it is >available for import no matter what directory Python starts in. The standard directory for placing >local additions to Python is the site-packages directory. On my Windows installation, the complete >path to the folder is:

C:\Python23\Lib\site-packages

On my Linux system, the file resides in:

/usr/local/bin/lib/python2.3/site-packages."

On a windows OS, all that you have to do is go to Python.org and download Python for Windows and put that graphics.py file in the main folder, and boom, you’re golden, NOT SO FOR MACINTOSH!!!!

2 years ago, this is where I got totally stuck, because I had no idea about site paths, or directories; I just pointed and clicked; I didn’t know about the Unix system underneath the Macintosh Aqua GUI.

And the book gives no instructions for what to do if you have a Macintosh, and I hit a wall.

But when I went at it again a few weeks ago, those directories made more sense to me, because I spent the spring and summer playing with a guidebook to using the UNIX command shell on my Mac.

So I realized that my problem had nothing to do with Tkinter, It was just that graphics.py needed to be in the right directory. I couldn’t just put it in the folder next to IDLE as I could on a Windows machine. I needed to find the right directory.

Since OSX is built on a UNIX base, I thought that the file path might be the same as Linux. Sadly, there was no “local”, “bin” after the “user” directory. But its not quite the same in OSX.

Instead you can use IDLE itself to find out which path directories it uses.

You type this:

import sys

print sys.path

and BOOM, it spits out a whole bunch of directories, confusingly formatted like this

['', '/Users/jamesbarnard/Documents', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages’, '/Library/Python/2.7/site-packages’]

But when I looked closer I noticed the directory “site-packages” which looked a lot like the “site-packages” on the Linux and Windows command lines. So I pulled out that directory chain

['', '/Users/jamesbarnard/Documents', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload’, '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', ‘ /Library/Python/2.7/site-packages’]

Then using the UNIX shell I followed it down, and then I did the same thing in the Aqua interface.

And there, buried 8 levels down in the directory, amidst hundreds of other files, I placed my graphics.py file.

Then I went back into IDLE, typed in “import graphics”

AND IT WORKED!!

If you are having this problem. I hope this solution saves you my headache.

like image 115
James Barnard Avatar answered Jan 02 '23 17:01

James Barnard


did you follow the instructions to install the graphics module and is it in your pythonpath?

like image 32
gtrak Avatar answered Jan 02 '23 18:01

gtrak