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 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 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 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 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 files
Upload New Configuration
Once the library has been packaged in the configuration, upload the configuration archive to the ctrlX OS.
Upload 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 script
... View more