Error when importing custom python module

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
rckdev
Posts: 7
Joined: Fri Apr 30, 2021 10:38 am
Contact:

Error when importing custom python module

Post by rckdev » Fri Apr 30, 2021 10:51 am

Hi,
I am trying to run a simple program which should do certain things depending on the value of an analog input.As long as I leave all the code in a single file, everything works, but if for example I put the "Sensor" class in a "sensmod.py" module, and I import it via "import sensmod" and then use "sensmod.Sensor()", it raises a "module not found" error.
Then I've tried to put the module in a folder "sens" in the same directory of the main script, and to import it using "from .sens import sensmod" but this raises "KeyError: __main__".
I don't know what to do anymore, I've never had a problem importing custom modules in python.
Has anything like this ever happened to anyone? Is there any solution? The only thing I found online refers to a bug report in micropython-lib

fdufnews
Posts: 58
Joined: Mon Jul 25, 2016 11:31 am

Re: Error when importing custom python module

Post by fdufnews » Fri Apr 30, 2021 11:27 am

How do you copy your modules into the Pico?

User avatar
Roberthh
Posts: 2697
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Error when importing custom python module

Post by Roberthh » Fri Apr 30, 2021 11:31 am

That's indeed strange. I am using this mechanism regularly. Can you show a simple version of your code which shows the error?
And you do use an IDE like Thonny? The latter may may hide whether files are permanently uploaded to the target or not.
For verification use a simple terminal emulator like Putty or picocom.

rckdev
Posts: 7
Joined: Fri Apr 30, 2021 10:38 am
Contact:

Re: Error when importing custom python module

Post by rckdev » Fri Apr 30, 2021 11:57 am

fdufnews wrote:
Fri Apr 30, 2021 11:27 am
How do you copy your modules into the Pico?
Roberthh wrote:
Fri Apr 30, 2021 11:31 am
And you do use an IDE like Thonny?
Actually I'm using Emacs with some ELisp functions i wrote which uses micropython to upload it to the device.
Roberthh wrote:
Fri Apr 30, 2021 11:31 am
The latter may may hide whether files are permanently uploaded to the target or not.
For the moment I'm just uploading them into the RAM, I'm not flashing the device (I've not figured how to do it from Emacs yet), so the persistence may be a problem

User avatar
Roberthh
Posts: 2697
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Error when importing custom python module

Post by Roberthh » Fri Apr 30, 2021 12:38 pm

Working with emacs and elisp, nothing should frighten you. I prefer using the mpr.py (with pyboard.py) script for copying files to the on-board file system. You can surely add a shell command into emacs.
pyboard.py is at https://github.com/micropython/micropyt ... ster/tools, and mpr.py is still a PR, but very useful.
mpr PR: https://github.com/micropython/micropython/pull/6375
You may also use just pyboard.py for uploading files.

rckdev
Posts: 7
Joined: Fri Apr 30, 2021 10:38 am
Contact:

Re: Error when importing custom python module

Post by rckdev » Fri Apr 30, 2021 1:13 pm

Roberthh wrote:
Fri Apr 30, 2021 12:38 pm
Working with emacs and elisp, nothing should frighten you.
Emacs and ELisp do ;)
Roberthh wrote:
Fri Apr 30, 2021 12:38 pm
You can surely add a shell command into emacs.
Yes, the emacs command is basically an async-shell-ommand which runs the pyboard.py script with the file of the buffer as a parameter with the com port and the baud rate.

I should specify that the error is received FROM the board, so i was thinking if i should compile the file using mpy-cross before uploading it to the board

User avatar
Roberthh
Posts: 2697
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Error when importing custom python module

Post by Roberthh » Fri Apr 30, 2021 1:19 pm

I should specify that the error is received FROM the board, so i was thinking if i should compile the file using mpy-cross before uploading it to the board
There should not be a difference between using .py or .mpy files, besides import time.

rckdev
Posts: 7
Joined: Fri Apr 30, 2021 10:38 am
Contact:

Re: Error when importing custom python module

Post by rckdev » Fri Apr 30, 2021 3:19 pm

Is it possible that I'm getting this error because of uploading ONLY the main script and not the "sensmod.py" file?

hippy
Posts: 35
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: Error when importing custom python module

Post by hippy » Fri Apr 30, 2021 3:43 pm

rckdev wrote:
Fri Apr 30, 2021 10:51 am
Then I've tried to put the module in a folder "sens" in the same directory of the main script, and to import it using "from .sens import sensmod" but this raises "KeyError: __main__".
That seems to be an issue of the leading period / dot ...

Code: Select all

>>> from sens.sensmod import Sensor
>>> 

Code: Select all

>>> from .sens.sensmod import Sensor
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: __main__

Code: Select all

>>> from sens import sensmod
>>>

Code: Select all

>>> from .sens import sensmod
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: __main__

Code: Select all

>>> import sens.sensmod
>>>

Code: Select all

>>> import .sens.sensmod
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
rckdev wrote:
Fri Apr 30, 2021 3:19 pm
Is it possible that I'm getting this error because of uploading ONLY the main script and not the "sensmod.py" file?
If you haven't uploaded 'sensmod.py' you won't be able to import it. You will still get the same error with the prefixed period / dot, but you will get a "no module" error if correctly specified but it's not there -

Code: Select all

>>> from sens.sensmod import Sensor
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'sens.sensmod'

Code: Select all

>>> from sens import sensmod
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'sens.sensmod'

Code: Select all

>>> import sens.sensmod
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'sens.sensmod'

hippy
Posts: 35
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: Error when importing custom python module

Post by hippy » Fri Apr 30, 2021 4:20 pm

I would start by using simpler code. On your MicroPython file system, both in your 'sens' directory ...

/sens/main.py

Code: Select all

import sens.sensmod

def Test():
  return sens.sensmod.Sensor()
/sens/sensmod.py

Code: Select all

def Sensor():
  return "Result from /sens/sensmod.py:Sensor()"

Then ...

Code: Select all

MicroPython v1.15-26 on 2021-04-29; Raspberry Pi Pico with RP2HACK
Type "help()" for more information.
>>> import sens.sensmod
>>> sens.sensmod.Sensor()
'Result from /sens/sensmod.py:Sensor()'

Code: Select all

>>> import sens.main
>>> sens.main.Test()
'Result from /sens/sensmod.py:Sensor()'

Code: Select all

>>> sens.main.sens.sensmod.Sensor()
'Result from /sens/sensmod.py:Sensor()'

Post Reply