Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Don't understand fundamental concept of Pymakr and modules

    Scheduled Pinned Locked Moved Pymakr
    3 Posts 3 Posters 56 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P Offline
      Patrick Keogh
      last edited by Patrick Keogh

      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 main.py 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 umqtt.py. I copied it to .atom/packages/umqtt/umqtt.py
      I have a project that has three files:

      boot.py (empty)
      main.py (38 lines of code)
      lib/umtqq.py (205 lines of code)
      

      The first few lines of main.py are

      print("Init of main")
      import pycom
      pycom.heartbeat(False)
      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/boot.py (0kb)
      [2/3] Writing file Garage/lib/umqtt.py (6kb)
      [3/3] Writing file Garage/main.py (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
      clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
      mode:DIO, clock div:1
      load:0x3fff8020,len:8
      load:0x3fff8028,len:2140
      ho 0 tail 12 room 4
      load:0x4009fa00,len:19760'''
      
      

      That all looks OK to me, but then if I select main.py 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 main.py runs and the LED is indeed red, but the import fails. So what am I not understanding about importing modules?

      M 1 Reply Last reply Reply Quote 0
      • M Offline
        Manuel Ricardo Alfonso Sanchez @Patrick Keogh
        last edited by

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

        Garage

        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.

        1 Reply Last reply Reply Quote 0
        • P Offline
          p s
          last edited by

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

          from umqtt import MQTTClient
          

          to

          from lib.umqtt import MQTTClient
          

          Thit will solve the problem.

          However, I would recommend creating an empty file called __init__.py 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.

          1 Reply Last reply Reply Quote 1

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom