QmixSDK for Python
python_header.svg

Python Integration

The QmixSDK comes with full Python integration. For Windows you find the Python integration in the subfolder lib/python and for Linux in the subfolder python. The Python integration is based on the Qmix SDK shared libraries and uses the Python ctypes module to access the C interface of the SDK libraries.


Running Python tests

With the test cases you can quickly check, if the QmixSDK Python integration runs properly on your system. To run a test case, you should open a terminal and change into the python folder that contains the runtest.bat or runtest.sh file. Now you can execute the test cases on Windows with

1 run_pytest.bat test_qmixpump.py

or on Linux with

1 ./run_pytest.sh test_qmixpump.py

For all tests a valid device configuration is required. You can pass in the device configuration folder as a parameter for a test. The runtest.bat or runtest.sh files use the simulated test configuration config/testconfig_qmixsdk. All devices in this test configuration are simulated.


Using the Python Integration

To use the QmixSDK you need to create a valid device configuration. Read the Getting Started section to learn how to create a device configuration and what a device configuration is.

The python integration is essentially a wrapper for the C interface of the SDK. That means, you can use the C API documentation for the Python integration. The Python integration wraps the C interface into Python classes. That means, the functions in the qmixbus.Bus class are documented in the labbCAN Bus API, the functions in the qmixpump.Pump class are documented in the labbCAN Pump API and so on.

If you use the Python integration, then you need to ensure that Python finds the Python modules and the shared libraries of the SDK. To ensure that the qmixsdk module is found, the path to the module folder needs to be in the Python module search path. There are two ways to achieve that. The first way is, to write code in your python script before any Qmix SDK module import. In the following example we have installed the Qmix SDK on a Windows machine into the folder C:/QmixSDK. Now you need to write the following code into your Python file before you import anything from the qmixsdk module.

1 import sys
2 
3 sys.path.append("C:/QmixSDK/lib/python")

The code appends the Qmix SDK module path C:/QmixSDK/lib/python to the module search path to enable Python to find the SDK modules.

The second way is to modify the PYTHONPATH environment variable to contain the directory where the qmixsdk module is located before starting the interpreter.

1 set PYTHONPATH=C:\temp\QmixSDK64\lib\python;%PATHONPATH%;

If you are working on Windows, then you are finished now and everything should work. The qmixsdk module contains code that ensures, that the shared libraries are found.

If you use the Python integration on Linux, then you need to set the LD_LIBRARY_PATH environment variable properly to enable the ctypes module to find the shared libraries of the QmixSDK. It is not possible to do this inside of your Python script, you need to do this before you run your python scripts. The LD_LIBRARY_PATH needs to point to the lib folder of the QmixSDK. Look into run_pytest.sh to see how the script calls the python tests. So that means, on Linux you need to export the following environment variables:

1 export QMIXSDK_PATH="$HOME/QmixSDK_Linux"
2 export PYTHONPATH="$QMIXSDK_PATH/python":"$PYTHONPATH"
3 export LD_LIBRARY_PATH="$QMIXSDK_PATH/lib":"$LD_LIBRARY_PATH"

Alternatively you can use the provided wrapper script qmixsdk_py_wrapper.sh instead of calling python when executing your scripts:

1 ~/QmixSDK_Linux/python/qmixsdk_py_wrapper my_qmix_python_script.py

Now you can import the Qmix SDK modules and use them:

1 from qmixsdk import qmixbus
2 from qmixsdk import qmixpump
3 from qmixsdk import qmixvalve
4 from qmixsdk.qmixbus import UnitPrefix, TimeUnit
5 
6 bus = qmixbus.Bus()
7 ...
Note
Make sure to use the correct SDK version (32-bit or 64-bit) for your Python installation. If you are using the 64-bit version of Python, you also need the 64-bit version of the Qmix SDK.

The Python integration was developed and tested with Python 3.6.5. and Python 3.8.2


Getting Started

Read the Getting Started section to get started using the Qmix SDK functions of the Python integration. The source code of the tests will also help you to understand how to use the Qmix Python integration.