Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
KevsRobots Learning Platform
60% Percent Complete
By Kevin McAleer, 3 Minutes
In MicroPython, a package is a collection of modules that are organized in a directory structure. Packages help in organizing code and making it reusable. A package can contain sub-packages, modules, and other resources.
package
A package is a directory containing a special file named __init__.py. This file can be empty or contain initialization code for the package. The presence of the __init__.py file indicates to Python that the directory should be treated as a package.
__init__.py
Here’s an example of a simple package structure:
my_package/ __init__.py module1.py module2.py sub_package/ __init__.py module3.py
In this example, my_package is a package that contains two modules (module1.py and module2.py) and a sub-package (sub_package) which itself contains an __init__.py file and a module (module3.py).
my_package
module1.py
module2.py
sub_package
module3.py
Create a directory for your package. Inside this directory, create an __init__.py file and the modules you need. For example, create the following structure:
my_robot_package/ __init__.py motors.py sensors.py
Add the following code to motors.py:
motors.py
# motors.py class Motor: def __init__(self, power): self.power = power def move(self, direction): print(f"Moving {direction} with power {self.power}")
And add the following code to sensors.py:
sensors.py
# sensors.py class Sensor: def __init__(self, type): self.type = type def read_value(self): # Simulate reading a sensor value return 42
Create a main program file and import the package modules:
# main.py from my_robot_package.motors import Motor from my_robot_package.sensors import Sensor motor = Motor(100) motor.move("forward") sensor = Sensor("Ultrasonic") print(sensor.read_value())
In this example, you create a package my_robot_package with two modules: motors.py and sensors.py. The main program imports and uses the classes from these modules.
my_robot_package
Packages in MicroPython are directories containing modules and an __init__.py file. They help organize and reuse code by grouping related modules together. By creating and using packages, you can improve the structure, maintainability, and reusability of your code.
< Previous Next >