Page 1 of 1

Error when importing custom python module

Posted: Fri Apr 30, 2021 10:51 am
by rckdev
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

Re: Error when importing custom python module

Posted: Fri Apr 30, 2021 11:27 am
by fdufnews
How do you copy your modules into the Pico?

Re: Error when importing custom python module

Posted: Fri Apr 30, 2021 11:31 am
by Roberthh
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.

Re: Error when importing custom python module

Posted: Fri Apr 30, 2021 11:57 am
by rckdev
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

Re: Error when importing custom python module

Posted: Fri Apr 30, 2021 12:38 pm
by Roberthh
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.

Re: Error when importing custom python module

Posted: Fri Apr 30, 2021 1:13 pm
by rckdev
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

Re: Error when importing custom python module

Posted: Fri Apr 30, 2021 1:19 pm
by Roberthh
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.

Re: Error when importing custom python module

Posted: Fri Apr 30, 2021 3:19 pm
by rckdev
Is it possible that I'm getting this error because of uploading ONLY the main script and not the "sensmod.py" file?

Re: Error when importing custom python module

Posted: Fri Apr 30, 2021 3:43 pm
by hippy
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'

Re: Error when importing custom python module

Posted: Fri Apr 30, 2021 4:20 pm
by hippy
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()'