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

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

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

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

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

wj8zmpe1

wj8zmpe11#

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

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

上载文件

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

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

  1. ampy --port /dev/ttyXXXX rm main.py
展开查看全部
bfhwhh0e

bfhwhh0e2#

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

  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. from dataclasses import dataclass, field
  4. from typing import Optional, Sequence, Tuple
  5. from mpremote import commands
  6. from mpremote import main as mpr_main
  7. from mpremote import mip
  8. @dataclass
  9. class MyArgs:
  10. device: Sequence[str]
  11. follow: bool = True
  12. expr: Sequence[str] = field(default_factory=list)
  13. eval: Sequence[str] = field(default_factory=list)
  14. recursive: bool = False
  15. verbose: bool = False
  16. command: Sequence[str] = field(default_factory=list)
  17. path: Tuple[str, ...] = field(default_factory=list)
  18. do_run_command: bool = False
  19. mpy: bool = True
  20. target: Optional[str] = None
  21. index: Optional[str] = None
  22. packages: Sequence[str] = field(default_factory=list)
  23. def main():
  24. args = MyArgs(
  25. # connect to the device
  26. device=["/dev/ttyACM0"],
  27. #
  28. # execute the string on the device
  29. # expr=["import micropython; micropython.mem_info()"],
  30. #
  31. # evaluate and print the result of a Python expression
  32. # eval=["1/2"],
  33. #
  34. # list all files on the device
  35. # command=["ls"],
  36. #
  37. # copy the file "changelog.md" from the parent dir to the device
  38. # command=["cp"],
  39. # path=["../changelog.md", ":changelog.md"],
  40. #
  41. # copy the folder "tests" recursively to the device
  42. # command=["cp"],
  43. # recursive=True,
  44. # path=["tests/", ":"],
  45. #
  46. # remove the file "changelog.md" from the device
  47. # command=["rm"],
  48. # path=["changelog.md"],
  49. #
  50. # run given local script
  51. # do_run_command=True,
  52. # path=["boot.py"],
  53. #
  54. # install some mip package
  55. # command=["install"],
  56. # packages=[
  57. # "umqtt.simple",
  58. # "github:brainelectronics/micropython-modbus",
  59. # ],
  60. )
  61. print(args)
  62. state = mpr_main.State()
  63. commands.do_connect(state=state, args=args)
  64. if args.expr:
  65. commands.do_exec(state=state, args=args)
  66. if args.eval:
  67. args.expr = args.eval
  68. commands.do_eval(state=state, args=args)
  69. if args.command and not args.packages:
  70. commands.do_filesystem(state=state, args=args)
  71. if args.do_run_command:
  72. commands.do_run(state=state, args=args)
  73. if args.packages:
  74. mip.do_mip(state=state, args=args)
  75. commands.do_disconnect(state)
  76. if __name__ == "__main__":
  77. main()
展开查看全部

相关问题