You can run Python scripts from Minitab in several ways:
  • Run the PYSC command in the Command Line pane.
  • Run a Minitab exec that includes the PYSC command.
  • Customize the Minitab interface to run a Minitab exec that includes the PYSC command.

The PYSC command extends Minitab's functionality with Python but does not make Minitab an interactive Python environment. As such, use Minitab only with Python scripts that complete without interaction. For example, the input() function, which accepts input from the keyboard while a script runs, does not work in Minitab. Interactivity in Python takes many other forms, such as plot.show() and keyboard.read_key().

For more information on Minitab's Python module, including Python code examples, go to Python API Reference.

All the files referenced in this guide are available in this .ZIP file: python_guide_files.zip.

Note

Install and run Python before you attempt to integrate Python with Minitab Statistical Software. For assistance with the installation of Python, please consult with your organization's technical support department. Minitab Technical Support cannot assist with the installation of Python.

Run Python scripts from the Command Line pane

You can run the PYSC command in the Command Line pane. For general information about the Command Line pane, go to Command Line/History pane. For general information about using session commands, go to Session Command help.

PYSC ["filename.py"] ["Args"...]

Runs the Python script that you specify.

The default file extension for Python scripts is .PY. If the file extension is .PY, you do not need to type the file extension.

The optional argument Args allows you to pass arguments to the Python script through sys.argv[1:]. Args can be any text values separated by a space. Enclose arguments in quotation marks. The default value is None, which means that the script does not receive any arguments.

In general, you use arguments to bring data from Minitab into Python. You can enter arguments in several ways. For example, you can use arguments that are identifiers for columns, matrices, or constants:

Minitab Session Command Value in Python Usage
PYSC "test.py" "C1"
"C1"
Use the following function to retrieve the column:
mtbpy.mtb_instance().get_column(sys.argv[1:][0])
PYSC "test.py" "M1"
"M1"
Use the following function to retrieve the matrix:
mtbpy.mtb_instance().get_matrix(sys.argv[1:][0])
PYSC "test.py" "K1"
"K1"
Use the following function to retrieve the constant:
mtbpy.mtb_instance().get_constant(sys.argv[1:][0])

You can also use arguments that are the names of columns, matrices, or constants in Minitab:

Minitab Session Command Value in Python Usage
PYSC "test.py" "My Column"
"My Column"
Use the following function to retrieve the column:
mtbpy.mtb_instance().get_column(sys.argv[1:][0])
PYSC "test.py" "My Matrix"
"My Matrix"
Use the following function to retrieve the matrix:
mtbpy.mtb_instance().get_matrix(sys.argv[1:][0])
PYSC "test.py" "My Constant"
"My Constant"
Use the following function to retrieve the constant:
mtbpy.mtb_instance().get_constant(sys.argv[1:][0])

You can also specify arguments to pass text to use in your Python code. You can pass text directly or in a constant.

Minitab Session Command Value in Python Usage
PYSC "test.py" "Text not Stored"
"Text not Stored"
This case passed a value that cannot be used with an mtbpy 'get' command. However, Args are not limited to only passing columns, matrices, and constants.
LET K1 = "Text in Constant"
PYSC "test.py" K1
"Text in Constant"
This case highlights that, although PYSC does not accept arguments that are not text values, you can pass a constant to PYSC as long as the constant is defined as a text value.

When you pass more than one argument, you can access the arguments in order from the list of arguments.

Minitab Session Command Value in Python Usage
PYSC "test.py" "C1" "C2" "M1" "K3" "10"
"C1"
"C2"
"M1"
"K3"
"10"
This case is an example of passing multiple Args, where you would access them by using the following functions in Python:
mtbpy.mtb_instance().get_column(sys.argv[1:][0])
mtbpy.mtb_instance().get_column(sys.argv[1:][1])
mtbpy.mtb_instance().get_matrix(sys.argv[1:][2])
mtbpy.mtb_instance().get_constant(sys.argv[1:][3])
int(sys.argv[1:][4])

Use the following file to see output from the example text for the following subcommands. Make sure that the test.py file is in Minitab's folder for Python scripts.
File Description
test.py A sample Python script that is used throughout this guide. When you pass arguments to the script, the results include a list of the values of the arguments. When you run the script with the argument "ArgToBePrintedToStdErr", the script writes the name of the argument to the stderr file. When you run the script with the argument "ArgToBePrintedToStdOut", the script writes the name of the argument to the stdout file. Use the subcommands that follow to control whether the contents of these files appear in Minitab's Output pane.
NOSERR
Specifies to not display text from the standard error (stderr) console output in the Output pane in Minitab. The stderr console output is where you see Python error messages when you run your code in a Python integrated development environment, although you can use Python to put other results in the stderr file. For example, by default:
PYSC "test.py" "ArgToBePrintedToStdErr".
Produces the following results that include the stderr console output:

Python Script

These results are from external software.
The following arguments were passed to Python: ['ArgToBePrintedToStdErr']

Python standard error

The following arguments were printed to Stderr: 'ArgToBePrintedToStdErr'
The following session commands exclude the stderr console output:
PYSC "test.py" "ArgToBePrintedToStdErr";
NOSERR.
The session commands produce the following results:

Python Script

These results are from external software.
The following arguments were passed to Python: ['ArgToBePrintedToStdErr']
SOUT
Specifies to display text from the standard console output (stdout) in the Output pane in Minitab. The stdout is where you would see the results of commands like print() in a Python integrated development environment. For example, by default:
PYSC "test.py" "ArgToBePrintedToStdOut".
Produces the following results that exclude the stdout:

Python Script

These results are from external software.
The following arguments were passed to Python: ['ArgToBePrintedToStdOut']
The following session commands include the stdout:
PYSC "test.py" "ArgToBePrintedToStdOut";
SOUT.
The session commands produce the following results:

Python Script

These results are from external software.
The following arguments were passed to Python: ['ArgToBePrintedToStdOut']

Python standard output

The following arguments were printed to Stdout: 'ArgToBePrintedToStdOut'

Run Python scripts from a Minitab exec file

Use the following file to perform the steps in this section:
File Description
test.py A sample Python script that is used throughout this guide. When you pass arguments to the script, the results include a list of the values of the arguments.
PYEXEC.MTB A sample Minitab exec file that includes the session commands to run the test.py script with 2 arguments.

Execs are text files that contain Minitab session commands. You can include the PYSC command that runs Python in a Minitab exec. With exec files, you can easily run commands without re-typing them, and you can assign the exec to a custom button in Minitab. For more information about Minitab execs, go to Minitab Macros Help. To run an exec, choose File > Run an Exec.

Suppose you create the exec PYEXEC.MTB. The exec file contains the following Minitab command:
PYSC "test.py" "Arg1" "Arg2"
To run the Python script with the exec, use the following steps:
  1. Choose File > Run an Exec.
  2. Click Select File.
  3. Select PYEXEC.MTB.
  4. Click Open.
The script displays the values of the arguments in Minitab, and the exec produces the following results:

Python Script

These results are from external software.
The following arguments were passed to Python: ['Arg1', 'Arg2']

Run Python scripts from the Minitab interface

If you have a Minitab exec file, you can create a custom button or menu that runs the exec. For general information on how to customize the interface in Minitab, go to Customize menus, toolbars and shortcut keys.

You can use the following steps to create a custom button that runs an exec:
  1. Choose View > Customize.
  2. Click the Tools tab.
  3. On the Tools tab, click the New (Insert) button .
  4. Type a name for the command, then press the Enter key.
  5. Click the Open button .
  6. From the file type drop-down list, select All Files (*.*).
  7. Browse to and select an exec file.
  8. Click Open.
  9. Choose View > Customize again.
  10. On the Commands tab, under Categories, select Tools.
  11. While the Customize dialog box is open, drag the new command to where you want it to appear on the Minitab menu or toolbar.
  12. Click Close.

In addition to customizing Minitab's interface, you can use a COM-compliant language to create custom dialog boxes and analyses. For information on how to customize Minitab through COM, go to Minitab Automation.

Stop the PYSC command

You can stop a Python script and keep Minitab open, which prevents the loss of any edits to your Minitab project since your last save. The method to stop a Python script depends on your operating system.
Microsoft® Windows

Press Ctrl + Alt + Delete to open the Microsoft® Windows Task Manager. Then, end the Python process.

macOS
Press Command + Option + Esc to open the Force Quit window. Then, end the Python application.