2 Installing and Managing Python Packages

Once you start working with embedded Python in Origin, you will sooner or later need to install specific Python packages such as numpy, scipy etc.

You have two options for installing and managing packages, and these are outlined in the following sections.

NOTE: It is good practice to restart Origin after installing packages. This can resolve any issues such as lack of immediate access to the package after installation.

For those without an internet connection and/or who have a WHL file on the file system, see Installing Wheel Files below.

Using the Python Packages Tool

A GUI tool named Python Packages is available for installing and managing packages. This tool is accessible from the Origin menu: Connectivity: Python Packages... and also from the Code Builder menu: Tools: Python Packages....

PythonPackagesDialog.png

The tool provides options to install, upgrade and uninstall packages. The Pause output window check box allows to keep the Command window open to monitor progress.

Using LabTalk Script

Python packages can be installed and managed from LabTalk script using the pip command. The easiest way is to run the command from the Script Window (Window: Script Window).

  • Note that when executing a single line of script in the Script Window, you do not include the semi-colon (;) at the end of the line.
  • To execute multiple lines of script, make sure all (except comments) have a terminating semi-colon (;), highlight all lines to be executed, then press Enter.
// Install pandas package:
pip install pandas;

All dependent packages for pandas will be automatically installed, such as NumPy, six, python-dateutil, and pytz.

Use the -chk option to check if package is already installed and is up to date.

pip -chk pandas;   // Asks for permission to install, otherwise reports already installed
pip -chk1 pandas;  // Shows message if not installed, otherwise does nothing

Multiple package names can be specified in the command:

pip -chk scipy pandas;  // Dialog will show only those packages that are not yet installed

Packages can be uninstalled from script. Note that there is no confirmation dialog.

pip uninstall pandas; 
pip uninstall *; // Uninstall all packages

You can also check for installed packages using the Python Object in LabTalk:

i=Python.chk("pandas cv2(opencv-python)");

This method will return:

    • 0: All packages are available
    • 1: Installation is needed and was initiated
    • 2. Embedded Python not installed
    • 3. No internet connection or user refused to install

This method is useful to check for packages when creating Apps that utilize Python script. To learn more, please review App Development using Python.

Installing Wheel Files

For those without an internet connection and/or who have a WHL file on the file system, please follow these steps to install the file:

  1. In Origin, open the Command Window from the main Window menu.
  2. Type in the following command, replacing the path to the WHL file and enclosing the path in double quotes:
pip install "C:\Path\To\Wheels\anywheel.whl";

If the Python package has dependencies, pip will try to download them from the internet during installation. If your computer lacks internet access, the installation will fail. In such cases, use the "no-dependencies" switch when running the install.

pip install "C:\Path\To\Wheels\anywheel.whl" --no-dependencies;

Install each dependency separately using the same approach.

Old Way to Use External Python Packages

import sys
//when the Python extension is not installed in default path,
//you have to add its installation folder to the system path list before you import it
py_ext_path = r"C:\Users\bob\AppData\Local\Programs\Python\Python38\Lib\site-packages"
if py_ext_path not in sys.path:
    sys.path.append(py_ext_path)
import numpy as np
print(np.__file__)

Use Proxy Setting when Install Python Package

Python Packages supports proxy setting. Open Preferences: Options dialog, go to Miscellenous tab, choose Use HTTP Proxy Server for Proxy, enter your proxy address. Then when install package with Python Packages Manager, it will install package from Proxy Server. Or you can do it with LabTalk script:

pip install --proxy http:%(char(47)$)%(char(47)$)user:password@proxy:port --trusted-host pypi.org --trusted-host files.pythonhosted.org <pkg>

In above script, %(char(47)$) replaces / in proxy URL because, when run via LT, the parser will think // is a comment and will break the LT call. All special character need to replace with Percent-encoding, like %24 for $.