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.
Global Variables
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Global Variables
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.
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.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
-
- Posts: 61
- Joined: Sun Oct 29, 2017 5:14 pm
Re: Global Variables
Depending on your variable you can do as @stijn suggested in this post.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Global Variables
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.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.