'Trouble using python/brownie in WSL

I'm trying to complete the simple collectible NFT tutorial from this freeCodeCamp video. (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 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. I've tried running the script in powershell too. I've ensured I'm using Python 3 and WSL 2. I'm unsure of what is happening here.

Here is my deploy_and_create.py code:

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:

dependencies:
  - OpenZeppelin/[email protected]

compiler:
  solc:
    remappings:
      - '@openzeppelin=OpenZeppelin/[email protected]'

dotenv: .env

Can anyone help me? Thank you!



Solution 1:[1]

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). The way it is written, it will incorrectly identify Users.username as "not a module."

The solution: replace _import_from_path with the following

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. The else: block now imports the module by specifying the file location and then loading that file as a module.

Solution 2:[2]

I had same troubles using WSL and I solved copying my project folder into "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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 martcerv