How to install other micropython libs in pyboard?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
kawata
Posts: 2
Joined: Fri Jan 08, 2016 6:16 am

How to install other micropython libs in pyboard?

Post by kawata » Fri Jan 08, 2016 6:39 am

Hi everyone! I am a beginner of micropython. After I flashed the firmware.dfu file made from 'stmhal' to pyboard successfully, I want to install the micropython-libs in my pyboard. But I don't know how to begin with it and I hope that someone know about it can help me to finish it. Thank you so much!

Turbinenreiter
Posts: 288
Joined: Sun May 04, 2014 8:54 am

Re: How to install other micropython libs in pyboard?

Post by Turbinenreiter » Fri Jan 08, 2016 11:04 am

You download the lib you want and copy it onto the pyboard.

EasyRider
Posts: 94
Joined: Wed Dec 30, 2015 8:17 am

Re: How to install other micropython libs in pyboard?

Post by EasyRider » Sat Jan 09, 2016 11:04 pm

I am also a beginner and interested in creating, saving and importing my own user libraries, modules, functions.

have read the MicrPython documentation and done searches but have not come across any clear information for beginners on how to.

I think beginners would really appreciate a brief clear tutorial on how to create a simple function/module and save it to pyboard as part of the user library that can then be imported into other programs.

For example:

in the link http://wiki.micropython.org/Importing-Modules

There is discussion about importing modules, but no clear information how and where the user created functions/modules are saved on pyboard?

Can someone give me a brief clear information how I can save following function on pyboard so that it can be executed in immediate REPL mode or imported into other programs?
There are 2 ways of creating modules: the first is to put your module into a file, and the second, is to put your module in a directory. So lets create a file, mod1.py which contains the following:

def hello():
print('hello from mod1')
and copy it to your internal flash. Note that you'll most likely need to restart micropython in order for it to see the new file. The simplest way of doing this is to press Control-D from the REPL. You can also press the RST (aka Reset) pushbutton. Now you can do:
>>> import mod1
>>> mod1.hello()
hello from mod1
Regards
John

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How to install other micropython libs in pyboard?

Post by dhylands » Sat Jan 09, 2016 11:43 pm

Where modules get loaded from is controlled by sys.path. On the pyboard, the default sys.path can be observed like this:

Code: Select all

>>> import sys
>>> sys.path
['', '/flash', '/flash/lib']
So if you place your modules that you want to import in the root of the internal flash or in a lib subdirectory, then import will work.

For example. Let's create 2 python files, called foo.py and bar.py. Suppose bar.py contains the following:

Code: Select all

def bar():
    print('bar was called')
and suppose foo.py contained:

Code: Select all

import bar

def foo():
    print('foo was called')
    print('about to call bar')
    bar.bar()
    print('after bar was called')
    
foo()
If I copied both the root of the internal flash, then from the REPL, I could do:

Code: Select all

>>> import foo
foo was called
about to call bar
bar was called
after bar was called
and we see that the foo function can call the bar function from the bar module.

EasyRider
Posts: 94
Joined: Wed Dec 30, 2015 8:17 am

Re: How to install other micropython libs in pyboard?

Post by EasyRider » Sun Jan 10, 2016 1:33 am

Thanks Dave,

That makes it very clear, except that I still lack some python file/directory handling fundamentals and I guess I need to learn that elsewhere/python3.

Sorry to be a pain, but until I get up to speed with python file/directory handling.

I have lost you at:
If I copied both the root of the internal flash, then from the REPL, I could do:
Can you clarify "If I copied both the root of the internal flash" and show how?

Regards
John

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How to install other micropython libs in pyboard?

Post by dhylands » Sun Jan 10, 2016 3:22 am

When you plug the pyboard into your PC, it should show up as a USB Mass Storage device. This is the root of the flash filesystem. So you copy the files using whatever it is you use for the OS of your host.

If you're using linux as your host, then you can use a tool I wrote called rshell to copy files over the serial port (allows USB Mass Storage to be disabled). See: https://github.com/dhylands/rshell

EasyRider
Posts: 94
Joined: Wed Dec 30, 2015 8:17 am

Re: How to install other micropython libs in pyboard?

Post by EasyRider » Sun Jan 10, 2016 3:59 am

Thanks Very Much Dave,
Yes, have to operate from the root directory.
All is good now.

Friendly Suggestion to The team.
For somebody that hasn't had much (recent) experience with Python or particularly terminal type Linux/Unix environment, may be useful if a small section can be dedicated in formal "Quick Reference" documentation on file/directory handling from micropython REPL or in REPL help(). Including details on how to create/import user modules for both flash and SD card similar to Dave's explanation in this thread.

Regards
John

kawata
Posts: 2
Joined: Fri Jan 08, 2016 6:16 am

Re: How to install other micropython libs in pyboard?

Post by kawata » Mon Jan 11, 2016 1:30 am

dhylands wrote:Where modules get loaded from is controlled by sys.path. On the pyboard, the default sys.path can be observed like this:

Code: Select all

>>> import sys
>>> sys.path
['', '/flash', '/flash/lib']
So if you place your modules that you want to import in the root of the internal flash or in a lib subdirectory, then import will work.

For example. Let's create 2 python files, called foo.py and bar.py. Suppose bar.py contains the following:

Code: Select all

def bar():
    print('bar was called')
and suppose foo.py contained:

Code: Select all

import bar

def foo():
    print('foo was called')
    print('about to call bar')
    bar.bar()
    print('after bar was called')
    
foo()
If I copied both the root of the internal flash, then from the REPL, I could do:

Code: Select all

>>> import foo
foo was called
about to call bar
bar was called
after bar was called
and we see that the foo function can call the bar function from the bar module.
Thanks a lot for your replying,Dave
I had successfully made it done!

Post Reply