1 Run Python in Origin


Origin provides an embedded Python environment, so that you can run Python in Origin (supports both command line and .py file), and 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 use Python version 2.7, open the Script Window and click Edit: Script Execution: Python 2.7. The version can also be changed using 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 the value for a system variable, click Preferences: System Variables to open the Set System Variables dialog.

The following documentation assumes that Python 3.3.5 is used in Origin.


Run Python in Origin

In Origin, you can run Python from the command line, or run Python files (.py extension).

Run Python at the Command Line

To run Python at the command line in Origin, use any of the following approaches:

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

OR

  • 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

OR

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

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 Preferences: System Variables to open the Set System Variables dialog, enter Variable = pyi and enter a Value = 0.

Run Python at the Python Console

To open the Python Console: Select Connectivity: Python Console in Origin interface.
You can execute Python code directly in Python console. The Python version embeded is controled by 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 the value for a system variable, click Preferences: System Variables to open the Set System Variables dialog.

PythonConsole.png

Run Python from a File

To run a Python file (with .py extension) in Origin, use either of the following LabTalk commands/objects:

  • 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. For example
//make sure '''ImportMatrixPlotContour.opj''' is opened in Origin (in folder''<Origin EXE Folder>\Samples\Python'')
run -pyp ImportMatrixPlotContour.py;

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

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

Evaluate a Python Expression

You can also evaluate a Python expression using either of the following LabTalk commands/object methods

  • Use the 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, 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

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 corresponds to the same CPU register size as the Origin being run - 32 bit or 64 bit. To know whether your current Origin version is 32 bit or 64 bit, look to the Origin program window title bar or type the command "system.bits=;" in the Command Window or Script Window.
  3. Make sure the installed Python extension path is properly appended so it can be directly used by the Python import command.
  4. Make sure the Python version that is running is compatible with the downloaded Python extension. By default, it is Python version 3.3. To switch to Python version 2.7, open the Script Window and select menu Edit: Script Execution: Python 2.7.


If the Python extension is installed in the 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

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

Starting from Origin 2017, Saving the Python extension path to Windows system environment variable PYTHONPATH can have Origin adding it automatically when launching. Go to System: Advanced system settings: Environment Variables.... In the lower panel for System variables, add "C:\Python33\Lib\site-packages" to PYTHONPATH.(If the variable is not existing, create one.) Click OK to apply the setting.

For examples, go to the SourceForge pages Numpy and Scipy and find version-compatible plug-ins.