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

如何解决Brownie导出私钥时出现的.env环境变量加载失效问题

Brownie无法加载.env环境变量导致私钥读取失败问题

碰到同类Brownie环境变量加载问题,为Patrick Collins课程相关章节学习时复现,使用的部署代码如下:

from brownie import accounts, config, SimpleStorage, network

def deploy_simple_storage():
    account = get_account()
    simple_storage = SimpleStorage.deploy({"from": account})
    stored_value = simple_storage.retrieve()
    print(stored_value)
    transaction = simple_storage.store(15, {"from": account})
    transaction.wait(1)
    updated_stored_value = simple_storage.retrieve()
    print(updated_stored_value)

def get_account():
    if network.show_active() == "development":
        return accounts[0]
    else:
        return accounts.add(config["wallets"]["from_key"])

def main():
    deploy_simple_storage()

现有配置及排查情况:

  • 使用Brownie v1.14.6版本
  • .env与brownie-config.yaml存放在同一项目目录下
  • 已尝试导入os库执行print(os.getenv("PRIVATE_KEY")),返回结果为none,手动添加PATH变量后问题仍存在

判断为.env环境变量加载异常,运行部署命令时返回如下报错:

PS C:\Users\ericl\demos\brownie_simple_storage> brownie run .\scripts\deploy.py --network rinkeby
INFO: Could not find files for the given pattern(s).
Brownie v1.14.6 - Python development framework for Ethereum

BrownieSimpleStorageProject is the active project.

Running 'scripts\deploy.py::main'...
  File "c:\users\ericl\appdata\local\programs\python\python39\lib\site-packages\brownie\_cli\run.py", line 49, in main
    return_value, frame = run(
  File "c:\users\ericl\appdata\local\programs\python\python39\lib\site-packages\brownie\project\scripts.py", line 103, in run
    return_value = f_locals[method_name](*args, **kwargs)
  File ".\scripts\deploy.py", line 23, in main
    deploy_simple_storage()
  File ".\scripts\deploy.py", line 5, in deploy_simple_storage
    account = get_account()
  File ".\scripts\deploy.py", line 19, in get_account
    return accounts.add(config["wallets"]["from_key"])
  File "c:\users\ericl\appdata\local\programs\python\python39\lib\site-packages\brownie\network\account.py", line 132, in add
    w3account = web3.eth.account.from_key(private_key)
_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "c:\users\ericl\appdata\local\programs\python\python39\lib\site-packages\eth_account\account.py", line 246, in from_key
    key = self._parsePrivateKey(private_key)
  File "c:\users\ericl\appdata\local\programs\python\python39\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "c:\users\ericl\appdata\local\programs\python\python39\lib\site-packages\eth_account\account.py", line 696, in _parsePrivateKey
    raise ValueError(
ValueError: The private key must be exactly 32 bytes long, instead of 0 bytes.

备注:使用的操作系统为Windows 10

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 02:15:03