2.17 Working with Python


Origin provides embedded python environment, so that you can either run python in Origin (support both command line and .py file), or use a PyOrigin module to access Origin from Python.

The embedded Python in Origin could be either version 3.3.5 or version 2.7.8. By default, it is Python version 3.3 running in Origin. To switch to use Python version 2.7, open Script Window and select menu Edit: Script Execution: Python 2.7. It can be also switched by a system variable @PYV. When @PYV = 3 (Default), the embedded Python is 3.3.5, while if @PYV = 2, it is 2.7.8. To set value for a system variable, click Tools:System Variables to open the Set System Variables dialog.

The following document page is written based on the assumption that Python 3.3.5 is used in Origin.

Minimum Origin Version Required: 2015 SR0

Run Python in Origin

In Origin, you can run Python command line, or Python files (.py extension). Additionally, you could evaluate a Python expression in Origin as well.

Run Python Command Line

To run python command line in Origin, there are a couple of ways:

Use the embedded Python enviroment

  • In the Script Window, make sure Edit:Script Execution:Python 3.3 is selected, then highlight the Python command line and hit Enter key to directly execute the command.
Run Python in Script Window.png

Use the run –py command option switch

  • Use the run -py command option switch in LabTalk, for example, make sure Edit:Script Execution:LabTalk is selected and run the following script:
// Define the Python command line(s) as a LabTalk string variable
str$ = "a = 2 ** 8;print(a)";
// Execute Python command lines
run -py %(str$); 

// ** is the Exponent operator in Python
//It should return 256, i.e. 2 to the power 8

Use the run.python() method

  • Use the run.python() object method in LabTalk, for example, also make sure Edit:Script Execution:LabTalk is selected and run the following script:
run.python(myname='Origin';print(myname));
//Should output 'Origin'

Use the LabTalk object Python

We can use LabTalk object Python to execute Python commands and pass variables between LabTalk and Python.

  1. Initialize Python
    Before running Python command in Labtalk, you can initialize the Python by running:
    if(Python.Init()<0)
    {
        type -b " Initialize failed. ";
        return;
     }
  2. Execute Python command
    // ** is the Exponent operator in Python
    //It should output256, i.e. 2 to the power 8
    Python.Exec("a = 2 ** 8; print(a)");
  3. Send Origin DataSet to Python
    //Active a workbook
    Python.Send("1!1", arr1); // Send Column 1 in Sheet 1 current workbook to Python object arr1
    Python.Exec(“print(arr1));
  4. Receive Python object as Origin DataSet
    Define a list in Python Console:
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];

    You can receive the list as Origin DataSet:

    Python.Receive("1!1",days); //Receive the list days to Column1 in Sheet 1 of current workbook
  5. Pass Variables between Labtalk and Python
    Python objects have four methods: GetReal, SetReal, GetStr, and SetStr, which are used to pass numeric and string variables between Labtalk and Python, for example:
    You can pass value to LabTalk variable in this way:
    Python.GetReal(LTVar, 10%4);
    Python.GetStr(LTStr$, Origin);

    Or define a numeric or string variable in LabTalk, and then pass the value to Python object py in this way:

    Python.SetReal(LTVar, py)
    Python.SetStr(LTStr$, py)


To execute multiple Python commands or a Python module in the Script Window, set the system variable @PYI to 0 to turn off interactive mode. Choose Tools:System Variables to open the Set System Variables dialog, enter Variable = pyi and Value = 0.

Run Python File

In order to run a Python file (with .py extension) in Origin, use either of the following LabTalk command/object:

  • Use the run -pyf command option switch in LabTalk, for example, make sure Edit:Script Execution:LabTalk is selected and run the following script:
// Define a string for the file path and name of the .py file
string str$ = system.PATH.PROGRAM$ + "\Samples\Python\ListMember.py";
// Run the .py file
run -pyf "%(str$)";  

// This .py file list all provided functions in PyOrigin module

OR

  • Use the run -pyp command option switch to execute a Python file attached to the Origin project file.

OR

  • Use the run.python() object method in LabTalk, for example, make sure Edit:Script Execution:LabTalk is selected and run the following script:
// Define a string for the file path
string file_path$ = system.PATH.PROGRAM$ + "\Samples\Python\";
// Set path of the python file
run.python("%(file_path$)", 32);
// Run the .py file named as ListMember.py under the selected path
run.python("ListMember.py", 2);

// This .py file list all provided functions in PyOrigin module

Evaluate Python Expression

You can also evaluate a Python expression using either of the following LabTalk command/object method

  • Use run -pye command option switch, for example, make sure Edit:Script Execution:LabTalk is selected and run the following script:
run -pye 10 % 4;

// It evaluates the Python expression 10 % 4, % is the Modulus operator in Python
// It outputs the evaluated value 2, which is the remainder of 10 divided by 4

OR

  • Use the run.python() object method in LabTalk, for example, also make sure Edit:Script Execution:LabTalk is selected and run the following script:
run.python("10 % 4", 1);

// It also returns the evaluated value 2, which is the same as the example above

Access Origin in Python

In your Python code, you can import the PyOrigin module and make use of the functions it provided to access Origin object.

Import PyOrigin Module

To import the PyOrigin module, make sure to have the following command line in your .py file:

import PyOrigin

Note that PyOrigin module is supposed to be used for the interaction of the current Origin session and Python extension, so the .py files with PyOrigin module imported can only be called from Origin (See the above section of Run Python File in Origin), i.e. such .py files cannot be executed directly from an external Python editor. (e.g. IDLE)

Inspect Functions Provided by PyOrigin

In order to inspect all the functions/global variable and classes/member functions accessible from embedded Python, you can run the following LabTalk script in the Script Window:

//make sure the Script Execution Mode is LabTalk
string str$ = system.PATH.PROGRAM$ + "\Samples\Python\ListMember.py";
run -pyf "%(str$)";

The functions/classes/global variables provided by the PyOrigin module are very similar to their counterpart in Origin, so you can refer to the Origin C Reference for detailed usage. (Help:Programming:Origin C and go to Origin C Reference Chapter)

Examples to Working with Python in Origin

Several built-in Python examples are provided under Origin sample folder(<Origin EXE Folder>\Samples\Python) in order to show how to work with Python in Origin, including how to send data to worksheets, import data and plot, export graphs and publish graphs on a web page, etc. The example *.py files can be run directly by some labtalk commands, and Origin example project files with Python files attached can be run by clicking the customized buttons.

Run Python File and Send Data From Python to Origin

The Python file SendDataToWorksheet.py under <Origin EXE Folder>\Samples\Python is an example to show how to send data from Python to Origin, it created a dialog using Python tkinter module to get user input such as column format, number of rows, etc. Then these inputted parameters will be passed to Origin.

In order to run this .py file, execute the following LabTalk script in the Script Window:

//Make sure the Script Execution Mode is LabTalk
//Make sure a worksheet is active in Origin
string str$ = system.PATH.PROGRAM$ + "\Samples\Python\SendDataToWorksheet.py";
run -pyf "%(str$)";


Run and View Python File Attached to Origin Project

The example ImportMatrixPlotContour.opj under <Origin EXE Folder>\Samples\Python shows how to import a matrix into Origin and plot it as a contour graph. It also shows the concept of attaching Python files to Origin projects and execute.

To use this example, select File:Open to open the project file in Origin, you will see a workbook Control Panel with two embedded buttons. Click the Import Matrix and Plot Contour button to run the attached Python file, this is equivalent to running the following LabTalk script in Script Window:

run -pyp ImportMatrixPlotContour.py;

You can also click the Show Python Code button to view the attached Python file in Origin's Code Builder. This is equivalent to the following LabTalk script:

%z = system.path.projectAttachedFilesPath$;
ed.open(%zImportMatrixPlotContour.py);

Alternatively, you could open the Code Builder (View: Code Builder or Alt + 4), and browse to the Project folder in Workspace panel to access the attached Python file.

To attach your Python files to an Origin project, right click on the Project folder and select Add Files... to include your Python file(s).

A Note to Use Python Extensions

The embedded Python in Origin includes all the basic modules and the modules which are installed to your Python application. If you want to use other Python extensions, e.g. PyQt4, numpy, scipy, follow the procedure below:

  1. Make sure the downloaded Python extension version is either version 3.3 or version 2.7 to be compatible with the embedded Python version in Origin.
  2. Make sure the downloaded Python extension corresponding to same CPU register size as the Origin being running - 32 bit or 64 bit. To know whether current Origin verison is 32 bit or 64 bit, you can directly read it from Origin program window title bar or type command " system.bits=;" in Command Window or Script Window.
  3. Make sure the installed Python extension path is properly appended so it can be directly used by Python import command.
  4. Make sure current Python running version is compatible with downloaded Python extension. By default, it is Python version 3.3 running in Origin. To switch to use Python version 2.7, open Script Window and select menu Edit: Script Execution: Python 2.7.


If the Python extension is installed in default path, such as C:\Program Files (x86)\Python\Lib\site-packages\, you can directly use the package as shown in example below:

# For Python extension version 3.3 (default)

import numpy

x = numpy.array([[1, 2, 3], [4, 5, 6]], numpy.int32)

print(x)

Otherwise, you have to add its installation folder to the system path list before you import it. To add the installation folder to system path list, the recommended approach is to run the following Python statements in the embedded Python environment of Origin (either directly run statements, or run from files):

import sys

# Replace the value of string variable py_ext_path 
# with the actual Python extension installation path in your computer

py_ext_path = "C:\Python33\Lib\site-packages"

if py_ext_path not in sys.path:
    sys.path.append(py_ext_path)

And after you have executed these Python statements, you will be able to access the Python extension in the current Origin session using for example:

import PyQt4

But once you closed the current Origin session, you will need to add the Python extension path to sys.path list once again.

For example purpose, you can click desired version folder and find the compatible packages to download in links Numpy and Scipy.