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

Windows VSCode下无法运行Brownie脚本及控制台问题求助

Windows下VSCode中Brownie相关问题的原因与解决方法

问题描述

1. Python脚本导入Brownie报错

Traceback (most recent call last):
  File "*filepath*", line 3, in <module>
    from brownie import *
ModuleNotFoundError: No module named 'brownie'

2. 运行brownie console报错

INFO: Could not find files for the given pattern(s).
Brownie v1.19.0 - Python development framework for Ethereum

No project was loaded.
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'C:\Users\user\AppData\Roaming\npm\node_modules\ganache\dist\node\cli.js'
←[90m    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:778:27)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m
←[90m    at node:internal/main/run_main_module:17:47←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m,
  requireStack: []
}
  File "C:\Users\user\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\_cli\__main__.py", line 64, in main
    importlib.import_module(f"brownie._cli.{cmd}").main()
  File "C:\Users\user\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\_cli\console.py", line 58, in main
    network.connect(CONFIG.argv["network"])
  File "C:\Users\user\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\network\main.py", line 50, in connect
    rpc.launch(active["cmd"], **active["cmd_settings"])
  File "C:\Users\user\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\network\rpc\__init__.py", line 76, in launch
    self.process = self.backend.launch(cmd, **kwargs)
  File "C:\Users\user\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\network\rpc\ganache.py", line 70, in launch
    ganache_version = get_ganache_version(cmd_list[0])
  File "C:\Users\user\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\network\rpc\ganache.py", line 115, in get_ganache_version
    raise ValueError("could not read ganache version: {}".format(ganache_version_stdout))
ValueError: could not read ganache version: b''

原因分析

问题1的原因

pipx会为每个包创建独立虚拟环境,你通过pipx安装的Brownie存放在C:\Users\user\.local\pipx\venvs\eth-brownie中。安装VSCode Python扩展后,扩展默认选中的可能是系统全局Python解释器或其他项目虚拟环境,导致当前脚本使用的解释器找不到Brownie模块。

问题2的原因

  1. Ganache路径异常:Ganache可能被意外卸载,或安装路径下的文件被删除/移动,导致Brownie无法定位指定的cli.js文件。
  2. 终端环境变量问题:VSCode安装Python扩展后,终端环境变量加载异常,无法识别全局安装的Ganache路径。
  3. 依赖适配问题:环境变动后触发了Brownie与Ganache的版本适配冲突(Brownie v1.19.0对Ganache版本有特定要求)。

解决方法

针对问题1

  1. 切换VSCode的Python解释器:
    • 点击VSCode左下角的Python版本标识(如Python 3.10.x)。
    • 在弹出列表中选择pipx创建的Brownie虚拟环境,路径为C:\Users\user\.local\pipx\venvs\eth-brownie\Scripts\python.exe。
    • 重新运行脚本即可。
  2. 通过pipx直接运行脚本:
    • 在终端执行pipx run brownie run your_script.py,调用Brownie的虚拟环境运行脚本。

针对问题2

  1. 重新安装Ganache:
    • 在终端执行npm install -g ganache,重新全局安装Ganache,确保C:\Users\user\AppData\Roaming\npm\node_modules\ganache路径存在且包含dist\node\cli.js。
  2. 重启VSCode加载环境变量:
    • 关闭并重新打开VSCode,让终端重新加载系统环境变量,确保能识别Ganache的全局路径。
  3. 指定Brownie网络运行:
    • 执行brownie console --network ganache-local,直接调用预设的本地Ganache网络配置。
  4. 重装Brownie修复依赖:
    • 执行pipx reinstall eth-brownie,修复Brownie可能存在的依赖损坏问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 09:03:19