MySQL Shell 8.0  /  Customizing MySQL Shell  /  Adding Module Search Paths

10.2 Adding Module Search Paths

When you use the require() function in JavaScript or the import function in Python, the well-known module search paths listed for the sys.path variable are used to search for the specified module. MySQL Shell initializes the sys.path variable to contain the following module search paths:

  • The folders specified by the module search path environment variable (MYSQLSH_JS_MODULE_PATH in JavaScript mode, or PYTHONPATH in Python mode).

  • For JavaScript, the subfolder share/mysqlsh/modules/js of the MySQL Shell home folder, or the subfolder /modules/js of the folder containing the mysqlsh binary, if the home folder is not present.

  • For Python, installation-dependent default paths, as for Python's standard import machinery.

MySQL Shell can also load the built-in modules mysql and mysqlx using the require() or import function, and these modules do not need to be specified using the sys.path variable.

For JavaScript mode, MySQL Shell loads the first module found in the specified location that is (in order of preference) a file with the specified name, or a file with the specified name plus the file extension .js, or an init.js file contained in a folder with the specified name. For Python mode, Python's standard import machinery is used to load all modules for MySQL Shell.

For JavaScript mode, from MySQL Shell 8.0.19, MySQL Shell also provides support for loading of local modules by the require() function. If you specify the module name or path prefixed with ./ or ../, in batch mode, MySQL Shell searches for the specified module in the folder that contains the JavaScript file or module currently being executed. In interactive mode, given one of those prefixes, MySQL Shell searches in the current working directory. If the module is not found in that folder, MySQL Shell proceeds to check the well-known module search paths specified by the sys.path variable.

You can add further well-known module search paths to the sys.path variable either by appending them to the module search path environment variable for JavaScript mode or Python mode (see Section 10.2.1, “Module Search Path Environment Variables”), or by appending them directly to the sys.path variable using the MySQL Shell startup script for JavaScript mode or Python mode (see Section 10.2.2, “Module Search Path Variable in Startup Scripts”). You can also modify the sys.path variable at runtime, which changes the behavior of the require() or import function immediately.