TypeError: 'module' object is not callable



  • Hi everybody

    I am new here to use the deep sleep shield. I add the deepsleep.py in the library and sync the project. I don't know if it's right and I am very confused this process:0_1522411632391_00db0c68-9025-479d-8c6c-e0d52896e43e-image.png
    You can see the deepsleep.py file is in the left side of my screenshots.
    The problem is always: TypeError: 'module' object is not callable!
    0_1522411779061_0a1af925-d0dd-4229-adaa-8b282926814e-image.png

    import pycom
    import time
    from machine import Pin, Timer
    from deepsleep import DeepSleep
    
    echo = Pin(Pin.exp_board.G7, mode=Pin.IN)
    trigger = Pin(Pin.exp_board.G8, mode=Pin.OUT)
    
    # Colors different color stand for different state
    off = 0x000000
    red = 0xff0000
    green = 0x00ff00
    blue = 0x0000ff
    
    # Turn off hearbeat LED
    pycom.heartbeat(False)
    
    #enable the deepsleep
    ds = DeepSleep()
    
    trigger(0)
    chrono = Timer.Chrono()
    
    while True:
        ds.go_to_sleep(60)  # go to sleep for 60 seconds
        print("Wake up")
        chrono.reset()
    
        trigger(1)
        time.sleep_us(10)
        trigger(0)
    
        while echo() == 0:
            pass
    
        chrono.start()
    
        while echo() == 1:
            pass
    
        chrono.stop()
    
        distance = chrono.read_us() / 58.0
    
        if distance > 400:
            print("Out of range")
        else:
            print("Distance {:.0f} cm".format(distance))
    
        time.sleep(1)


  • This error statement TypeError: 'module' object is not callable is raised as you are being confused about the Class name and Module name. The problem is in the import line . You are importing a module, not a class. This happend because the module name and class name have the same name .

    If you have a class "MyClass" in a file called "MyClass.py" , then you should import :

    from MyClass import MyClass
    

    In Python , a script is a module, whose name is determined by the filename . So when you start out your file MyClass.py with import MyClass you are creating a loop in the module structure.

    In Python, everything (including functions, methods, modules, classes etc.) is an object , and methods are just attributes like every others. So,there's no separate namespaces for methods. So when you set an instance attribute, it shadows the class attribute by the same name. The obvious solution is to give attributes different names.



  • @jcaron Thank you. I tried but still like this, I don't know why it's so complicated.
    0_1522429924401_11fc2968-19ce-49de-8853-1b51e65584cf-image.png
    I run the deepsleep.py and there is no action in the console, if the deepsleep code is right. I downloaded from there.
    https://github.com/pycom/pycom-libraries/tree/master/deepsleep
    0_1522430148933_e30bfb96-f357-4737-8873-299c3bc69a76-image.png



  • @nathanh Thaks. I tried. It still the same problem.



  • Would it be because you have the directory as /Lib instead of /lib (i.e. try using lowercase).



  • The errors do not seem to be very consistent. Note that Pymakr uploads files it knows about but it can leave odd stuff on the module's filesystem.

    I recommend you either use FTP to check that you only have the files required, or clear the flash and then re-upload with Pymakr, it should help.

    Also, don't add all those other files in lib, you'll end up with issues.




  • Global Moderator

    @rachelsimida if you type in the REPL prompt:

    from deepsleep import DeepSleep
    dir (DeepSleep)
    

    What do you get as output?



  • @jcaron I tried but still fail.0_1522416923781_b54165aa-8551-4720-a011-25df8be45387-image.png



  • I believe you should have deepsleep.py directly in the lib folder, not a subfolder.



  • This is my device. 0_1522414421476_bd41dd1f-2625-4387-9340-de3099824a68-image.png



Pycom on Twitter