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
相关产品推荐
相关产品推荐

