

支援終止通知：2026 年 10 月 7 日 AWS 將停止 的支援 AWS IoT Greengrass Version 1。2026 年 10 月 7 日之後，您將無法再存取 AWS IoT Greengrass V1 資源。如需詳細資訊，請造訪[從 遷移 AWS IoT Greengrass Version 1](https://docs.aws.amazon.com/greengrass/v2/developerguide/migrate-from-v1.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 安裝 AWS IoT Device SDK 適用於 Python 的
<a name="IoT-SDK"></a>

用戶端裝置可以使用 AWS IoT Device SDK 適用於 Python 的 與 AWS IoT 和 AWS IoT Greengrass 核心裝置通訊 （使用 Python 程式設計語言）。如需詳細資訊，包括需求，請參閱 GitHub 上的 AWS IoT Device SDK 適用於 Python [ Readme](https://github.com/aws/aws-iot-device-sdk-python) 的 。

在此步驟中，您會安裝 SDK，並取得模擬用戶端裝置在電腦上使用`basicDiscovery.py`的範例函數。

1. 若要將軟體開發套件與所有需要的元件安裝到您的電腦，請選擇您的作業系統：

------
#### [ Windows ]

   1. 請開啟[提高命令提示](https://technet.microsoft.com/en-us/library/cc947813(v=ws.10).aspx)，並執行下列命令：

      ```
      python --version
      ```

      若沒有傳回任何版本資訊，或是版本號碼小於 2.7 (Python 2) 或小於 3.3 (Python 3)，請遵循 [Downloading Python](https://wiki.python.org/moin/BeginnersGuide/Download) 中的說明來安裝 Python 2.7\+ 或 Python 3.3\+。如需詳細資訊，請參閱 [Using Python on Windows](https://docs.python.org/3.6/using/windows.html)。

   1. 將 [AWS IoT Device SDK for Python](https://github.com/aws/aws-iot-device-sdk-python) 下載為 `zip` 檔案，並將其解壓縮到電腦上適當的位置。

      記下已解壓縮之 `aws-iot-device-sdk-python-master` 資料夾的檔案路徑，該資料夾包含 `setup.py` 檔案。在下一個步驟中，這檔案路徑會以 {{path-to-SDK-folder}} 表示。

   1. 自提高命令提示執行下列命令：

      ```
      cd {{path-to-SDK-folder}}
      python setup.py install
      ```

------
#### [ macOS ]

   1. 請開啟終端機視窗並執行下列命令：

      ```
      python --version
      ```

      若沒有傳回任何版本資訊，或是版本號碼小於 2.7 (Python 2) 或小於 3.3 (Python 3)，請遵循 [Downloading Python](https://wiki.python.org/moin/BeginnersGuide/Download) 中的說明來安裝 Python 2.7\+ 或 Python 3.3\+。如需詳細資訊，請參閱 [Using Python on a Macintosh](https://docs.python.org/3/using/mac.html)。

   1. 在終端機視窗中執行下列命令以判斷 OpenSSL 版本：

      ```
      python
      >>>import ssl
      >>>print ssl.OPENSSL_VERSION
      ```

      記下 OpenSSL 的版本值。
**注意**  
若您執行的是 Python 3，請使用 **print(ssl.OPENSSL\_VERSION)**。

      若要關閉 Python shell，請執行下列命令：

      ```
      >>>exit()
      ```

      如果 OpenSSL 版本為 1.0.1 或更新，請跳到[步驟 c](#step-c-install-python-sdk)。否則，請遵循這些步驟：

      1. 請從終端機視窗中執行下列命令來判斷電腦是否使用 Simple Python 版本管理：

        ```
        which pyenv
        ```

      如果傳回檔案路徑，請選擇**使用 `pyenv`** 標籤。如果未傳回，請選擇**不使用 `pyenv`** 標籤。

------
#### [ Using pyenv ]

      1. 請參閱 [Python Releases for Mac OS X](https://www.python.org/downloads/mac-osx/) (或類似) 以判斷最新又穩定的 Python 版本。在以下範例中，此數值會以 {{latest-Python-version}} 表示。

      1. 從終端機視窗中執行下列命令：

         ```
         pyenv install {{latest-Python-version}}
         pyenv global {{latest-Python-version}}
         ```

         例如，若 Python 2 的最新版本是 2.7.14，則這些命令為：

         ```
         pyenv install 2.7.14
         pyenv global 2.7.14
         ```

      1. 關閉然後重新開啟終端機視窗，執行以下命令：

         ```
         python
         >>>import ssl
         >>>print ssl.OPENSSL_VERSION
         ```

         OpenSSL 版本至少應為 1.0.1。若版本小於 1.0.1，則更新會失敗。檢查 **pyenv install** 和 **pyenv global** 命令中所使用的 Python 版本值，然後再試一次。

      1. 執行下列命令退出 Python shell：

         ```
          exit()
         ```

------
#### [ Not using pyenv ]

      1. 從終端機視窗執行下列命令來判斷 [brew](https://brew.sh/) 是否已安裝：

         ```
         which brew
         ```

         如果沒有傳回檔案的路徑，請安裝如下所示的 `brew`：

         ```
         /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
         ```
**注意**  
遵循安裝提示。下載 Xcode 命令列工具可能需要一些時間。

      1. 執行下列命令：

         ```
         brew update
         brew install openssl
         brew install python@2
         ```

          AWS IoT Device SDK 適用於 Python 的 需要使用 Python 可執行檔編譯的 OpenSSL 1.0.1 版 （或更新版本）。**brew install python** 命令會安裝 `python2` 可執行檔，以符合此需求。`python2` 可執行檔安裝於 `/usr/local/bin` 目錄中，應屬於 `PATH` 環境變數的一部分。若要確認，請執行下列命令：

         ```
         python2 --version
         ```

         如果已提供 `python2` 版本資訊，請跳到下一個步驟。若否，請新增 `/usr/local/bin` 路徑至您的 `PATH` 環境變數，此需為永久持續，透過將下行附加至您的 shell 描述檔：

         ```
         export PATH="/usr/local/bin:$PATH"
         ```

         例如，如果您使用的是 `.bash_profile` 或沒有 shell 設定檔從終端視窗，請執行下列命令：

         ```
         echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile
         ```

         接著，[來源](https://en.wikipedia.org/wiki/Source_(command))您的 shell 描述檔，並確認 `python2 --version` 提供版本資訊。例如，如果您是使用 `.bash_profile`，請執行下列命令：

         ```
         source ~/.bash_profile
         python2 --version
         ```

         應傳回 `python2` 版本資訊。

      1. 附加下行至您的 shell 描述檔中：

         ```
         alias python="python2"
         ```

         例如，如果您是使用 `.bash_profile` 或尚未有 shell 描述檔，則請執行下列命令：

         ```
         echo 'alias python="python2"' >> ~/.bash_profile
         ```

      1. 接著，[來源](https://en.wikipedia.org/wiki/Source_(command))您的 shell 描述檔。例如，如果您是使用 `.bash_profile`，請執行下列命令：

         ```
         source ~/.bash_profile
         ```

         呼叫 **python** 命令會執行 Python 可執行檔，其中包含所需的 OpenSSL 版本 (`python2`)。

      1. 執行下列命令：

         ```
         python
          import ssl
          print ssl.OPENSSL_VERSION
         ```

         OpenSSL 版本至少應為 1.0.1 或更新版。

      1. 若要退出 Python shell，請執行下列命令：

         ```
          exit()
         ```

------

   1. <a name="step-c-install-python-sdk"></a>執行下列命令來安裝 AWS IoT Device SDK 適用於 Python 的 ：

      ```
      cd ~
      git clone https://github.com/aws/aws-iot-device-sdk-python.git
      cd aws-iot-device-sdk-python
      sudo python setup.py install
      ```

------
#### [ UNIX-like system ]

   1. 從終端機視窗中，執行下列命令：

      ```
      python --version
      ```

      若沒有傳回任何版本資訊，或是版本號碼小於 2.7 (Python 2) 或小於 3.3 (Python 3)，請遵循 [Downloading Python](https://wiki.python.org/moin/BeginnersGuide/Download) 中的說明來安裝 Python 2.7\+ 或 Python 3.3\+。如需詳細資訊，請參閱 [Using Python on Unix platforms](https://docs.python.org/3.6/using/unix.html)。

   1. 在終端機中執行下列命令以判斷 OpenSSL 版本：

      ```
      python
      >>>import ssl
      >>>print ssl.OPENSSL_VERSION
      ```

      記下 OpenSSL 的版本值。
**注意**  
若您執行的是 Python 3，請使用 **print(ssl.OPENSSL\_VERSION)**。

      若要關閉 Python shell，請執行下列命令：

      ```
       exit()
      ```

      如果 OpenSSL 版本為 1.0.1 或更新，請跳到下一個步驟。否則，請執行命令來更新您發行版本的 OpenSSL (例如 `sudo yum update openssl`、`sudo apt-get update` 等)。

      透過執行下列命令確認 OpenSSL 版本為 1.0.1 或更新版：

      ```
      python
      >>>import ssl
      >>>print ssl.OPENSSL_VERSION
      >>>exit()
      ```

   1. 執行下列命令來安裝 AWS IoT Device SDK 適用於 Python 的 ：

      ```
      cd ~
      git clone https://github.com/aws/aws-iot-device-sdk-python.git
      cd aws-iot-device-sdk-python
      sudo python setup.py install
      ```

------

1. 安裝 AWS IoT Device SDK 適用於 Python 的 之後，導覽至 `samples` 資料夾並開啟 `greengrass` 資料夾。

   在本教學課程中，您將複製 `basicDiscovery.py` 範例函數，該函數會使用您在 [在 AWS IoT Greengrass 群組中建立用戶端裝置](device-group.md) 中下載的憑證和金鑰。

1. `basicDiscovery.py` 複製到包含 HelloWorld\_Publisher 和 HelloWorld\_Subscriber 裝置憑證和金鑰的資料夾。