Minitab Statistical Software integrates with Python, a general-purpose programming language with applications in data science. To accomplish Python integration, Minitab, LLC. provides the mtbpy library. With this custom library, you can create tables, graphs, messages, and notes in Python and display them in Minitab. Python integration offers the flexibility of custom Python code within Minitab's easy-to-use interface, and the results can be saved, stored, and shared in Minitab Project Files. After you install the mtbpy package, you're ready to run Python code from Minitab. If you have Python code that you want to access routinely, you can customize Minitab's interface to make the analysis more accessible through custom buttons or menus.

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

For more information on Python, consult the guidance available at www.python.org.

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

Requirements

  • Minitab 19.2020.1 (64-bit) or higher. To check your version of Minitab, choose Help > About Minitab.
  • Python 3.6.1 (64-bit) or higher.

Install Python

Typically, the default installation of Python works with Minitab. Minitab supports Python 3.6.1 or higher. Follow the directions for your operating system at Python's website.
Microsoft® Windows
Select the option Add Python 3.x to PATH during installation. If Python is not in your PATH, then add Python to your system environment variables. Otherwise, Minitab will not be able to find Python.
macOS
Install the set of certificates according to the instructions on the Summary page of the Python installer.

Install mtbpy via PIP

To use Python with Minitab, install the mtbpy package. The mtbpy package gives you the capability to bring data from Minitab into Python and to return Python results to Minitab. To install the latest version via PIP, run the corresponding command for your operating system's terminal:
Windows
python -m pip install mtbpy
macOS
python3 -m pip install mtbpy

After Python and the mtbpy package are installed, you can use the PYSC command to run Python from Minitab.

Run in macOS

Minitab uses environment variables to run the PYSC command. In particular, Minitab uses the PATH variable to fine the Python interpreter, which Minitab expects to have the name python3.

In macOS, programs use different environments depending on how you open the program. Typically, the location of Python is not where Minitab can find Python when you open Minitab from Finder. Usually, you can open Minitab through the terminal so that Minitab can find Python. You can open Minitab from the terminal with the following command:
open /Applications/Minitab.app
/Applications/Minitab.app is the default location for Minitab in macOS. If Minitab is not in the default location, correct the location accordingly.
You can also make changes to your macOS settings so that Minitab can find Python when you open Minitab from Finder. For example, you can add a path to the PATH variable in the environment that Finder-opened apps inherit. This method adds convenience because Minitab can find Python when you open Minitab from Finder. However, this method also has some disadvantages. The method affects all applications, not only Minitab. The method also affects all users. You should use this method only if you are familiar with macOS and the resulting implications. For example, if python3 is in /usr/local/bin, you can use the following command to add that path:
sudo launchctl config user path "/usr/local/bin:$PATH"

Run in an Anaconda environment

To run Minitab in an Anaconda environment, complete the following steps:
  1. Open an Anaconda Prompt.
  2. (Optional) Activate the Anaconda environment you want to run Minitab in using the command: Activate.
  3. Copy the path to Mtb.exe. You can find the path by navigating to the Minitab install location or by copying the Target from a shortcut to Minitab.
    Tip

    The default Minitab install location on Windows is "C:\Program Files\Minitab\Minitab 19\Mtb.exe".

  4. Paste the path into the Anaconda Prompt and press Enter.

Verify the Python installation

Default folders for Python files for Minitab

Typically, Python integration with Minitab is easiest when all of the files that you use are in the default folder location for Minitab files. If you do not specify a file path when you run Python from within Minitab, Minitab looks for your Python files in the default folder.

Windows

The default location is the "My Documents" folder.

macOS
The default location is the "Documents" folder.

In the Windows operating system, if your Python file is in the default folder and you receive the error File not found:, check the default file location in your Minitab settings. To view or change the default file location in Minitab, choose File > Options > General > Default file location.

Run a test 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. For this section, you run the script without arguments. When the script runs successfully, the result is the message "Minitab successfully located your Python installation."

Put test.py in Minitab's folder for Python scripts. The default folder location depends on your operating system.

After you save the file, run the following command in the Command Line pane in Minitab:
PYSC "test.py"
Important

If the Minitab Command Line is not visible, choose View > Command Line/History.

If Python successfully works with Minitab, you will see the following output in Minitab:

Python Script

These results are from external software.
Minitab successfully located your Python installation.

Run Python scripts in Minitab

You can run Python scripts from Minitab in three 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().

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'

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']

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.
Windows

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

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

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.

By using this site you agree to the use of cookies for analytics and personalized content.  Read our policy