How to Execute Pythin Scripts

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
andyf9798
Posts: 5
Joined: Sat Jun 06, 2020 9:27 am

How to Execute Pythin Scripts

Post by andyf9798 » Sat Jun 13, 2020 2:55 pm

Hi Guys

I am new to Micropython and trying to get to grips with it.

I started with executing commands in Tera Terminal and I all looks to be fine.

example I type 2+2 and it is echo back 4

But then if I create a test.py that contains 2+4 and then import test I get errors

I would like to create tests code then execute it, but I cannot figure out how.

User avatar
Roberthh
Posts: 2069
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How to Execute Pythin Scripts

Post by Roberthh » Sat Jun 13, 2020 3:02 pm

Please post the script and the errors.

andyf9798
Posts: 5
Joined: Sat Jun 06, 2020 9:27 am

Re: How to Execute Pythin Scripts

Post by andyf9798 » Sat Jun 13, 2020 4:15 pm

Code: Select all


import machine
import pyb
2+2

this file called test.py

Then I try import test.py

result is
>>> import test
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: no module named 'test'
>>>

>>> import test.py
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: no module named 'test'
>>>

I cannot figure how to execute a script.

User avatar
Roberthh
Posts: 2069
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How to Execute Pythin Scripts

Post by Roberthh » Sat Jun 13, 2020 4:31 pm

Did you put that script onto your device?
Depending on your device type, you have several options. Which device are you using?

andyf9798
Posts: 5
Joined: Sat Jun 06, 2020 9:27 am

Re: How to Execute Pythin Scripts

Post by andyf9798 » Sat Jun 13, 2020 4:35 pm

it is an STM32F407G-DISC1 development board, I have Micropythin running on it.

I copy the test.py to the same folder as boot.py and main.py called a PYBflash

The I connect to the board with Tera Term and I can execute the lines of the file but not execute it as a file.

User avatar
Roberthh
Posts: 2069
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How to Execute Pythin Scripts

Post by Roberthh » Sat Jun 13, 2020 6:19 pm

Did you disconnect the flash drive properly to ensure, that the file is actually written to your board?

SpotlightKid
Posts: 417
Joined: Wed Apr 08, 2015 5:19 am

Re: How to Execute Pythin Scripts

Post by SpotlightKid » Sat Jun 13, 2020 8:03 pm

Also, when you manage to import your script properly, please not that all code of module thats on a global level is executed when importing it. It is therefor better to put it into a function, which you call after importing, Secondly, when executing a script (as opposed to the interactive prompt), you need to ouput the results of expressions with print, otherwise they will not show up.

Code: Select all

def main():
    print(2+2)

Code: Select all

>>> from test import main
>>> main()
4

andyf9798
Posts: 5
Joined: Sat Jun 06, 2020 9:27 am

Re: How to Execute Pythin Scripts

Post by andyf9798 » Sun Jun 14, 2020 5:18 am

Ok Guys, Finally with your examples I managed to figure out where I was upside down.

I made this example testing.py

Code: Select all

import pyb
import machine
import ubinascii


def functionone():
	print('Hello from function 1!')

def functiontwo():
	print('Hello from function 2!')

def serialtest():
	id = machine.unique_id()
	serial_bytes = bytes([id[11], id[10] + id[2], id[9], id[8] + id[0], id[7], id[6]])
	serial = ubinascii.hexlify(serial_bytes).upper()
	print(serial)
So first of all, I save this to the board.

Then the magic came by resetting the board with Ctrl + D ( Soft Reboot)
MPY: sync filesystems
MPY: soft reboot
MicroPython v1.12 on 2019-12-20; F4DISC with STM32F407
Type "help()" for more information.

and then
>>>import testing
no errors ( Finally )

Now the thing I did not know is you must call the functions preceded with testing.

so:
testing.functionone() results in Hello from function 1!
testing.functiontwo() results in Hello from function 2!
testing.serialtest() results in b'206D35824E48'

if I import the function directly like
import from testing function1
import from testing function2
import from testing serialtest

then I can call those functions without preceding them with testing.

functionone() results in Hello from function 1!
functiontwo() results in Hello from function 2!
serialtest() results in b'206D35824E48'

This is something likely explained somewhere on a forum.

Two things I learnt today is that it seams to be mandatory to do soft-reboots each time some code is tested and the method of importing functions matters.

Post Reply