Skip to content

Commit

Permalink
Python scripts: Use PDM and virtualenv to manage dependencies
Browse files Browse the repository at this point in the history
PDM and virtualenv are tools to manage installation of Python modules
not included in the Python Standard Library. Usage:

1. Install Pipx (e.g. `brew install pipx`) - https://pipx.pypa.io/
2. Use Pipx to install PDM (`pipx install pdm`).
3. Run `pdm install` in MuseScore's root directory.

The final command creates a virtual environment (.venv folder) and
installs the dependencies mentioned in MuseScore's pyproject.toml.

See #23198 for details.
  • Loading branch information
shoogle committed Jun 11, 2024
1 parent 3ca965e commit 7d44a24
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ VERSION
temp_*

*.local.cmake

# Python
__pycache__/
*.pyc
.venv/
.pdm-python
129 changes: 129 additions & 0 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[project]
description = "Python scripts for MuseScore Studio development."
readme = "https://github.com/musescore/MuseScore/wiki"
requires-python = ">=3.9"
dependencies = [
"requests>=2.32.3",
]

# Useful PDM commands:
# --------------------
# pdm install Install all dependencies into a virtual environment (venv).
# pdm run <script> Run a Python script with access to venv.
# pdm add <package> Add new dependency on a PyPi package.
# pdm venv remove in-project Remove the venv (e.g. to test installation from scratch).
[tool.pdm]
distribution = false
19 changes: 19 additions & 0 deletions share/instruments/update_instruments_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@
def eprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)

if __name__ == '__main__' and sys.prefix == sys.base_prefix:
# Not running inside a virtual environment. Let's try to load one.
import os
old_dir = os.path.realpath(__file__)
new_dir, script_name = os.path.split(old_dir)
rel_pyi = r'.venv\Scripts\python.exe' if sys.platform == 'win32' else '.venv/bin/python'
while new_dir != old_dir:
abs_pyi = os.path.join(new_dir, rel_pyi)
if os.access(abs_pyi, os.X_OK):
eprint(f'{script_name}: Loading virtual environment:\n {abs_pyi}')
if sys.platform == 'win32':
import subprocess
raise SystemExit(subprocess.run([abs_pyi, *sys.argv]).returncode)
os.execl(abs_pyi, abs_pyi, *sys.argv)
old_dir = new_dir
new_dir = os.path.dirname(new_dir)
eprint(f'{script_name}: Not running inside a virtual environment.')
del old_dir, new_dir, rel_pyi, abs_pyi, script_name

import locale
if locale.getpreferredencoding().lower() != 'utf-8':
encoding = locale.getpreferredencoding()
Expand Down

0 comments on commit 7d44a24

Please sign in to comment.