简体   繁体   English

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

[英]Trouble using python/brownie in WSL

I'm trying to complete the simple collectible NFT tutorial from this freeCodeCamp video .我正在尝试通过这个freeCodeCamp 视频完成简单的收藏品 NFT 教程。 (I'm getting stuck on the script that starts exactly where the link is timestamped to.) (我被卡在了恰好在链接时间戳所在的位置开始的脚本上。)

For those that can't open the video, I'm trying to run this brownie command:对于那些无法打开视频的人,我正在尝试运行这个 brownie 命令:

brownie run scripts/deploy_and_create.py --network rinkeby

and I'm getting the following error:我收到以下错误:

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'

I'm running this from the WSL Ubuntu terminal inside VSCode.我从 VSCode 内的 WSL Ubuntu 终端运行它。 I've tried running the script in powershell too.我也试过在 powershell 中运行脚本。 I've ensured I'm using Python 3 and WSL 2. I'm unsure of what is happening here.我确保我使用的是 Python 3 和 WSL 2。我不确定这里发生了什么。

Here is my deploy_and_create.py code:这是我的 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.")
    

and my brownie-config.yaml:和我的 brownie-config.yaml:

dependencies:
  - OpenZeppelin/openzeppelin-contracts@3.4.0

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

dotenv: .env

Can anyone help me?谁能帮我? Thank you!谢谢!

It seems to me like the issue stems from the function _import_from_path in the file brownie/project/scripts.py (that should be found in your eth-brownie folder, wherever you installed it).在我看来,问题源于文件 brownie/project/ _import_from_path中的 function _import_from_path (应该在您的 eth-brownie 文件夹中找到,无论您安装在哪里)。 The way it is written, it will incorrectly identify Users.username as "not a module."它的编写方式会错误地将 Users.username 识别为“不是模块”。

The solution: replace _import_from_path with the following解决方案:将 _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]

Explanation: import_str is now modified to reflect the exact file location instead of a module name.说明: import_str现在被修改以反映确切的文件位置而不是模块名称。 The else: block now imports the module by specifying the file location and then loading that file as a module. else:块现在通过指定文件位置然后将该文件作为模块加载来导入模块。

I had same troubles using WSL and I solved copying my project folder into "home".我在使用 WSL 时遇到了同样的麻烦,我解决了将我的项目文件夹复制到“home”的问题。

You can do it using this command:您可以使用此命令执行此操作:

cp -R <source_folder> <destination_folder> 

To make it clearer my implementation were this:为了更清楚地说明我的实现是这样的:

cp -R /mnt/c/users/macb/desktop/macb/personal/codigos/blockchain/brownie_simple_storage /home/martcerv

I recommend to navigate through your directories to know the paths you desire.我建议浏览您的目录以了解您想要的路径。

I am not able to add comment.我无法添加评论。 I will delete this if it is not helpful.如果没有帮助,我会删除它。 I am not able to replicate this error in my WSL2 environment.我无法在我的 WSL2 环境中复制此错误。 Same code and similar location works fine within WSL for me.对我来说,相同的代码和相似的位置在 WSL 中运行良好。 Could you also post directory permissions information and may be some more debug information such as trace.您是否还可以发布目录权限信息,并且可能是一些更多的调试信息,例如跟踪。 Try also the pdb debugging option.也试试 pdb 调试选项。 https://ethereum.stackexchange.com/questions/112558/how-to-debug-brownie-in-vscode . https://ethereum.stackexchange.com/questions/112558/how-to-debug-brownie-in-vscode How about other examples or normal non-brownie python codes.其他示例或普通的非布朗尼 python 代码怎么样。 Do they work?做他们的工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM