xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

如何修复 Pico 内置模块 machine 在 VSCode 无法跳转到定义 bug All In One

如何修复 Pico 内置模块 machine 在 VSCode 无法跳转到定义 bug All In One

Pylance

problems

Go to Definition, No definition found for machine ❌

image

image

reasons & answers

image

solutions

  1. micropython-rp2-stubs ✅

安装 micropython-stubs(MicroPython 类型存根),这是专门给 Pylance 用的.pyi类型描述文件,补上 machine、Pin 这些的提示和跳转:

➜  Raspberry-Pi git:(master) ✗ which python3
/usr/bin/python3
➜  Raspberry-Pi git:(master) ✗ which pip3
/usr/bin/pip3
➜  Raspberry-Pi git:(master) ✗ pip3 install micropython-rp2-stubs

image

https://docs.micropython.org/en/v1.29.0/library/machine.html

或者 mip

import mip
mip.install("micropython-rp2-stubs")


  1. VSCode 配置文件 .vscode/settings.json, 添加配置 让 Pylance 识别 MicroPython 存根 ❓
{
    "python.analysis.typeCheckingMode": "basic",
    "python.analysis.typeshedPaths": ["./typings"],
    "python.analysis.extraPaths": ["./typings"]
}


demos

#!/usr/bin/env python3
# coding: utf8

import machine
import gc
import os

machine.freq()
# 125000000

print("Pico CPU 默认频率:", machine.freq())

# CPU 超频
machine.freq(300_000_000)

gc.collect()

# print("Pico 内存:", gc.mem())
print("Pico 剩余内存:", gc.mem_free())

uname = os.uname()
print("Pico Name:", uname.sysname)
print("Pico Version:", uname.version)


"""

Pico CPU 默认频率: 125000000
Pico 剩余内存: 229184
Pico Name: rp2
Pico Version: v1.29.0 on 2026-08-24 (GNU 16.1.0 MinSizeRel)

"""

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

Pico machine Module

https://docs.micropython.org/en/v1.29.0/library/machine.html

Pylance

Pylance 是微软给 VS Code 做的 Python 语言服务器(Language Server),是现在 VS Code Python 扩展默认的后端,底层基于 Pyright(静态类型检查器)

作用:负责给你提供代码提示、自动补全、类型检查、跳转定义,让 VS Code 写 Python 更好用、更快.

主要功能

  • 智能补全(IntelliSense)、函数 / 类提示
  • 鼠标悬浮文档提示、跳转定义、查找引用
  • 自动导入、代码重构
  • 静态类型检查(标红提示类型错误)
  • 自带大量第三方库的类型存根(.pyi),numpy、pandas、torch 等提示更准

https://marketplace.visualstudio.com/items?itemName=ms-Python.vscode-pylance

Announcing Pylance: Fast, feature-rich language support for Python in Visual Studio Code

June 30th, 2020

https://devblogs.microsoft.com/python/announcing-pylance-fast-feature-rich-language-support-for-python-in-visual-studio-code/

Python 数字分隔符 / 下划线分隔符

作用:在写大整数的时候,方便读数和数零的个数

# 数字分隔符
machine.freq(125000000)
# 等价于
machine.freq(125_000_000)

打印输出的时候,不会显示下划线,输出还是正常数字

big_int = 125_000_000
print(big_int)
# 125000000


大多现代语言,如(Java、C++、JavaScript)后来也都支持使用下划线分隔数字,作用一致;
Python 3.5 及更早版本写 125_000_000 会直接报错。

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2026-11-15 01:06  xgqfrms  阅读(7)  评论(5)    收藏  举报