【问题标题】:eth-brownie - No module named <Users.someuser>eth-brownie - 没有名为 <Users.someuser> 的模块
【发布时间】:2021-08-10 05:53:25
【问题描述】:
尝试在 MacOS 上运行 eth-brownie 脚本时出现此错误
ModuleNotFoundError: 没有名为“Users.xyz”的模块
运行命令:
brownie run scripts/mainnet/poolUpdaterMainNet.py --network bsc-main
如果有人能帮忙就好了。
【问题讨论】:
标签:
python
macos
blockchain
solidity
cryptocurrency
【解决方案1】:
我使用的是 Python 3.9.6,但仅使用我的 MacOS 时遇到了类似的问题,在我的 Linux 机器上没有发生。
无论哪种方式,我认为我找到了解决方案(至少对于我的版本而言)。问题源于文件 brownie/project/scripts.py 中的函数 _import_from_path (应该在 eth-brownie 文件夹中找到,无论您安装在哪里)。它的编写方式会错误地将 Users.username 识别为“不是模块”。
我的解决方案:将 _import_from_path 替换为以下内容
def _import_from_path(path: Path) -> ModuleType:
# Imports a module from the given path
import_str = "/" + "/".join(path.parts[1:-1] + (path.stem,))+'.py'
if import_str in _import_cache:
importlib.reload(_import_cache[import_str])
else:
spec = importlib.util.spec_from_file_location('.'+path.stem,import_str)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
_import_cache[import_str] = module
return _import_cache[import_str]
说明:import_str 现在被修改以反映确切的文件位置而不是模块名称。 else: 块现在通过指定文件位置然后将该文件作为模块加载来导入模块。我不确定这是否会破坏其他操作系统中的任何功能,但我很高兴将它用作我的 Mac 的修补程序——我现在可以运行我的脚本文件夹。