Global Variables

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Nikunj_Vadher
Posts: 14
Joined: Mon Dec 10, 2018 6:37 am

Global Variables

Post by Nikunj_Vadher » Tue Dec 18, 2018 9:27 am

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.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Global Variables

Post by kevinkk525 » Tue Dec 18, 2018 9:30 am

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.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

manseekingknowledge
Posts: 61
Joined: Sun Oct 29, 2017 5:14 pm

Re: Global Variables

Post by manseekingknowledge » Tue Sep 24, 2019 7:12 pm

Depending on your variable you can do as @stijn suggested in this post.

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

Re: Global Variables

Post by pythoncoder » Wed Sep 25, 2019 7:26 am

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.

Post Reply