Python Interface¶
The Python® package allows you to access the functionalities of the NxLib via the 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
:: For all users.
pip3 install "%ENSENSO_INSTALL%\development\python"
:: Or just for the current user.
pip3 install --user "%ENSENSO_INSTALL%\development\python"
# For all users.
pip3 install $env:ENSENSO_INSTALL\development\python
# Or just for the current user.
pip3 install --user $env: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
:: Example for fictional nxlib version 3.3.42
:: Make sure you can invoke the Python interpreter with
:: "python3", otherwise consider creating an alias or use
:: "python" instead.
cd C:\Users\<YOUR_USERNAME>
mkdir ensenso
cd ensenso
python3 -m venv .nxlib-3.3.42
.\.nxlib-3.3.42\Scripts\activate.bat
:: If you get an "Parameter Format" error, you can ignore at
:: since the environment should still have been activated.
pip3 install nxlib==3.3.42
# Example for fictional nxlib version 3.3.42
# Make sure you can invoke the Python interpreter with
# "python3", otherwise consider creating an alias or use
# "python" instead.
# Also make sure that your execution policy is set
# correctly. If you are not allowed to execute scripts open
# a powershell as administrator and execute the following:
# set-executionpolicy remotesigned
cd C:\Users\<YOUR_USERNAME>
mkdir ensenso
cd ensenso
python3 -m venv .nxlib-3.3.42
.\.nxlib-3.3.42\Scripts\Activate.ps1
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 functionsnxlib.constants
- Contains the NxLib constants provided by the corresponding SDK versionnxlib.item
- Contains the NxLibItem class implementationnxlib.command
- Contains the NxLibCommand class implementationnxlib.exception
- Contains the NxLibException class implementationnxlib.context
- Contanis context managers which wrap API functionsnxlib.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.