windows 如何从Python脚本上传Python文件?

dsekswqp  于 2023-10-22  发布在  Windows
关注(0)|答案(2)|浏览(168)

我试图写一个脚本,可以从多个输入创建一个Python文件,并将其上传到安装了MicroPython的Raspberry Pi皮科。我试了一下rshell:

import subprocess
cmds = 'python -m venv venv & venv\Scripts\activate & pip install rshell pyreadlline3 & rshell & connect serial COM3'
ret = subprocess.run(cmd, capture_output=True, shell=True)
ret.stdout.decode()

子进程不能在rshell中键入命令。我找不到办法上传。是不是有一个图书馆我错过了,或者是不可能的?

wj8zmpe1

wj8zmpe11#

如果rshell不适合你,我会尝试ampy
MicroPython Tool(ampy)-通过串行连接与CircuitPython或MicroPython板交互的实用程序。
April是一个简单的命令行工具,可以通过串行连接在CircuitPython或MicroPython板上操作文件和运行代码。使用ampy,您可以将文件从您的计算机发送到板的文件系统,将文件从板下载到您的计算机,甚至可以将Python脚本发送到板以执行。
pip install adafruit-ampy
然后你必须找到你的baord连接到PC的串行端口,然后
在板子上运行一个文件而不上传

ampy --port /dev/ttyXXXX run main.py

上载文件

ampy --port /dev/ttyXXXX put main.py

要删除文件,请执行以下操作:

ampy --port /dev/ttyXXXX rm main.py
bfhwhh0e

bfhwhh0e2#

您可以通过后门以某种方式使用mpremote。唯一的缺点是缺少了mpremote/pyboard.py中定义的stdout/stderr捕获,因此使用redirect_stdout并不能开箱即用

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from dataclasses import dataclass, field
from typing import Optional, Sequence, Tuple

from mpremote import commands
from mpremote import main as mpr_main
from mpremote import mip

@dataclass
class MyArgs:
    device: Sequence[str]
    follow: bool = True
    expr: Sequence[str] = field(default_factory=list)
    eval: Sequence[str] = field(default_factory=list)
    recursive: bool = False
    verbose: bool = False
    command: Sequence[str] = field(default_factory=list)
    path: Tuple[str, ...] = field(default_factory=list)
    do_run_command: bool = False
    mpy: bool = True
    target: Optional[str] = None
    index: Optional[str] = None
    packages: Sequence[str] = field(default_factory=list)

def main():
    args = MyArgs(
        # connect to the device
        device=["/dev/ttyACM0"],
        #
        # execute the string on the device
        # expr=["import micropython; micropython.mem_info()"],
        #
        # evaluate and print the result of a Python expression
        # eval=["1/2"],
        #
        # list all files on the device
        # command=["ls"],
        #
        # copy the file "changelog.md" from the parent dir to the device
        # command=["cp"],
        # path=["../changelog.md", ":changelog.md"],
        #
        # copy the folder "tests" recursively to the device
        # command=["cp"],
        # recursive=True,
        # path=["tests/", ":"],
        #
        # remove the file "changelog.md" from the device
        # command=["rm"],
        # path=["changelog.md"],
        #
        # run given local script
        # do_run_command=True,
        # path=["boot.py"],
        #
        # install some mip package
        # command=["install"],
        # packages=[
        #     "umqtt.simple",
        #     "github:brainelectronics/micropython-modbus",
        # ],
    )
    print(args)

    state = mpr_main.State()
    commands.do_connect(state=state, args=args)

    if args.expr:
        commands.do_exec(state=state, args=args)

    if args.eval:
        args.expr = args.eval
        commands.do_eval(state=state, args=args)

    if args.command and not args.packages:
        commands.do_filesystem(state=state, args=args)

    if args.do_run_command:
        commands.do_run(state=state, args=args)

    if args.packages:
        mip.do_mip(state=state, args=args)

    commands.do_disconnect(state)

if __name__ == "__main__":
    main()

相关问题