Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python scripts: Use PDM and virtualenv to manage dependencies #23198

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

shoogle
Copy link
Contributor

@shoogle shoogle commented Jun 11, 2024

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

  1. Install Pipx (e.g. brew install pipx).
  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 file (added by this PR).

Running scripts

Simply run the Python scripts as you normally would. E.g.

share/instruments/update_instruments_xml.py

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 of pip install <package>.

This installs the package to your own virtual environment and also modifies the pyproject.toml and pdm.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:

pip install --user <package> --break-system-packages

The boilerplate code will load these modules if no virtual environment is detected.

shoogle added a commit to shoogle/MuseScore that referenced this pull request Jun 11, 2024
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 musescore#23198 for details.
@shoogle shoogle marked this pull request as ready for review June 11, 2024 16:43
shoogle added a commit to shoogle/MuseScore that referenced this pull request Jun 13, 2024
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 musescore#23198 for details.
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 musescore#23198 for details.
@shoogle
Copy link
Contributor Author

shoogle commented Jun 19, 2024

@shoogle shoogle merged commit f57c61b into musescore:master Jun 19, 2024
11 checks passed
@shoogle shoogle deleted the python-pdm branch June 19, 2024 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants