Python Interface

The Python package allows you to access the functionalities of the NxLib via Python. The EnsensoSDK includes the source code of the corresponding Python interface package. We additionally release the package on PyPI. In case you have several NxLib versions installed, you can conveniently install the corresponding Python interface in a virtual environment for a specific SDK version.

Prerequisites

  • An installed EnsensoSDK

  • Python 3.6 or newer

  • Python package manager pip

Installation

The package either ships with the EnsensoSDK or can be installed with pip3 from PyPI.

Installing from EnsensoSDK

The EnsensoSDK installation includes the Python interface source code, which can be found in your installation directory under $ENSENSO_INSTALL/development/python. Installing the package from there can be achieved as follows:

# For all users.
pip3 install $ENSENSO_INSTALL/development/python
# Or just for the current user.
pip3 install --user $ENSENSO_INSTALL/development/python

Installing from PyPI

Since the integration of the Python interface into EnsensoSDK, each new SDK release automatically creates and uploads a PyPI package with the same version number as the SDK. This allows to easily find and install the corresponding Python interface version.

pip3 install nxlib==<SDK-version-number>
# If you prefer the old package name (or do not want to change dependencies)
pip3 install ensenso_nxlib==<SDK-version-number>

In case you have several versions of the EnsensoSDK installed and you also want to have a matching Python interface for each of them, you can create a virtual environment for each SDK version like this:

# Example for fictional nxlib version 3.3.42
cd ~
mkdir ensenso
cd ensenso
python3 -m venv .nxlib-3.3.42
source .nxlib-3.3.42/bin/activate
pip3 install nxlib==3.3.42

Getting started

After a successful installation you should be able to import the Python interface (e.g. in an interpreter session):

import nxlib
# Or with the old package name
import ensenso_nxlib

The Python interface package nxlib consists of the following modules:

  • nxlib.api - Wraps the global NxLib functions

  • nxlib.constants - Contains the NxLib constants provided by the corresponding SDK version

  • nxlib.item - Contains the NxLibItem class implementation

  • nxlib.command - Contains the NxLibCommand class implementation

  • nxlib.exception - Contains the NxLibException class implementation

  • nxlib.context - Contanis context managers which wrap API functions

  • nxlib.log - Contains NxLog class implementation

The classes implemented in the described modules can be imported directly from the nxlib package without specifying the complete path via the following shortcut:

# Standard import
from nxlib.item import NxLibItem,
from nxlib.command import NxLibCommand
from nxlib.exception import NxLibException
# Shortcut
from nxlib import NxLibItem, NxLibCommand, NxLibException

It is helpful and recommended to import the constants module since your IDE will then know all the constants definitions and will on the one hand not display any warnings and on the other hand provide code suggestions for you.

# Import all constants
from nxlib.constants import *

Usually import * should be avoided. In this case however, it is unlikely that the constants defined by nxlib have the same name as other variables in your project. Otherwise you will have to access them within a namespace like the following:

# Import all constants with namespace
import nxlib.constants as consts

# Call them in your code with the defined namespace
consts.ITM_NXLIB_CONSTANT

To get further insight into how the Python interface works, see the provided Examples.