You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python区块链开发报错:'account'未定义(Pylance)求代码排查

解决Pylance报错"'account' is not defined"的问题

你的代码存在三个明确问题,导致了报错:

  • 命名混淆与对象误用:你从brownie导入的是accounts(复数),但代码里错误使用account.load()(单数),同时还将变量名设为account,造成了命名冲突。
  • 变量作用域错误:print(account)写在函数deploy_simple_storage外部,而account是函数内部定义的局部变量,外部无法访问。
  • 冗余导入:from eth_account import Account这行代码未被使用,可以删除。

修正后的代码如下:

import brownie
from brownie import accounts

def deploy_simple_storage():
    # 使用正确的accounts(复数)调用load方法
    account = accounts.load("freecodecamp-account")
    # 将print语句移到函数内部,确保能访问局部变量account
    print(account)

def main():
    deploy_simple_storage()

内容的提问来源于stack exchange,提问作者whatever12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 23:20:28