套件管理

使用 mip 安裝套件

具備網路功能的開發板包含 mip 模組,可從 micropython-lib 以及第三方網站(包括 GitHub、GitLab)安裝套件。

mip("mip installs packages")在概念上與 Python 的 pip 工具類似,但它不使用 PyPI 索引,而是預設使用 micropython-lib 作為其索引。從 micropython-lib 下載時,mip 會自動擷取已編譯的 .mpy file

使用 mip 最常見的方式是從 REPL 進行::

>>> import mip
>>> mip.install("pkgname")  # Installs the latest version of "pkgname" (and dependencies)
>>> mip.install("pkgname", version="x.y")  # Installs version x.y of "pkgname"
>>> mip.install("pkgname", mpy=False)  # Installs the source version (i.e. .py rather than .mpy files)

mip 會搜尋 sys.path 中第一個以 /lib 結尾的項目,藉此偵測檔案系統上的適當位置。您可以使用 target 覆寫目的地,但請注意此路徑必須位於 sys.path 中,後續才能匯入它::

>>> mip.install("pkgname", target="third-party")
>>> sys.path.append("third-party")

除了從 micropython-lib 索引下載套件外,mip 也可以安裝第三方程式庫。最簡單的方式是直接下載一個檔案::

>>> mip.install("http://example.com/x/y/foo.py")
>>> mip.install("http://example.com/x/y/foo.mpy")

直接安裝檔案時,仍支援 target 引數以設定目的地路徑,但會忽略 mpyversion

URL 也可以用 github:gitlab: 開頭,作為指向 GitHub 或 GitLab 上託管內容的簡便方式::

>>> mip.install("github:org/repo/path/foo.py")  # Uses default branch
>>> mip.install("github:org/repo/path/foo.py", version="branch-or-tag")  # Optionally specify the branch or tag
>>> mip.install("gitlab:org/repo/path/foo.py")  # Uses default branch
>>> mip.install("gitlab:org/repo/path/foo.py", version="branch-or-tag")  # Optionally specify the branch or tag

更複雜的套件(亦即含有多個檔案,或具有相依套件者)可以透過指定其 package.json 的路徑來下載::

>>> mip.install("http://example.com/x/package.json")
>>> mip.install("github:org/user/path/package.json")
>>> mip.install("gitlab:org/user/path/package.json")

若未指定 json 檔案,則會隱含加上 "package.json"::

>>> mip.install("http://example.com/x/")
>>> mip.install("github:org/repo")  # Uses default branch of that repo
>>> mip.install("github:org/repo", version="branch-or-tag")
>>> mip.install("gitlab:org/repo")  # Uses default branch of that repo
>>> mip.install("gitlab:org/repo", version="branch-or-tag")

在 Unix 埠上使用 mip

在 Unix 埠上,mip 可如上所述在 REPL 中使用,也可以透過 -m 使用::

$ ./micropython -m mip install pkgname-or-url
$ ./micropython -m mip install pkgname-or-url@version

可設定 --target path--no-mpy--index 引數::

$ ./micropython -m mip install --target third-party pkgname
$ ./micropython -m mip install --no-mpy pkgname
$ ./micropython -m mip install --index https://host/pi pkgname

使用 mpremote 安裝套件

mpremote 工具也包含與 mip 相同的功能,可從主機 PC 將套件安裝到本機連接的裝置上(例如透過 USB 或 UART)::

$ mpremote mip install pkgname
$ mpremote mip install [email protected]
$ mpremote mip install http://example.com/x/y/foo.py
$ mpremote mip install github:org/repo
$ mpremote mip install github:org/repo@branch-or-tag
$ mpremote mip install gitlab:org/repo
$ mpremote mip install gitlab:org/repo@branch-or-tag

可設定 --target=path--no-mpy--index 引數::

$ mpremote mip install --target=/flash/third-party pkgname
$ mpremote mip install --no-mpy pkgname
$ mpremote mip install --index https://host/pi pkgname

mpremote 也可以從儲存在主機本機檔案系統上的檔案安裝套件::

$ mpremote mip install path/to/pkg.py
$ mpremote mip install path/to/app/package.json
$ mpremote mip install \\path\\to\\pkg.py

這在開發期間測試套件,以及從 GitHub 儲存庫的本機複本安裝套件時特別有用。請注意,package.json 檔案中的 URL 必須使用正斜線("/")作為目錄分隔符,即使在 Windows 上也是如此,如此才能與從網路安裝相容。

手動安裝套件

套件也可以(以 .py 或 .mpy 形式)藉由手動將檔案複製到裝置來安裝。視開發板而定,這可能是透過 USB 大量儲存裝置、mpremote 工具(例如 mpremote fs cp path/to/package.py :package.py)、webrepl 等方式進行。

撰寫與發布套件

發布到 micropython-lib 是讓您的套件能廣泛供 MicroPython 使用者取用的最簡單方式,並可自動透過 mipmpremote 取得,且會編譯為位元組碼。詳情請參閱 https://github.com/micropython/micropython-lib

若要撰寫可由 mipmpremote 下載的「自我託管」套件,您需要一個靜態網頁伺服器(或 GitHub)來託管單一個 .py 檔案,或一個與您的 .py 檔案放在一起的 package.json 檔案。

託管於 GitHub 上的範例 mlx90640 程式庫可用下列方式安裝::

$ mpremote mip install github:org/micropython-mlx90640

此套件在 GitHub 上的版面配置可能如下所示::

https://github.com/org/micropython-mlx90640/
    package.json
    mlx90640/
        __init__.py
        utils.py

package.json 指定了要安裝之檔案的位置以及其他相依套件::

{
  "urls": [
    ["mlx90640/__init__.py", "mlx90640/__init__.py"],
    ["mlx90640/utils.py", "mlx90640/utils.py"]
  ],
  "deps": [
    ["collections-defaultdict", "latest"],
    ["os-path", "latest"],
    ["github:org/micropython-additions", "main"],
    ["gitlab:org/micropython-otheradditions", "main"]
  ],
  "version": "0.2"
}

urls 清單依下列方式指定要安裝的檔案::

"urls": [
    [destination_path, source_url]
    ...

其中 destination_path 是要安裝在裝置上之檔案的位置與名稱,source_url 則是要安裝之檔案的 URL。來源 URL 通常會以相對於包含 package.json 檔案之目錄的方式指定,但也可以是絕對 URL,例如::

["mlx90640/utils.py", "github:org/micropython-mlx90640/mlx90640/utils.py"]

此套件相依於 collections-defaultdictos-path,它們會自動從 micropython-lib 安裝。第三個相依套件會依 GitHub 儲存庫 org/micropython-additionsmain 分支的 package.json 檔案所定義的內容進行安裝。

凍結套件(freezing)

從裝置檔案系統匯入 Python 模組或套件時,它會在 RAM 中編譯為 bytecode,準備供 VM 執行。對於 .mpy file 而言,此轉換已事先完成,但位元組碼最終仍會存在於 RAM 中。

對於低記憶體裝置或大型應用程式而言,改為從 ROM(亦即快閃記憶體)執行位元組碼可能會更有利。這可以藉由將位元組碼「凍結」到 MicroPython 韌體中來達成,接著再將韌體燒錄到裝置上。執行階段的效能維持不變(雖然匯入會更快),但它可以為您的程式釋出大量可用的 RAM。

此做法的缺點是開發速度慢得多,因為您每次都必須燒錄韌體,但對於凍結那些不常變動的相依套件而言,它仍然很有用。

凍結是藉由撰寫一個資訊清單(manifest)檔案並在建置時使用它來完成的,通常作為自訂開發板定義的一部分。詳情請參閱 MicroPython manifest 檔案 指南。