User’s Guide, Chapter 24: Configuring Environment Settings¶
Music21 features an environment configuration system which lets users configure and customize settings. These settings will be saved so that the next time the user starts Python, the settings will still work.
Environment configuration is particularly useful for setting default third-party applications (necessary for handling Music21 output in different media formats such as MusicXML, Lilypond, and graphics files) and for setting a default scratch directory (for writing output without providing explitic file paths).
Environment configuration can be handled with two objects. The
Environment object provides lower-level
access and control, as well as a numerous utiity methods for music21
UserSettings object is a
convenience class for users to quickly set and check settings, and is
reccommended for general usage. For complete information on the
Environment and UserSettings objects, see :mod:
Creating and Configuring the UserSettings Object¶
Environment configuration files are not created by default. To create an
environment configuration file, import environment from Music21 and
UserSettings object. Then,
create() method to
create an XML environment file.
from music21 import * us = environment.UserSettings() us.create()
After creating an environment file, the resulting XML preference file can be edited directly by the user or using the UserSettings object. The keys tell you what can be changed:
for key in sorted(us.keys()): print(key)
autoDownload braillePath debug directoryScratch graphicsPath ipythonShowFormat lilypondBackend lilypondFormat lilypondPath lilypondVersion localCorporaSettings localCorpusPath localCorpusSettings manualCoreCorpusPath midiPath musescoreDirectPNGPath musicxmlPath pdfPath showFormat vectorPath warnings writeFormat
To set and write a preference, a key and value pair must be provided using Python dictionary-like syntax. For example, to set the Music21 scratch directory, the ‘directoryScratch’ key can be set to a file path of the user’s choice. Changes are made immediately to the environment configuration file. To see the current setting, the value can be accesed by key.
>>> us['directoryScratch'] = '/_scratch' >>> us['directoryScratch'] '/_scratch'
Note that Music21 objects may need to be reloaded, and/or the Python session restarted, to have changes made to the UserSettings object take effect.
Location of Environment Configuration Files¶
Environment configuration files are stored in platform-specific locations.
On Linux and MacOS computers, the configuration file is stored as the file .music21rc, located in the user’s home directory.
On Windows computers the configuration file is generally located in the Application Data directory as the file ‘music21-settings.xml’. In some cases the XML settings file may be stored in the user directory.
The path to the environment settings file can always be found with the
us = environment.UserSettings() us.getSettingsPath()
To permanently delete the environment configuration file, call the
us = environment.UserSettings() us.delete()
Important Tools that May Use Environment Settings¶
The following important functions and methods will make use of environment configuration file and are important to properly configure.
show() Methods and ‘directoryScratch’, ‘showFormat’ and ‘writeFormat’¶
The show method, inherited from
show(), will, depending on user
settings, write a temporary file in a user specified format in a
user-specified scratch directory.
showFormat key will set the default output format of all
show() methods. The behavior can be deviated from by
providing an argument to
writeFormat key will set the default output format of
all calls to
write() methods. The behavior can be deviated from by
providing an argument to
directoryScratch key will determine where the file is
written. If this setting is not made, the file will be written in a
system-specified scratch directory. While useful, such temporary files
and directories may be buried deeply in your file system.
parse() Functions and ‘autoDownload’¶
Users may configure the ‘autoDownload’ key to determine whether downloading is attempted automatically without prompting the user (‘allow’), whether the user is asked first before attempting a download (‘ask’), or whether downloading is prohibited (‘deny’).