IDE_Modules_Teaser.jpg.png

Import External Libraries to Python Runtime

Sgilk
Contributor

Introduction

This article will explain how to package external Python libraries in the active configuration, in order to utilize them with the Python runtime application. Two methods will be provided in case the IDE application is not available.

Download External Library Source Code

The first step in using external libraries in the Python runtime is to download those libraries. This method will require the source Python files. The PyMySQL library will be used as an example.

Download the source code locally. Here, I will use the following command in the Git command line interface.

git clone https://github.com/PyMySQL/PyMySQL.git


I should now have the source code on my local machine.

Git CloneGit Clone

IDE Method
Upload Library Directly to IDE

If the IDE application is installed, the library can be copied into the textual editor explorer. The library can be copied into the /scripts/libraries/bosch directory which exists on the import path. If you'd like to separate your external libraries from the default ones, you can create a new directory via the IDE. Right click the directory and select "Upload Files...". Select the directory containing your Python library source files. Dragging and dropping from an open File Explorer works here as well.

Upload files IDEUpload files IDE

No IDE Method
Download Active Configuration

First, download the active ctrlX OS configuration to preserve current functionality. The external Python files will be added to the configuration to make them available. 

First, select the "Manage App Data" button.

Manage App DataManage App Data

 Next, select the ellipsis to reveal menu options related to the active configuration. Select the "Download" option to download the active configuration as a zipped file.

Download ConfigurationDownload Configuration

 Unzip the downloaded configuration to proceed with the next step.

Update Configuration with External Libraries

Copy the Python source files from your external library into the configuration archive. If you don't mind the libraries existing in the same location as the default, you can copy them into /scripts/libraries/bosch. This will allow you to import them immediately as this location is on the import path already. If you want to separate the external libraries from the default, you can add them to a new directory. In this example, I will use /scripts/libraries/external.

Using the PyMySQL library, copy the following files into /scripts/libraries/external/PyMySQL.

Python source filesPython source files

Upload New Configuration

Once the library has been packaged in the configuration, upload the configuration archive to the ctrlX OS.

Upload configurationUpload configuration

Import Library in Python Script

After the new configuration has been uploaded, or the libraries copied directly into the active configuration via the IDE application, the libraries can be imported in the Python script. If you placed the new libraries in the /scripts/libraries/bosch directory, no further action is needed. The imports should be found immediately. If you placed the new libraries in a separate directory, add the code below to the top of your Python script to add the new location to the Python import path. 

import sys 
# append external path
sys.path.append(
'/var/snap/rexrothide/common/solutions/activeConfiguration/scripts/libraries/external')


At this point, the libraries should import correctly, and may be utilized in the Python script.

Execute Python scriptExecute Python script

Sgilk
Sgilk
Hello, my name is Sam and I am a Software Applications Engineer supporting ctrlX Automation. Feel free to reach out if you have any questions!
Must Read
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist