python - 在 WSL 中使用 python/brownie 时遇到问题

标签 python ethereum solidity windows-subsystem-for-linux brownie

我正在尝试通过此 freeCodeCamp video 完成简单的收藏品 NFT 教程. (我卡在了恰好从链接时间戳所在的位置开始的脚本上。)

对于那些无法打开视频的人,我正在尝试运行这个 brownie 命令:

brownie run scripts/deploy_and_create.py --network rinkeby

我收到以下错误:

dsine@DESKTOP-T74SG6U:/mnt/c/Users/dylan/projects/demos/nft-demo$ brownie run scripts/deploy_and_create.py --network rinkeby
Brownie v1.17.1 - Python development framework for Ethereum

NftDemoProject is the active project.
  File "brownie/_cli/run.py", line 50, in main
    return_value, frame = run(
  File "brownie/project/scripts.py", line 53, in run
    module = _import_from_path(script)
  File "brownie/project/scripts.py", line 149, in _import_from_path
    _import_cache[import_str] = importlib.import_module(import_str)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
  File "<frozen, line line, in in
ModuleNotFoundError: No module named 'mnt.c.Users.dylan.projects'

我在 VSCode 中从 WSL Ubuntu 终端运行它。我也试过在 powershell 中运行脚本。我确保我使用的是 Python 3 和 WSL 2。我不确定这里发生了什么。

这是我的 deploy_and_create.py 代码:

from scripts.helpful_scripts import get_account
from brownie import SimpleCollectible

sample_token_uri = "https://ipfs.io/ipfs/Qmd9MCGtdVz2miNumBHDbvj8bigSgTwnr4SbyH6DNnpWdt?filename=0-PUG.json"
OPENSEA_URL = "https://testnets.opensea.io/assets/{}/{}"

def main():
    account = get_account()
    simple_collectible = SimpleCollectible.deploy({"from":account})
    tx = simple_collectible.createCollectible(sample_token_uri, {"from": account})
    tx.wait(1)
    print(f"Awesome, you can view your NFT at {OPENSEA_URL.format(simple_collectible.address, simple_collectible.tokenCounter() - 1)}")
    print("Please wait up to 20 minutes and hit the refresh metadata button.")
    

和我的 brownie-config.yaml:

dependencies:
  - OpenZeppelin/openzeppelin-contracts@3.4.0

compiler:
  solc:
    remappings:
      - '@openzeppelin=OpenZeppelin/openzeppelin-contracts@3.4.0'

dotenv: .env

谁能帮帮我?谢谢!

最佳答案

在我看来,问题源于文件 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 现在被修改为反射(reflect)准确的文件位置而不是模块名称。 else: block 现在通过指定文件位置导入模块,然后将该文件作为模块加载。

关于python - 在 WSL 中使用 python/brownie 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70452624/

相关文章:

Python MySQL 连接器 fetchone 不返回 dict

python Spark根据值对元素进行排序

python - Web3py send_raw_transaction ValueError : {'message' : 'invalid remainder' , 'code' : -32000}

ethereum - 无法添加以太坊对等点。对等列表为空

transactions - Ethereum/Solidity - 查询交易

blockchain - 链上智能合约是如何存储的?

python - 跟踪Python进程和子进程的CPU时间

ethereum - 从 Solidity 中的映射中删除映射

javascript - 格式错误: invalid address

python 2.7 : type object "ElementTree" has no attribute "register_namespace"