gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:35:49 26 4
gpt4 key购买 nike

我正在尝试通过此 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com