このトピックでは、Link SDK for Python の推奨開発環境と、開発環境を構成する方法について説明します。
環境構成
Python 3.6 以降を使用することをお勧めします。
Link SDK for Python は、以下のオペレーティングシステムで検証されています。開発時および実行時のエラーを回避するために、以下のいずれかの環境を使用することをお勧めします。
Linux: Ubuntu 18.04 64 ビット以降
Windows: Windows 7 64 ビット以降
macOS: High Sierra
使用方法
この例は、環境をインストールして構成する方法を示しています。この例では、Python 3.6 を使用しています。 Python 3.6 よりも高いバージョンをインストールする場合は、
python3.6
コマンドのバージョン番号をインストールする Python のバージョン番号に置き換えます。例:python3.9
。この例では、一般ユーザーを使用しています。管理者権限が必要な特定の操作を実行する場合は、
sudo
コマンドを実行します。
Python 3.6 をインストールする
Linux および macOS
手順 1: Python 3.6 をインストールする
Python 3.6 以降がインストールされている場合は、このステップをスキップします。
Linux
Python 3.6 をインストールするには、次のいずれかの方法を使用します。
コマンド: このメソッドは、Ubuntu 18.04 以降に適しています。
sudo apt-get update sudo apt install software-properties-common sudo add-apt-repository ppa:deadsnakes/ppa sudo apt-get install wget python3.6
ソースコード: このメソッドは、コマンドベースのインストールをサポートしていないオペレーティングシステムに適しています。
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
macOS 版 Python から Python をダウンロードします。
手順 2:pip をインストールする
pip スクリプトを取得します。
```python # Check if a string starts with a specific prefix. if text.startswith("prefix"): # If it does, remove the prefix. text = text[len("prefix"):] ``` Python 3.9: ```python # Check if a string starts with a specific prefix. if text.startswith("prefix"): # If it does, remove the prefix using removeprefix(). text = text.removeprefix("prefix") ``` In Python 3.9 and later versions, you can use the removeprefix() method to efficiently remove a prefix from a string. For earlier versions like Python 3.6, you need to manually slice the string. Python 3.6: ```python # Check if a string ends with a specific suffix. if text.endswith("suffix"): # If it does, remove the suffix. text = text[:-len("suffix")] ``` Python 3.9: ```python # Check if a string ends with a specific suffix. if text.endswith("suffix"): # If it does, remove the suffix using removesuffix(). text = text.removesuffix("suffix") ``` Similarly, the removesuffix() method is available in Python 3.9 and later for removing suffixes. ```python # Python 3.6 で接頭辞を削除する例 text = "prefix_example" if text.startswith("prefix"): text = text[len("prefix"):] # 接頭辞を削除 print(text) # example を出力 # Python 3.9 で接頭辞を削除する例 text = "prefix_example" if text.startswith("prefix"): text = text.removeprefix("prefix") # removeprefix() を使用して接頭辞を削除 print(text) # example を出力 # Python 3.6 で接尾辞を削除する例 text = "example_suffix" if text.endswith("suffix"): text = text[:-len("suffix")] # 接尾辞を削除 print(text) # example を出力 # Python 3.9 で接尾辞を削除する例 text = "example_suffix" if text.endswith("suffix"): text = text.removesuffix("suffix") # removesuffix() を使用して接尾辞を削除 print(text) # example を出力 ``` These examples demonstrate how to remove prefixes and suffixes in different Python versions. ```python # 文字列が特定の接頭辞で始まるかどうかを確認します。 if text.startswith("prefix"): # 接頭辞で始まる場合は、接頭辞を削除します。 text = text[len("prefix"):] ``` Python 3.9: ```python # 文字列が特定の接頭辞で始まるかどうかを確認します。 if text.startswith("prefix"): # 接頭辞で始まる場合は、removeprefix() を使用して接頭辞を削除します。 text = text.removeprefix("prefix") ``` Python 3.9 以降のバージョンでは、removeprefix() メソッドを使用して、文字列から接頭辞を効率的に削除できます。 Python 3.6 などの以前のバージョンでは、文字列を手動でスライスする必要があります。 Python 3.6: ```python # 文字列が特定の接尾辞で終わるかどうかを確認します。 if text.endswith("suffix"): # 接尾辞で終わる場合は、接尾辞を削除します。 text = text[:-len("suffix")] ``` Python 3.9: ```python # 文字列が特定の接尾辞で終わるかどうかを確認します。 if text.endswith("suffix"): # 接尾辞で終わる場合は、removesuffix() を使用して接尾辞を削除します。 text = text.removesuffix("suffix") ``` 同様に、Python 3.9 以降では、接尾辞を削除するために removesuffix() メソッドを使用できます。 ```python # Python 3.6 で接頭辞を削除する例 text = "prefix_example" if text.startswith("prefix"): text = text[len("prefix"):] # 接頭辞を削除 print(text) # example を出力 # Python 3.9 で接頭辞を削除する例 text = "prefix_example" if text.startswith("prefix"): text = text.removeprefix("prefix") # removeprefix() を使用して接頭辞を削除 print(text) # example を出力 # Python 3.6 で接尾辞を削除する例 text = "example_suffix" if text.endswith("suffix"): text = text[:-len("suffix")] # 接尾辞を削除 print(text) # example を出力 # Python 3.9 で接尾辞を削除する例 text = "example_suffix" if text.endswith("suffix"): text = text.removesuffix("suffix") # removesuffix() を使用して接尾辞を削除 print(text) # example を出力 ``` これらの例は、さまざまな Python バージョンで接頭辞と接尾辞を削除する方法を示しています。
wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
Python 3.7 以降
wget https://bootstrap.pypa.io/get-pip.py
pip をインストールします。
この例では、Python 3.6 用の pip がインストールされています。より高いバージョンの pip をインストールする場合は、
python3.6
コマンドのバージョン番号をインストールする pip のバージョン番号に置き換えます。例:python3.9
。sudo apt-get install python3-distutils sudo python3.6 get-pip.py
手順 3: setuptools、wheel、および venv をインストールする
setuptools、wheel、venv をインストールして、Link SDK for Python をコンパイルおよび実行する必要があります。setuptools は Python パッケージの管理に使用され、wheel と venv は Link SDK for Python のコンパイルに必要です。venv は仮想環境の管理に使用されます。
次のコマンドを実行します。
python3.6 -m pip install --upgrade pip setuptools wheel
sudo apt-get install python3.6-venv
Windows
次のオペレーティングシステムの種類に基づいて、以下のいずれかのパッケージをダウンロードしてインストールします。
デモをダウンロード
[サンプル コードを入手する] からデモをダウンロードします。
環境を構成する
Alibaba Cloud は、paho-mqtt v1.4.0 および paho-mqtt v1.5.1 でストレステストを実行しました。上記のいずれかのバージョンを使用することをお勧めします。pip list
コマンドを実行して、paho-mqtt のバージョンを確認できます。
Linux と macOS
仮想環境を作成してアクティブ化します。
Link SDK for Python を自動または手動でインストールします。
Go SDK を自動的にインストールする
次のコマンドを実行して、paho-mqtt v1.4.0 をインストールします。
pip install paho-mqtt==1.4.0
次のコマンドを実行して、Link SDK for Python の最新バージョンをインストールします。
pip install aliyun-iot-linkkit
ソフトウェアを手動でインストールする
Link SDK for Python の最新バージョンを入手する から SDK を、オープンソースの Paho MQTT ライブラリを入手する から必要なオープンソース Message Queuing Telemetry Transport (MQTT) ライブラリをダウンロードします。
Link SDK for Python の最新バージョンは 1.2.12 です。
aliyun-iot-linkkit-1.2.12.tar.gz パッケージと paho-mqtt-1.4.0.tar.gz パッケージを work_dir ディレクトリにコピーし、次のコマンドを実行します。
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
仮想環境を作成してアクティブ化する
mkdir work_dir cd work_dir python3.6 -m venv test_env test_env\Scripts\activate.bat
Link SDK for Python を自動または手動でインストールします。
Go SDK を自動的にインストールする
Run the following command to install paho-mqtt v1.4.0: 以下のコマンドを実行して、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: 以下のコマンドを実行して、最新バージョンの Python 用 Link SDK をインストールします。
pip install aliyun-iot-linkkit
ソフトウェアを手動でインストールする
Python 用 Link SDK の最新バージョンを入手する から SDK をダウンロードし、オープンソースの Paho MQTT ライブラリを入手する から必要なオープンソース MQTT ライブラリをダウンロードします。
Link SDK for Python の最新バージョンは 1.2.12 です。
paho-mqtt-1.4.0.tar.gz パッケージと aliyun-iot-linkkit-1.2.12.tar.gz パッケージを work_dir ディレクトリにコピーし、パッケージを解凍してから、次のコマンドを実行します。
cd paho-mqtt-1.4.0 python setup.py install cd .. cd aliyun-iot-linkkit-1.2.12 python setup.py install cd ..
ログ切り替え
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)