MySQL Shell 9.1  /  ...  /  Module Search Path Variable in Startup Scripts

13.2.2 Module Search Path Variable in Startup Scripts

The sys.path variable can be customized using the MySQL Shell startup script mysqlshrc.js for JavaScript mode or mysqlshrc.py for Python mode. For more information on the startup scripts and their locations, see Section 13.1, “Working With Startup Scripts”. Using the startup script, you can append module paths directly to the sys.path variable.

Note that each startup script is only used in the relevant language mode, so the module search paths specified in mysqlshrc.js for JavaScript mode are only available in Python mode if they are also listed in mysqlshrc.py.

For Python modify the mysqlshrc.py file to append the required paths into the sys.path array:

# Import the sys module
import sys

# Append the additional module paths
sys.path.append('~/custom/python')
sys.path.append('~/other/custom/modules')

For JavaScript modify the mysqlshrc.js file to append the required paths into the sys.path array:

// Append the additional module paths
  sys.path = [...sys.path, '~/custom/js'];
  sys.path = [...sys.path, '~/other/custom/modules'];

A relative path that you append to the sys.path array is resolved relative to the current working directory.

The startup scripts are loaded when you start or restart MySQL Shell in either JavaScript or Python mode, and also the first time you change to the other one of those modes while MySQL Shell is running. After this, MySQL Shell does not search for startup scripts again, so implementing updates to a startup script requires a restart of MySQL Shell if you have already entered the relevant mode. Alternatively, you can modify the sys.path variable at runtime, in which case the require() or import function uses the new search paths immediately.