Error when importing custom python module
Error when importing custom python module
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
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
How do you copy your modules into the Pico?
Re: Error when importing custom python module
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.
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
Actually I'm using Emacs with some ELisp functions i wrote which uses micropython to upload it to the device.
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
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.
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
Emacs and ELisp do
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
There should not be a difference between using .py or .mpy files, besides import time.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
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
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
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
I would start by using simpler code. On your MicroPython file system, both in your 'sens' directory ...
/sens/main.py
/sens/sensmod.py
Then ...
/sens/main.py
Code: Select all
import sens.sensmod
def Test():
return sens.sensmod.Sensor()
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()'