This topic describes the recommended development environments for Link SDK for Python and how to configure the development environments.
Environment configuration
We recommend that you use Python 3.6 or later.
Link SDK for Python is validated on the following operating systems. To prevent errors during development and runtime, we recommend that you use one of the following environments:
Linux: Ubuntu 18.04 64-bit or later
Windows: Windows 7 64-bit or later
macOS: High Sierra
Usage notes
This example shows how to install and configure environments. In this example, Python 3.6 is used. If you want to install a version that is higher than Python 3.6, replace the version number in the
python3.6
command with the version number of Python that you want to install. Example:python3.9
.In this example, a regular user is used. If you want to perform specific operations that require administrator permissions, run the
sudo
command.
Install Python 3.6
Linux and macOS
Step 1: Install Python 3.6
If Python 3.6 or later is installed, skip this step.
Linux
To install Python 3.6, use one of the following methods:
Commands: This method is suitable for Ubuntu 18.04 or later.
sudo apt-get update sudo apt install software-properties-common sudo add-apt-repository ppa:deadsnakes/ppa sudo apt-get install wget python3.6
Source code: This method is suitable for operating systems that do not support command-based installation.
sudo apt-get install wget gcc make zlib1g-dev wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz tar -xf Python-3.6.7.tgz cd Python-3.6.7 ./configure make -j sudo make install
macOS
Download Python from Python for macOS.
Step 2: Install pip
Obtain a pip script.
Python 3.6:
wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
Python 3.7 or later
wget https://bootstrap.pypa.io/get-pip.py
Install pip.
In this example, pip for Python 3.6 is installed. If you want to install pip of a higher version, replace the version number in the
python3.6
command with the version number of pip that you want to install. Example:python3.9
.sudo apt-get install python3-distutils sudo python3.6 get-pip.py
Step 3: Install setuptools, wheel, and venv
If you want to compile and run Link SDK for Python, you must install setuptools, wheel, and venv. setuptools is used to manage Python packages, and wheel and venv are required to compile Link SDK for Python. venv is used to manage virtual environments.
Run the following command:
python3.6 -m pip install --upgrade pip setuptools wheel
sudo apt-get install python3.6-venv
Windows
Download and install one of the following packages based on the type of your operating system:
Download Demo
Download the demo from Obtain sample code.
Configure environments
Alibaba Cloud performed stress testing on paho-mqtt v1.4.0 and paho-mqtt v1.5.1. We recommend that you use one of the preceding versions. You can run the pip list
command to view the version of your paho-mqtt.
Linux and macOS
Create and activate a virtual environment
Automatically or manually install Link SDK for Python.
Automatically install the Go SDK
Run the following command to install paho-mqtt v1.4.0:
pip install paho-mqtt==1.4.0
Run the following command to install the latest version of Link SDK for Python:
pip install aliyun-iot-linkkit
Manually install software
Download the SDK from Obtain the latest version of Link SDK for Python and the required open source Message Queuing Telemetry Transport (MQTT) libraries from Obtain open source Paho MQTT libraries.
The latest version of Link SDK for Python is 1.2.12.
Copy the aliyun-iot-linkkit-1.2.12.tar.gz and paho-mqtt-1.4.0.tar.gz packages to the work_dir directory and then run the following commands:
tar zxvf paho-mqtt-1.4.0.tar.gz cd paho-mqtt-1.4.0 python3 setup.py install cd .. tar zxvf aliyun-iot-linkkit-1.2.12.tar.gz cd aliyun-iot-linkkit-1.2.12 python3 setup.py install cd ..
mkdir work_dir
cd work_dir
python3.6 -m venv test_env
source test_env/bin/activate
pip install wheel
Windows
Create and activate a virtual environment
mkdir work_dir cd work_dir python3.6 -m venv test_env test_env\Scripts\activate.bat
Automatically or manually install Link SDK for Python.
Automatically install the Go SDK
Run the following command to install paho-mqtt v1.4.0:
pip install paho-mqtt==1.4.0
Run the following command to install the latest version of Link SDK for Python:
pip install aliyun-iot-linkkit
Manually install software
Download the SDK from Obtain the latest version of Link SDK for Python and the required open source MQTT libraries from Obtain open source Paho MQTT libraries.
The latest version of Link SDK for Python is 1.2.12.
Copy the paho-mqtt-1.4.0.tar.gz and aliyun-iot-linkkit-1.2.12.tar.gz packages to the work_dir directory, decompress the packages, and then run the following commands:
cd paho-mqtt-1.4.0 python setup.py install cd .. cd aliyun-iot-linkkit-1.2.12 python setup.py install cd ..
Log switch
Turn on the internal log output switch of the SDK.
import logging
__log_format = '%(asctime)s-%(process)d-%(thread)d - %(name)s:%(module)s:%(funcName)s - %(levelname)s - %(message)s'
logging.basicConfig(format=__log_format)