如何在单独的文件中使用 Micropython 类
MicroPython 入门并遇到单独文件中的类问题:
在 main.py
中:
import clientBase
import time
if __name__ == "__main__":
time.sleep(15) # Delay to open Putty
print("Starting")
print("Going to class")
cb = clientBase.ClientBaseClass
cb.process()
在 clientBase.py
中:
class ClientBaseClass:
def __init__(self):
print("init")
def process(self):
print("Process")
编译并复制到 Pico,没有错误,但无法运行。 Putty 输出:不知道如何在不阻塞端口的情况下运行 Putty(或其他端口监视器)!
MPY: soft reboot
Traceback (most recent call last):
感谢
Python控制台:
“C:\ Users \ jluca \ OneDrive \ Apps \ Analytical Engine \ Python \ Client \ venv \ Scripts \ python.exe”“C:\ Program Files \ JetBrains \ PyCharm Community Edition 2021.2.4 \ plugins \ python -ce\helpers\pydev\pydevconsole.py" --mode=client --port=59708 导入系统; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['C:\Users\jluca\OneDrive\Apps\Analytical Engine\Python\Client', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython \typehints\stdlib', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\micropython', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij -micropython\typehints\rpi_pico', 'C:/Users/jluca/OneDrive/Apps/Analytical Engine/Python/Client']) PyDev 控制台:正在启动。 Win32 上的 Python 3.10.3(tags/v3.10.3:a342a49,2022 年 3 月 16 日,13:07:40)[MSC v.1929 64 位 (AMD64)]
Getting started with MicroPython and having problems with classes in separate files:
In main.py
:
import clientBase
import time
if __name__ == "__main__":
time.sleep(15) # Delay to open Putty
print("Starting")
print("Going to class")
cb = clientBase.ClientBaseClass
cb.process()
In clientBase.py
:
class ClientBaseClass:
def __init__(self):
print("init")
def process(self):
print("Process")
Compiles and copies to Pico without errors but does not run. Putty output: No idea how to run Putty (or other port monitor) without blocking port!
MPY: soft reboot
Traceback (most recent call last):
Thanks
Python Conslole:
"C:\Users\jluca\OneDrive\Apps\Analytical Engine\Python\Client\venv\Scripts\python.exe" "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2.4\plugins\python-ce\helpers\pydev\pydevconsole.py" --mode=client --port=59708
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['C:\Users\jluca\OneDrive\Apps\Analytical Engine\Python\Client', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\stdlib', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\micropython', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\rpi_pico', 'C:/Users/jluca/OneDrive/Apps/Analytical Engine/Python/Client'])
PyDev console: starting.
Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这里看到的第一个问题是您没有正确实例化 ClientBaseClass 对象。这里缺少括号:
这是将变量
cb
设置为类ClientBaseClass
,而不是创建该类的新对象。您需要:
我不知道这是否是您的唯一问题;查看您的回溯将提供有关问题的更多详细信息。
如果我解决了这个问题,一切似乎都会奏效。我正在使用
ampy
将文件传输到我的 Pico 板(我还使用 Thonny 编辑,它提供了一个用于使用 Micropython 板的菜单驱动界面):我按
return
以获得 Micropython REPL 提示:然后输入
CTRL-D重置板:
然后板出现,代码按预期执行:(
请注意,如果将 MicroPython 替换为 CircuitPython,Pico 将显示为驱动器,您只需在其上拖放文件即可。)
The first problem I see here is that you're not properly instantiating the
ClientBaseClass
object. You're missing parentheses here:This is setting the variable
cb
the classClientBaseClass
, rather than creating a new object of that class.You need:
I don't know if that's your only problem or not; seeing your traceback will shed more details on the problem.
If I fix that one problem, it all seems to work. I'm using
ampy
to transfer files to my Pico board (I've also repeated the same process using the Thonny edit, which provides a menu-driven interface for working with Micropython boards):I press
return
to get the Micropython REPL prompt:And then type
CTRL-D
to reset the board:And then the board comes up, the code executes as expected:
(note that if you replace MicroPython with CircuitPython,the Pico will show up as a drive and you can just drag-and-drop files on it.)
使用 Pycharm、Thonny 和 VisualStudio 代码尝试了 micropython 和 Circuitpython。唯一可靠的工作是带有 Mu 编辑器的 CircuitPython。我认为这一切都与 .py 文件复制到 Pico 板的方式有关,而且寿命太短,无法进行更多诊断。 Mu 很基础,但很有效!感谢您的帮助。
Tried micropython and circuitpython with Pycharm, Thonny and VisualStudio code. The only thing that reliably works is CircuitPython with Mu editor. I think its all about the way the .py files are copied to the Pico board and life's too short to do more diagnostics. Mu is pretty basic but it works! Thanks for the help.