2

I am writing a program in Python that uses graphics to create a house using shapes. It works on my school computer but when I tried to run this program on my macbook, I get this error

from graphics import GraphicsWindow
ImportError: cannot import name 'GraphicsWindow'

Here is my code

from graphics import GraphicsWindow

# Create the graphics window named win
win = GraphicsWindow(400,400)

# Create a canvas we can draw on
canvas = win.canvas()

# Draw the house. Change this if you want to draw a different style house
canvas.setColor("red")
canvas.drawRect(100,150,200,200)
# Add your python code to complete the house drawing here...

# House texture

# Vertical lines
canvas.setColor("Magenta")
canvas.drawLine(100,170,300,170)
canvas.drawLine(100,190,300,190)
canvas.drawLine(100,210,300,210)
canvas.drawLine(100,230,300,230)
canvas.drawLine(100,250,300,250)
canvas.drawLine(100,270,300,270)
canvas.drawLine(100,290,300,290)
canvas.drawLine(100,310,300,310)
canvas.drawLine(100,330,300,330)

# Horizontal lines
canvas.drawLine(120,150,120,350)
canvas.drawLine(140,150,140,350)
canvas.drawLine(160,150,160,350)
canvas.drawLine(180,150,180,350)
canvas.drawLine(200,150,200,350)
canvas.drawLine(220,150,220,350)
canvas.drawLine(240,150,240,350)
canvas.drawLine(260,150,260,350)
canvas.drawLine(280,150,280,350)

# Door
canvas.setColor("brown")
canvas.drawRect(140,250,50,100)

# Door knob
canvas.setColor("yellow")
canvas.drawOval(180,300,5,5)

# House window
canvas.setColor("green")
canvas.drawRect(220,270,50,50)
canvas.setColor("blue")
canvas.drawLine(245,270,245,320)
canvas.drawLine(220,295,270,295)

# Roof boundary lines
canvas.setColor("red")
canvas.drawLine(200,50,100,150)
canvas.drawLine(200,50,300,150)

# Roof fill
canvas.setColor("purple")
canvas.drawPoly(100,150,200,50,300,150)

# Sign saying "Welcome Home"
canvas.setColor("orange")
canvas.drawRect(150,185,101,25)
canvas.setColor("black")
canvas.drawText(155,190,"Welcome Home")

# Creates a stick person
canvas.setColor(255,204,153)
canvas.drawOval(310,250,30,30) # Head
canvas.setColor("black")
canvas.drawLine(325,320,325,280) # Body
canvas.drawLine(325,320,310,350) # Left leg
canvas.drawLine(325,320,340,350) # Right Leg
canvas.drawLine(325,300,310,300) # Left arm
canvas.drawLine(325,300,340,300) # Right arm
canvas.drawOval(317,260,3,3) # Left eye
canvas.drawOval(327,260,3,3) # Right eye
canvas.drawOval(320,270,6,3) # Mouth


# This statement will make the window stay open until its "close" button is clicked
win.wait()

What could be wrong? I have Python 3.5.1 installed.

Reginald
  • 23
  • 1
  • 1
  • 4
  • Did you by any chance name your file `graphics.py`? That would mean you have accidentally imported your own file instead of the library. – Tadhg McDonald-Jensen Apr 25 '16 at 19:35
  • No Its not named graphics.py but it is named houseprogram.py – Reginald Apr 25 '16 at 19:38
  • are you able to ask your teacher for a copy of the `graphics.py` module to copy to your personal computer? It is not on [pypi](https://pypi.python.org/pypi) and I can't find any obviously correct package with google. – Tadhg McDonald-Jensen Apr 25 '16 at 19:51
  • @TadhgMcDonald-Jensen *That would mean you have accidentally imported your own file ...* Thats wrong. Modules found in the Pythonpath go first. – linusg Apr 25 '16 at 19:53
  • @linusg please see http://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at, shadowing modules is a fairly common question on SO. – Tadhg McDonald-Jensen Apr 25 '16 at 20:20

3 Answers3

0

Your code seems to be okay, but you have to install the graphics module (while it's not installed, Python will raise an ImportError). Give pip a try:

pip install graphics

I think, it's because your school installed this already.

EDIT:

I've looked into the Python docs and graphics seems to be an module delivered with Python, but only wit some editions I think (I don't have it either). See this: http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/graphics.html. I would recommend asking your teacher how they've installed this or what Python edition they use.

EDIT2:

You can download the graphics module here: http://mcsp.wartburg.edu/zelle/python/graphics.py. Just put the file in your site-packages folder of the Python installation.

linusg
  • 6,289
  • 4
  • 28
  • 78
  • 3
    there is no `graphics` module that can be installed via pip. – Tadhg McDonald-Jensen Apr 25 '16 at 19:33
  • How do I install this and where? – Reginald Apr 25 '16 at 19:36
  • if there was no module to import called `graphics` then the error would read `ImportError: No module named 'graphics'` but that is not the message, so this is clearly a different problem. – Tadhg McDonald-Jensen Apr 25 '16 at 19:37
  • I asked my teacher and he said that we have 3.5.1 installed at school also. – Reginald Apr 25 '16 at 19:42
  • @linusg the file you linked to has absolutely no reference to `GraphicsWindow` at all, so it is clearly not the same thing. – Tadhg McDonald-Jensen Apr 25 '16 at 19:46
  • @TadhgMcDonald-Jensen In the Python doc, it says `from graphics import * win = GraphWin()`. Also John Zelle is named at the beginning. The file at the link is from this guy. – linusg Apr 25 '16 at 19:52
  • @linusg I tried to install graphics.pyin my site-packages folder but nothing happens. I get the same error – Reginald Apr 25 '16 at 19:53
  • No need to install, just put it in the folder. What error do you get? To have a `graphics.py` in site-packages and get an `ImportError` with `import graphics` is still impossible. – linusg Apr 25 '16 at 19:55
  • @linusg that is not the [official python documentation](https://www.python.org/doc/) and if the OP can import something called `GraphicsWindow` on the schools computer and there is nothing with that name in your linked file it can't be the same one used by their school. – Tadhg McDonald-Jensen Apr 25 '16 at 19:55
  • @TadhgMcDonald-Jensen I know the official docs. If you still do not believe me, see this: http://cpsc110.umwblogs.org/installing-python-graphics-library/, this: http://mcsp.wartburg.edu/zelle/python/ and this: https://www.rose-hulman.edu/Users/faculty/young/CS-Classes/resources/Python/ZelleGraphics.html. – linusg Apr 25 '16 at 19:59
  • @linusg I think that maybe, due to the extremely generic nature of the word "graphics", that maybe Reginald's school is using a different `graphics` module. The first line of code in the original question is `from graphics import GraphicsWindow` so the graphics module they are using clearly has something in it called `GraphicsWindow`, which is not present in the one you have linked to. I have a strong feeling they are different `graphics` modules. – Tadhg McDonald-Jensen Apr 25 '16 at 20:07
0

I think the graphics module your school is using originated from http://www.ezgraphics.org since it does contain a GraphicsWindow and your code runs correctly using the graphics.py obtained from there.

Note that you can place the graphics.py in the same folder as your working file or in the site-packages folder in your library.

(pretty house btw)

image generated from OP's script

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
-1

Copy any graphics.py into your IDE Editor or directory of file.py. Then use import graphics.