Frozen modules not present when running external scripts?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
purplepenguins
Posts: 1
Joined: Mon Jul 30, 2018 8:37 pm

Frozen modules not present when running external scripts?

Post by purplepenguins » Mon Jul 30, 2018 8:41 pm

I've been experimenting with frozen modules of late...Today I noticed something odd - the frozen modules are accessible just fine from the micropython cli, but if I try to run a script externally with the imports of my frozen modules specified like so "./micropython testscript.py", it says there is no module named my module. But if I just run ./micropython and import it there, it's present. What gives?

User avatar
pythoncoder
Posts: 3119
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Frozen modules not present when running external scripts?

Post by pythoncoder » Tue Jul 31, 2018 10:34 am

The answer seems to lie in sys.path. At the REPL I get

Code: Select all

>>> sys.path
['', '/home/adminpete/.micropython/lib', '/usr/lib/micropython']
Frozen modules are imported from '', so they will work. But the following script

Code: Select all

import sys

def main():
    print(sys.path)

if __name__ == '__main__':
    main()
run from Bash produces

Code: Select all

[adminpete@axolotl]: ~
$ micropython rats5.py
['/home/adminpete', '/home/adminpete/.micropython/lib', '/usr/lib/micropython']
[adminpete@axolotl]: ~
$ 
So I think your code needs to issue

Code: Select all

if sys.path[0] != '':
     sys.path.insert(0,'')
I'm not sure why this difference exists.
Peter Hinch

Post Reply