패키지 관리

mip으로 패키지 설치하기

네트워크 기능이 있는 보드에는 mip 모듈이 포함되어 있으며, 이를 통해 micropython-lib 및 서드파티 사이트(GitHub, GitLab 포함)에서 패키지를 설치할 수 있습니다.

mip(“mip installs packages”)은 개념적으로 Python의 pip 도구와 유사하지만, PyPI 인덱스를 사용하지 않고 기본적으로 micropython-lib를 인덱스로 사용합니다. mip은 micropython-lib에서 다운로드할 때 컴파일된 .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)

mipsys.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 포트에서는 위와 같이 REPL에서 mip을 사용할 수 있으며, -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 를 참조하십시오.

mip 또는 mpremote로 다운로드할 수 있는 “자체 호스팅” 패키지를 작성하려면, 단일 .py 파일 또는 .py 파일과 함께 package.json 파일을 호스팅할 정적 웹 서버(또는 GitHub)가 필요합니다.

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 모듈 또는 패키지가 장치 파일 시스템에서 임포트되면, VM에 의해 실행될 준비가 된 bytecode로 RAM에서 컴파일됩니다. .mpy file의 경우 이 변환은 이미 완료되었지만, 바이트코드는 여전히 RAM에 들어가게 됩니다.

저메모리 장치나 대규모 애플리케이션의 경우, 대신 ROM(즉, 플래시 메모리)에서 바이트코드를 실행하는 것이 유리할 수 있습니다. 이는 바이트코드를 MicroPython 펌웨어에 “동결(freezing)”한 다음 장치에 플래시하여 수행할 수 있습니다. 런타임 성능은 동일하지만(임포트는 더 빠름), 프로그램이 사용할 수 있는 상당량의 RAM을 확보할 수 있습니다.

이 접근법의 단점은 매번 펌웨어를 플래시해야 하므로 개발 속도가 훨씬 느리다는 점이지만, 자주 변경되지 않는 의존성을 동결하는 데는 여전히 유용할 수 있습니다.

동결은 매니페스트 파일을 작성하고 이를 빌드에서 사용함으로써 이루어지며, 종종 사용자 정의 보드 정의의 일부로 수행됩니다. 자세한 내용은 MicroPython 매니페스트 파일 가이드를 참조하십시오.