SaiKiran
SaiKiran

Reputation: 6504

ImportError : No module named graphics

I tried working on a graphics algorith which requires a graphics package, so I imported graphics like in this code:

from graphics import *
import time
 
def BresenhamLine(x1,y1,x2,y2):
    """ Bresenham Line Drawing Algorithm For All Kind Of Slopes Of Line """
   
    dx = abs(x2 - x1)
    dy = abs(y2 - y1)
    slope = dy/float(dx)
    
    x, y = x1, y1   
 
    # creating the window
    win = GraphWin('Brasenham Line', 600, 480)
    
    # checking the slope if slope > 1 
    # then interchange the role of x and y
    if slope > 1:
        dx, dy = dy, dx
        x, y = y, x
        x1, y1 = y1, x1
        x2, y2 = y2, x2
 
    # initialization of the inital disision parameter
    p = 2 * dy - dx
    
    PutPixle(win, x, y)
 
    for k in range(2, dx):
        if p > 0:
            y = y + 1 if y < y2 else y - 1
            p = p + 2*(dy - dx)
        else:
            p = p + 2*dy
 
        x = x + 1 if x < x2 else x - 1
        
        # delay for 0.01 secs
        time.sleep(0.01)
        PutPixle(win, x, y)
 
def PutPixle(win, x, y):
    """ Plot A Pixle In The Windows At Point (x, y) """
    pt = Point(x,y)
    pt.draw(win)
 
def main():
    x1 = int(input("Enter Start X: "))
    y1 = int(input("Enter Start Y: "))
    x2 = int(input("Enter End X: "))
    y2 = int(input("Enter End Y: "))
 
    BresenhamLine(x1, y1, x2, y2)
        
if __name__ == "__main__":
    main()

But when I executed this code it gave me this error:

python app.py 
Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from graphics import *
ImportError: No module named graphics

So I tried to install the graphics package with pip but it too failed:

sudo pip install graphics
Downloading/unpacking graphics
  Could not find any downloads that satisfy the requirement graphics
Cleaning up...
No distributions at all found for graphics
Storing debug log for failure in /home/ubuntu/.pip/pip.log

How to rectify this error?

Upvotes: 2

Views: 22620

Answers (3)

Arash Davoudi
Arash Davoudi

Reputation: 1

You should be able to install the graphics package with this command:

pip install graphics.py

Copy that command and paste in your terminal then press Enter.

When it says it was installed, you can check at the lib(libary) in Python folder. For example, on Windows the address is this:

C\Users\DELL\AppData\Local\Programs\Python\Python313\Lib

Upvotes: 0

Mohit Namdev
Mohit Namdev

Reputation: 1

click to the link shown here. https://mcsp.wartburg.edu/zelle/python/graphics.py Now follow this : select all text data > copy > save (in working folder) > graphics.py > enter. Create a new file like <test.py> , write code and test it.

Upvotes: -2

Francisco
Francisco

Reputation: 11496

You have to download this file and put it in the same folder with your script.

From the docstring:

INSTALLATION: Put this file somewhere where Python can see it.

Upvotes: 6

Related Questions