pythonturtle-graphicsplaysound

I want to use playsound, but i get: TypeError: 'module' object is not callable this error


import turtle,random
import playsound
t = turtle.Turtle()
s = turtle.Screen()
t.speed(0)
colors = ["red", "blue", "yellow", "green","cyan","orange","brown","pink","purple"]
s.bgcolor("orange")
t.penup()
t.goto(-200, -10)
t.pendown()

def h():
    t.lt(90)
    t.forward(120)
    t.rt(90)
    t.forward(25)
    t.rt(90)
    t.forward(50)
    t.lt(90)
    t.forward(40)
    t.lt(90)
    t.forward(50)
    t.rt(90)
    t.forward(25)
    t.right(90)
    t.forward(120)
    t.rt(90)
    t.forward(25)
    t.rt(90)
    t.forward(50)
    t.left(90)
    t.forward(40)
    t.left(90)
    t.forward(50)
    t.right(90)
    t.forward(25)
def a():
    t.penup()
    t.goto(-75,-10)
    t.rt(110)
    t.pendown()
    t.forward(125)
    t.rt(70)
    t.forward(20)
    t.rt(70)
    t.fd(129)
    t.rt(110)
    t.fd(20)
    t.rt(70)
    t.forward(100)
    t.left(130)
    t.forward(113)
def p():
    t.penup()
    t.goto(-175,-130)
    t.pendown()
    t.rt(150)
    t.forward(100)
    t.rt(90)
    t.forward(20)
    t.rt(90)
    t.forward(60)
    t.left(90)
    t.penup()
    t.goto(-155, -90)
    t.pendown()
    t.circle(30,180)
    t.penup()
    t.goto(-175, -130)
    t.right(180)
    t.pendown()
    t.fd(20)
    t.left(90)
    t.fd(40)
    t.penup()
    t.goto(-145,-130)
    t.pendown()
    t.write("2",font=("Normal",30,"normal"))

def y():
    t.penup()
    t.goto(-30,-130)
    t.pendown()
    t.forward(50)
    t.lt(35)
    t.forward(50)
    t.penup()
    t.goto(-30,-130)
    t.pendown()
    t.rt(125)
    t.forward(20)
    t.lt(90)
    t.forward(50)
    t.rt(35)
    t.forward(50)
    t.lt(125)
    t.forward(15)
    t.lt(55)
    t.forward(40)
    t.penup()
    t.goto(-56,-40)
    t.pendown()
    t.rt(-125)
    t.forward(15)
    t.rt(55)
    t.forward(40)
    t.hideturtle()

def _22_():
    t.penup()
    t.goto(60,-80)
    t.write("22!",font=("Normal", 100, "normal"))
    t.goto(30,-180)
    t.write("BRO!",font=("Courier", 80, "italic"))
def sound():
    playsound(r"C:\\Users\\Freddy\PycharmProjects\\pythonProject\\l.mp3")

h()
a()
p()
y()
sound()
_22_()
while True:
    s.bgcolor(random.choice(colors))
turtle.done()

C:\Users\Freddy\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Freddy\PycharmProjects\pythonProject\main.py Traceback (most recent call last): File "C:\Users\Freddy\PycharmProjects\pythonProject\main.py", line 122, in <module> sound() File "C:\Users\Freddy\PycharmProjects\pythonProject\main.py", line 116, in sound playsound(r"C:\Users\Freddy\PycharmProjects\pythonProject\l.mp3") TypeError: 'module' object is not callable

Process finished with exit code 1


Solution

  • That's because you are not referring playsound and python thinks it's a def function. When you import playsound try doing:

    from playsound import *
    

    or do:

    from playsound import playsound
    

    Otherwise, you can also change the actual code itself and do:

    playsound.playsound(r"C:\\Users\\Freddy\PycharmProjects\\pythonProject\\l.mp3")
    

    Hope this fixes your issue!