114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
54% Percent Complete
By Kevin McAleer, 4 Minutes
In MicroPython, a module is a file that contains Python code, including functions, classes, and variables. A library is a collection of modules that provide additional functionality for your projects.
module
library
A module is a file containing Python code that defines functions, classes, and variables. Modules help in organizing code and making it reusable. You can import a module into your MicroPython program to use its functions and classes.
Here’s an example of a simple module named my_module.py:
my_module.py
# my_module.py def greet(name): return f"Hello, {name}!" class Calculator: def add(self, x, y): return x + y PI = 3.14159
You can use this module in your main program by importing it:
# main.py import my_module print(my_module.greet("Alice")) calc = my_module.Calculator() print(calc.add(5, 3)) print(my_module.PI)
In this example, my_module.py contains a function, a class, and a variable. By importing my_module in main.py, you can access and use these elements.
my_module
main.py
A library is a collection of modules that provide additional functionality to your MicroPython projects. Libraries extend the capabilities of your code by providing pre-written functions and classes.
MicroPython includes several built-in libraries you can use. For example, the math library offers mathematical functions:
math
import math print(math.sqrt(16)) # Outputs: 4.0 print(math.pi) # Outputs: 3.141592653589793
You can also install external libraries to add more features to your projects. This is often done using a package manager like upip.
upip
For instance, to install the urequests library for making HTTP requests, you can use:
urequests
import upip upip.install('micropython-urequests')
Then, you can use the installed library in your program:
import urequests response = urequests.get('http://example.com') print(response.text)
Creating your own modules helps you organize your code and make it reusable across different projects. Here’s how you can create and use your own module:
Create a new file named my_robot.py with the following content:
my_robot.py
# my_robot.py class Robot: def __init__(self, name): self.name = name def greet(self): return f"Hello, I am {self.name}"
In your main program, import and use the my_robot module:
my_robot
# main.py import my_robot r1 = my_robot.Robot("Robo") print(r1.greet())
In this example, you create a module my_robot.py that defines a Robot class. In main.py, you import and use the Robot class from the my_robot module.
Robot
MicroPython includes the mip module, which can install packages from micropython-lib and third-party sites like GitHub and GitLab. The mip tool is similar to Python’s pip but uses micropython-lib as its default index. It also automatically fetches compiled .mpy files when downloading from micropython-lib.
mip
You can use mip from the REPL to install packages:
import mip # Install the latest version of "pkgname" (and dependencies) mip.install("pkgname") # Install a specific version of "pkgname" mip.install("pkgname", version="x.y") # Install the source version (i.e., .py rather than .mpy files) mip.install("pkgname", mpy=False)
Modules and libraries in MicroPython enhance your projects by providing reusable and organized code. Whether you are using built-in libraries, installing external ones, or creating your own modules, they help you build more efficient and maintainable programs.
< Previous Next >