How can a flash a micropython program that imports an external module?

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

How can a flash a micropython program that imports an external module?

Post by Jim.S » Thu Feb 22, 2018 9:03 pm

I am trying to flash a script to the microbit that imports from an external module. I have written a simple example and successfully tested on my linux box, with the main program (ModuleTest.py) and module (Hello.py) in the same directory.

ModuleTest.py

Code: Select all

# testing modules on the microbit
try:
    from microbit import *
    not_microbit=False
except ImportError:
    not_microbit=True
    print('not microbit')
from Hello import *
print_hello()
With Hello.py containing

Code: Select all

def print_hello():
    print('Hello')
    return
Mu doesn't work properly on my version of Linux (Mint 17) so I cannot use that. I have tried to use uFlash but cannot understand how to make it import the module. When I flash the main program using the command uflash ModuleTest.py it only seems to flash the main program and I get the following error

Traceback (most recent call last):
File "__main__", line 9, in <module>
ImportError: no module named 'Hello'

I can't see in the uflash documentation anyway of making it recognise modules in the same directory, does it need the module to be in a sub directory?

Jim

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: How can a flash a micropython program that imports an external module?

Post by deshipu » Fri Feb 23, 2018 9:27 am

I think you are looking for https://github.com/ntoll/microfs

Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

Re: How can a flash a micropython program that imports an external module?

Post by Jim.S » Fri Feb 23, 2018 9:13 pm

Thanks! I now understand. I use ufs to copy the module file (in this case Hello.py) to the microbits memory and use uflash to flash the main programme (ModuleTest.py). This is easier if you can get mu to run on your system (but difficult if you are running Mint 17)

And I now understand why i am unlikely to get ascyncio running on a microbit (or even usched as I was hoping to) The microbits memory is so small you cannot copy a large module file to it.

Is it possible to reduce the size of a module file by compiling it to byte code?

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: How can a flash a micropython program that imports an external module?

Post by mcauser » Sat Feb 24, 2018 3:59 am

This one to flash a vanilla MicroPython runtime:
https://github.com/ntoll/uflash

I have a precompiled v1.9 firmware available here:
https://github.com/mcauser/microbit-tm1 ... r/firmware
or you could build it from source:
https://github.com/bbcmicrobit/micropyt ... s/version1

This one to copy your main.py, module.py, etc:
https://github.com/ntoll/microfs

If you created a .hex file from one of the online editors, eg. https://python.microbit.org/v/1, it includes the older stable v1.7 MicroPython runtime and contains both the MicroPython runtime and a script to run on boot. It's sort of like boot.py or main.py, however, you can't see it with ufs ls or os.listdir(). If you'e comfortable with the command line, you're better off using uflash + microfs above.

Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

Re: How can a flash a micropython program that imports an external module?

Post by Jim.S » Sun Feb 25, 2018 12:45 pm

So are you saying that if I flash the micropython firmware to the microbit, I can use microfs to copy a text python script to the microbits memory and it will run just like a pyboard? I am currently under the impression that I have to 'compile' the python script to a hex file before it will run on the microbit.

I currently believe that one of the limitations of the microbit is the memory available for programs and modules. So I'm still interested in finding out if there is a way to precompile the modules (just like the main.py) to .hex code so that they take up less space

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: How can a flash a micropython program that imports an external module?

Post by mcauser » Sun Feb 25, 2018 1:50 pm

That's right. The hex file is just a combination of the runtime + a script to run.
You can make your own combined hex with this tool:
https://github.com/bbcmicrobit/micropyt ... inedhex.py
firmware.hex + script.py = combined.hex

I normally flash the v1.9 firmware once, then repeatedly ufs rm/put the files I'd like to run.
If you precompiled your own combined.hex, I'm not sure how much less memory it consumes. Anyone?

Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

Re: How can a flash a micropython program that imports an external module?

Post by Jim.S » Sun Feb 25, 2018 9:16 pm

Nice one! Thanks! This makes programming the microbit very similar to the pyboard

What I did was flash the firm ware to the microbit using uflash
$ uflash -r microbit-micropython-v1.9.2-34-gd64154c73.hex
renamed my python script ModuleTest.py main.py then copied it to the microbit using ufs
$ ufs put main.py
then copied the module file to the microbit using ufs
$ufs put Hello.py

and then rebooted the microbit and it worked perfectly..

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: How can a flash a micropython program that imports an external module?

Post by mcauser » Sun Feb 25, 2018 10:09 pm

One thing worth noting. If you have a mistake in your main.py/boot.py and you get stuck in a reboot loop, you haven't bricked your device. Simply flash a new micropython.hex and it will wipe the slate clean and you can ufs put your files back (after you fix the bug).

rhubarbdog
Posts: 168
Joined: Tue Nov 07, 2017 11:45 pm

Re: How can a flash a micropython program that imports an external module?

Post by rhubarbdog » Sat May 12, 2018 2:06 am

Jim.S wrote:
Thu Feb 22, 2018 9:03 pm

Mu doesn't work properly on my version of Linux (Mint 17) so I cannot use that.
have you tried these instructions to install Mu https://ubuntuforums.org/showthread.php?t=2377314

Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

Re: How can a flash a micropython program that imports an external module?

Post by Jim.S » Sun May 13, 2018 8:13 am

Yes, I have tried those but on linux Mint 17 I run into problems with the installed/available version of pyqt5.

Post Reply