- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
- Introduction
- Download External Library Source Code
- IDE Method
- Upload Library Directly to IDE
- No IDE Method
- Download Active Configuration
- Update Configuration with External Libraries
- Upload New Configuration
- Import Library in Python Script
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.
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.
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.
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.
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.
Upload New Configuration
Once the library has been packaged in the configuration, upload the configuration archive to the ctrlX OS.
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.