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 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?
-
-
This is a classic.
You have to change your import fromfrom 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.
-
@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.