main.mpy is not executed?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
zmaier
Posts: 13
Joined: Thu May 16, 2019 12:44 pm

main.mpy is not executed?

Post by zmaier » Wed Jun 19, 2019 6:55 am

Hi,
sorry for my english, i hope you understand what I am trying to explain :)

i am using an Wemos D1 Mini Pro and my main.py file is to big to compile. I got a memory error (problem is well described here in the forum).
So i am trying to get rid of that by pre-compiling it.
I have the compiled main.mpy file, which I copied to my Device. But it is not beeing excututed?
To simplify the problem, I tried the following very simple test:

boot.py

Code: Select all

print('boot.py start')
print('boot.py end')
main.py

Code: Select all

print('main.py start')
import time
while True:
	print('---')
	time.sleep(1)
with main.py beeing compiled on the device, everything works, but when copying the main.mpy file instead of the main.py file on the device. It wont execute main.mpy? Any idea why?

User avatar
jimmo
Posts: 800
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: main.mpy is not executed?

Post by jimmo » Wed Jun 19, 2019 9:33 am

I'd have to check but my memory is that it specifically looks for main.py. (Unlike the" import foo" statement which tries both foo.mpy and foo.py)

On pyboard (and other STM32 boards), you can use pyb.main('other.py') to override the default path but I don't know if this or an equivalent is available on your board.

However there's likely an easy workaround -- create app.mpy (or anything.mpy) and just have main.py contain a single line "import app".

zmaier
Posts: 13
Joined: Thu May 16, 2019 12:44 pm

Re: main.mpy is not executed?

Post by zmaier » Wed Jun 19, 2019 10:02 am

Hi,

is the workaround really working?
the line "import app" dosn't execute the code inside app.mpy? It just imports functions inside the file, or?

I have to check that.

User avatar
jimmo
Posts: 800
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: main.mpy is not executed?

Post by jimmo » Wed Jun 19, 2019 10:10 am

Yes it will immediately execute any code at global scope in the file. Think of importing a module as just running a piece of code and if that code happens to assign any variables then they become available as attributes on that module. (And that "def foo" or "class foo" is kind of like assigning a variable named foo).

If you wanted to, you could make a function inside app.py and call that after importing it into main.py, but not strictly necessary.

zmaier
Posts: 13
Joined: Thu May 16, 2019 12:44 pm

Re: main.mpy is not executed?

Post by zmaier » Wed Jun 19, 2019 10:42 am

Ok is see, i tried it and the code will be executed.
But still a problem.
Because variables assigned in boot.py aren't available inside the imported code.

eg:
boot.py

Code: Select all

var='test'
main.py

Code: Select all

import abc
abc.py

Code: Select all

print(var)
NameError: name 'var' isn't defined

User avatar
jimmo
Posts: 800
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: main.mpy is not executed?

Post by jimmo » Wed Jun 19, 2019 10:55 am

boot.py typically just does the minimum set up for the board, not set variables to be used by the code.

I can think of two options:
- are these configuration variables? like maybe a wifi network name or something? If so, make a config.py

Code: Select all

WIFI_SSID = "hello"
then in app.py

Code: Select all

import config

def foo():
  print(config.WIFI_SSID)
- or, make a "run()" function in app.py, so that you main.py can pass these variables set by boot.py into it

Code: Select all

import app
app.run(var)

zmaier
Posts: 13
Joined: Thu May 16, 2019 12:44 pm

Re: main.mpy is not executed?

Post by zmaier » Wed Jun 19, 2019 11:39 am

ok, this is a good point.

An other thing, for example :
I init the wifi in boot.py
so the object is not visible inside app.run().
When i want to check whether wifi is connected or not with wifi.isconnected(), this is not possible, because wifi is not available?

So i have to init the wifi inside the app.run() function.

I don't get it :)

User avatar
jimmo
Posts: 800
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: main.mpy is not executed?

Post by jimmo » Wed Jun 19, 2019 12:27 pm

I guess you have three options:

- Use the same thing I described above to pass the wifi object to app.run()
- When you need to check the status, just use network.WLAN(network.STA_IF) to get the wifi object again
- Or, like you say, init the wifi in app.run()

Note that network.WLAN doesn't actually create anything, it just returns a reference to the internal object, so #2 doesn't have any issues you might imagine (i.e. it's going to give you the exact same object).

Post Reply