Page 1 of 1

Global Variables

Posted: Tue Dec 18, 2018 9:27 am
by Nikunj_Vadher
How to declare global variables in micropython that can be accessible from boot.py, main.py and many files.
creating and writing and updating file data taking time and getting into error.I want to declare only 2 variables.
Also want to create and freeze(compile) code for python server that listen on port for request.

Thank You.

Re: Global Variables

Posted: Tue Dec 18, 2018 9:30 am
by kevinkk525
That's not possible in python, only in lua.
If you want to have accessible globals you have to declare those in a module e.g. "config.py" and then import that module in every other file/module that wants to access these.

Re: Global Variables

Posted: Tue Sep 24, 2019 7:12 pm
by manseekingknowledge
Depending on your variable you can do as @stijn suggested in this post.

Re: Global Variables

Posted: Wed Sep 25, 2019 7:26 am
by pythoncoder
If you create a variable in boot.py it will be available to main.py and to the REPL. But it won't be available to modules you import because of the way Python namespaces work: you would need explicitly to pass it to functions/methods in the module.