Don't understand fundamental concept of Pymakr and modules

  • I wasn't sure whether to put this in Pymakr or just "Getting Started"...

    I know Python well, but despite reading lots of introductory documentation there is something fundamental that I don't understand. I have Atom and Pymakr running fine. I can load a Python module as and it executes just fine. I can type Python statements to the ">>>" and they work fine too.

    My program will use MQTT, and so I found the module and I have it as I copied it to .atom/packages/umqtt/
    I have a project that has three files: (empty) (38 lines of code)
    lib/ (205 lines of code)

    The first few lines of are

    print("Init of main")
    import pycom
    pycom.rgbled(0xFF0000)  # Green
    from umqtt import MQTTClient
    from network import WLAN
    import machine
    import time

    So I use Pymakr to

    • Format flash storage

    • With the project selected, Upload Project to Device

    Reading file status
    Failed to read project status, uploading all files
    Creating dir Garage
    Creating dir Garage/lib
    [1/3] Writing file Garage/ (0kb)
    [2/3] Writing file Garage/lib/ (6kb)
    [3/3] Writing file Garage/ (0kb)
    Upload done, resetting board...
    OKets Jun  8 2016 00:22:57
    rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    mode:DIO, clock div:1
    ho 0 tail 12 room 4

    That all looks OK to me, but then if I select and click Run I get

    Init of main
    Traceback (most recent call last):
      File "<stdin>", line 7, in <module>
    ImportError: no module named 'umqtt'
    Pycom MicroPython 1.20.2.r1 [v1.11-a5aa0b8] on 2020-09-09; WiPy with ESP32
    Pybytes Version: 1.6.0
    Type "help()" for more information.

    So runs and the LED is indeed red, but the import fails. So what am I not understanding about importing modules?

  • This is a classic.
    You have to change your import from

    from umqtt import MQTTClient


    from lib.umqtt import MQTTClient

    Thit will solve the problem.

    However, I would recommend creating an empty file called inside your lib folder. This way you just need to make a reference to the package where your libraries are, like

    from directory.filename import Myclass

    This might be helpful if you need to import something from a directory one level up.

  • @Patrick-Keogh said in Don't understand fundamental concept of Pymakr and modules:


    Hi. I suggest you to upload files directly in flash. As I seen in your upload sketch it appears a Garage folder and inside the lib folder and so on.

Log in to reply

Pycom on Twitter