1

Getting started with MicroPython and having problems with classes in separate files:

In main.py:

import clientBase
import time

if __name__ == "__main__":
    time.sleep(15)     # Delay to open Putty
    print("Starting")
    print("Going to class")
    cb = clientBase.ClientBaseClass
    cb.process()

In clientBase.py:

class ClientBaseClass:

    def __init__(self):
        print("init")

    def process(self):
        print("Process")

Compiles and copies to Pico without errors but does not run. Putty output: No idea how to run Putty (or other port monitor) without blocking port!

MPY: soft reboot
Traceback (most recent call last):

Thanks

Python Conslole:

"C:\Users\jluca\OneDrive\Apps\Analytical Engine\Python\Client\venv\Scripts\python.exe" "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2.4\plugins\python-ce\helpers\pydev\pydevconsole.py" --mode=client --port=59708 import sys; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['C:\Users\jluca\OneDrive\Apps\Analytical Engine\Python\Client', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\stdlib', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\micropython', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\rpi_pico', 'C:/Users/jluca/OneDrive/Apps/Analytical Engine/Python/Client']) PyDev console: starting. Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 1
    What are the contens of the traceback? Also, it looks like you included `main.py` twice in this question; what does `clientBase.py` look like? – larsks Mar 22 '22 at 23:50
  • You also have an error in the way in which you're trying to call `clientBase.ClientBaseClass` that would cause `cb.process()` to return an error, but without the traceback it's hard to tell if that's your immediate problem or not. – larsks Mar 22 '22 at 23:52
  • Sorry, I really messed up the editing for this post! should be OK now. – user7117046 Mar 23 '22 at 16:02
  • You still haven't provided the content of the traceback. Can you show what error you're getting? – larsks Mar 23 '22 at 17:44
  • I'm new to Pycharm/micropython and not sure what you mean by TracebacK. The Python Console output is added to post. I also get a warning about process not static. Have tried with and without @staticmethod: Thanks – user7117046 Mar 23 '22 at 18:35
  • I mean after the line `Traceback (most recent call last):`, is there any other output? – larsks Mar 23 '22 at 18:44

2 Answers2

1

The first problem I see here is that you're not properly instantiating the ClientBaseClass object. You're missing parentheses here:

if __name__ == "__main__":
    time.sleep(15)     # Delay to open Putty
    print("Starting")
    print("Going to class")
    cb = clientBase.ClientBaseClass    # <-- THIS IS INCORRECT
    cb.process()

This is setting the variable cb the class ClientBaseClass, rather than creating a new object of that class.

You need:

if __name__ == "__main__":
    time.sleep(15)     # Delay to open Putty
    print("Starting")
    print("Going to class")
    cb = clientBase.ClientBaseClass()
    cb.process()

I don't know if that's your only problem or not; seeing your traceback will shed more details on the problem.


If I fix that one problem, it all seems to work. I'm using ampy to transfer files to my Pico board (I've also repeated the same process using the Thonny edit, which provides a menu-driven interface for working with Micropython boards):

$ ampy  -p /dev/usbserial/3/1.4.2  put main.py
$ ampy  -p /dev/usbserial/3/1.4.2  put clientBase.py
$ picocom -b 115200 /dev/usbserial/3/1.4.2

I press return to get the Micropython REPL prompt:

<CR>
>>>

And then type CTRL-D to reset the board:

>>> <CTRL-D>
MPY: soft reboot

And then the board comes up, the code executes as expected:

<pause for 15 seconds>
Starting
Going to class
init
Process
MicroPython v1.18 on 2022-01-17; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>>

(note that if you replace MicroPython with CircuitPython,the Pico will show up as a drive and you can just drag-and-drop files on it.)

larsks
  • 277,717
  • 41
  • 399
  • 399
  • I did think of the parentheses and its the same with and without. Traceback? If that is the PyCharm "run" panel it shows: "C:\Users\jluca\OneDrive\Apps\Analytical Engine\Python\testing\venv\Scripts\python.exe" C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython/scripts/microupload.py -C "C:/Users/jluca/OneDrive/Apps/Analytical Engine/Python/testing" -v COM12 "C:/Users/jluca/OneDrive/Apps/Analytical Engine/Python/testing/main.py" Connecting to COM12 Uploading files: 0% (0/1) C:\Users\... -> main.py Uploading files: 100% (1/1) Soft reboot – user7117046 Mar 23 '22 at 20:17
  • Also, Commenting out the cb= and cb.process it will not run. Just the presence of the import clientBase causes it to fail! Putty shows: MPY: soft reboot Traceback (most recent call last): – user7117046 Mar 23 '22 at 20:35
  • Thanks all. I'll try Thonny and circuit Python tomorrow. I prototyped key parts of the app last year with 3 Arduinos in a network using C++. Really frustrating this week. Should be easy. The Pico will simplify things as I hope one core can monitor network and the other make things move. Again thanks – user7117046 Mar 24 '22 at 00:38
1

Tried micropython and circuitpython with Pycharm, Thonny and VisualStudio code. The only thing that reliably works is CircuitPython with Mu editor. I think its all about the way the .py files are copied to the Pico board and life's too short to do more diagnostics. Mu is pretty basic but it works! Thanks for the help.