Python scripts: Use PDM and virtualenv to manage dependencies #23198
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Python 3.12 and later prevents users from installing packages system-wide with Pip, as this can create "dependency hell" when different packages depend on different versions of other packages.
The recommended solution is to run each Python program/library inside its own virtual environment that includes only the specific packages needed for that program/library.
PDM and virtualenv are tools to manage virtual environments and install packages within them. (See comparison to similar tools.)
Set up
brew install pipx
).pipx install pdm
).pdm install
in MuseScore's root directory.The final command creates a virtual environment (
.venv
folder) and installs the dependencies mentioned in MuseScore'spyproject.toml
file (added by this PR).Running scripts
Simply run the Python scripts as you normally would. E.g.
Boilerplate code is added to the top of scripts that need to use modules from the virtual environment. (This is only necessary for scripts that use modules not present in Python's Standard Library, such as the Requests module).
Adding new dependencies
Basically, use
pdm add <package>
instead ofpip install <package>
.This installs the package to your own virtual environment and also modifies the
pyproject.toml
andpdm.lock
files in the repository. You should commit the changes to those files.Other developers can pick up the new dependency by running
pdm install
again on their machine.If writing a new script, you should also add the boilerplate code from
update_instruments_xml.py
to ensure the virtual environment gets loaded.Installing packages with Pip (not recommended)
If you don't want to use virtual environments, you can ignore this PR and use:
The boilerplate code will load these modules if no virtual environment is detected.