The Python library is available!

The Python library is available!

Among the top ten questions regularly received by support, there is: "When the hell are you finally going to add Python support?" The phrasing is usually more polite, but that's the message :-)

Well, guess what, the Python library is now available, you can at long last control Yoctopuce modules with Python.

The Python library for Yoctopuce modules is compatible with both Python 2.6+ and 3+ (that's true). It is able to pilot USB modules natively (without requiring a Virtual Hub). It works under Windows, Mac OS X, and Linux, both i386 and ARM. Even Java is not as portable :-)

To install the library, download the Python Yoctopuce library, copy the content of the Sources directory in a directory of your choice, and add this directory in your PYTHONPATH environment variable. That's it.
If you use an IDE to program in Python, you may also have to configure your IDE to enable it to find this directory by itself.

Using the library
The Python library can be used in exactly the same manner as the other Yoctocpuce libraries: you must first import the yocto_api module and the modules corresponding to the functions that you want to manage. Then, you must call registerHub. You can now access your devices. Here is a minimal example to read the temperature from a Yoctopuce module equipped with a temperature sensor, such as a Yocto-Temperature or a Yocto-Meteo.


import sys

# import yoctopuce library
from yocto_api import *
from yocto_temperature import *

# Setup the API to use local USB devices
errmsg= YRefParam()  # little trick for By Ref parameters
if YAPI.RegisterHub("usb", errmsg)!= YAPI.SUCCESS:
    sys.exit("init error"+str(errmsg))

# look for any temperature sensor
sensor = YTemperature.FirstTemperature()
if sensor is None :
    sys.exit("No temperature sensor")

# display sensor value
print("Temperature : %.1f░C" % sensor.get_currentValue())

There you are, you know the basics, you can find the rest in the docs.

If you want to run the Python library on a platform or OS which is not yet supported, contact Yoctopuce support, we may be able to do something for you.

Add a comment No comment yet
Back to blog

Yoctopuce, get your stuff connected.