3 Running Python Code

The following sections document various ways to store and execute Python code in Origin.

From Python Console

The Python Console is ideal if you have just a few lines of Python code to execute. Intellisense is supported in the console, so it is also very useful to explore objects and methods available in the originpro package.

Use the Connectivity: Python Console... menu in Origin to open Python Console window.

PythonConsoleIntens1.png

From Code Builder

You can develop your Python code using Code Builder. Code highlighting, intellisense, and debugging are supported.

CodeBuilder Python.png

Use the Connectivity: Open utitled.py... menu in Origin to open Code Builder. Once you have edited your code, you can run the code by pressing the F5 key. It is not even necessary to save the code to a file prior to executing. The Tools menu in Code Builder has three entries specific for Python:

  • Python Packages: Install, update or uninstall packages
  • Intellisense: Turn intellisense on/off
  • Clean Python Packages: Clean/remove compiled PYC files in User Files Folder


From External .py File

You can run code saved in a .py file from anywhere in Origin where LabTalk Script is supported, such as the Script Window, using the Run command.

// Run code from file test.py saved in your User-Files Folder
run -pyf test.py;

// Run code from file saved in a specific folder
string str$ = "C:\temp\test.py";
run -pyf %(str$);


You can pass variables to your Python code from LabTalk script. Try the following:

  1. From Origin, use the menu Connectivity: Open untitled.py....
  2. In Code Builder, on the right side, you will see a tab named untitled.py. Paste the following Python code:
    import sys
    if __name__ == '__main__':
        print(len(sys.argv))
        print(sys.argv[1])
        print(sys.argv[2])
  3. Use the File:Save As... menu in Code Builder, and save the file as test.py to the default location, which is your User Files Folder.
  4. Go back to Origin and open the script window using the Origin menu Window: Script Window. Then paste the following lines of LabTalk script:
    double var1 = 123.456;
    string var2$ = 'hello';
    run -pyf test.py "$(var1)" "%(var2$)";
  5. Drag your mouse and select all three lines of the script code and press Enter key to execute.
  6. You should see the output:
    3
    123.456
    'hello'

From .py File Attached to Project

The .py file containing your Python code can be attached to the Origin project. Once it is attached, you can execute the code using the script command:

// Execute Python code stored in file test.py attached to project
run -pyp test.py;
// Can leave out the .py extension
run -pyp test;


To create and attach a file to the project, do the following:

  1. Edit your Pyton code in Code Builder and save it to a file with desired name.
  2. Use the Code Builder menu File: Add to Workspace. Your file will now appear under the User branch of the Workspace tree on the left.
  3. Drag and drop your file from the User branch to the Project branch in the Workspace tree.
  4. Now the file is attached to the project.
  5. Double click on this entry under the Project branch to open the code in a new tab on the right side to perform further edits.
  6. From Origin, save your project. The code file is now attached to the project and the code can be executed from anywhere in Origin where Labtalk script is supported.

From a Button

Python code can be stored in a button and gets executed on click. Check this page for details.

Calling Python Functions

Instead of running Python code, you can define Python functions and call the functions from Origin such as from Column Formula, Import Wizard and from Fitting Functions.