Python - Imports | MUSE
In this video, you’ll learn how to properly manage imports when writing Python code for an AMX MUSE controller. We’ll cover why the mojo library and context import are essential for interacting with the system, along with how to work with built-in and external libraries. You’ll also see how to resolve IDE warnings by pulling program type definitions directly from the controller. By the end, you’ll understand how to structure your imports for clean, reliable, and scalable MUSE programming.
What You’ll Learn:
• Understanding the role of imports in MUSE Python programs
• Using the mojo library and context for system interaction
• Managing built-in vs. external Python libraries
• Resolving IDE warnings with controller-provided type definitions
• Organizing imports for readability and scalability
• Best practices for maintaining clean and reliable code
Hello, and welcome to this video on using imports in Python on your MUSE controller. One of the benefits of using Python is the vast number of libraries that exist in the world to make your coding easier. In this video we will discuss importing the mojo package and context class, rules to using libraries, and how to incorporate non-standard libraries into your code.
Let’s start this video by looking at the default Python script that is generated when you create a MUSE local program.
The first line of any Python script that directly interfaces with the MUSE controller must begin with the line “from mojo import context”. This import incorporates everything that “is” a MUSE controller. Without this, your code couldn’t communicate with the MUSE controller or utilize the MUSE controller to communicate with Harman products or other 3rd party devices.
If you notice, you receive a warning when referencing mojo. This is because the mojo package does not exist in the language server that is referenced in the IDE. The only place mojo exists is on the MUSE controller itself. Keep in mind, this is just a warning, and it will not keep your script from running once it is loaded to the controller.
If you want to get rid of the warning, you can download the program type definitions from the MUSE controller so the IDE can better understand the mojo package and context.
While connected to your controller, right-click on the empty space of the file explorer and select “Fetch program type definitions”. At the top of the screen, it will ask you to select the controller you would like to fetch them from. Click on the controller from the list, and the download will begin.
Notice that a new folder named mojo has been created in your program folder, and the warning that was in your code has been removed. The mojo folder contains a Python interface file named __init__. This file is created at runtime and contains the definitions for all the devices, scripts, drivers, and extensions the muse controller is aware of at the time of the download, so the file can be very large. This file will be used as a reference by the language server. It is important to remember that this file should not be edited.
When it comes to importing other classes and libraries, you should know that the currently installed version of Python on the MUSE controller is version 3.1. This means that any built-in functions of the language that exist in the Python Standard Library for that version can be imported into your script. This includes things like urllib, json, math, and datetime.
Any libraries that exist outside of the Python Standard Library can still be used if they are supported at that version. The Python file you are importing should be saved in your program folder and loaded to the controller along with your main Python script. Keep in mind, your imported library can only contain Python files. Libraries that require dependencies built using C, C++, or other languages cannot be imported in this way.
More complicated libraries can be added to your scripts using Python Virtualization. We will discuss this process in another video.