Brownie / Rinkeby: Unable to expand environment variable in host setting

Viewed 977

I was following the tutorial from this video and now I'm stuck during deploying a contract to rinkeby testnet.

If I run brownie run scripts/deploy.py --network rinkeby I get an error:

BrownieProject is an active project.
  File "brownie/_cli/__main__.py", line 64, in main
    importlib.import_module(f"brownie._cli.{cmd}").main()
  File "brownie/_cli/run.py", line 44, in main
    network.connect(CONFIG.argv["network"])
  File "brownie/network/main.py", line 40, in connect
    web3.connect(host, active.get("timeout", 30))
  File "brownie/network/web3.py", line 52, in connect
    uri = _expand_environment_vars(uri)
  File "brownie/network/web3.py", line 183, in _expand_environment_vars
    raise ValueError(f"Unable to expand environment variable in host setting: '{uri}'")
ValueError: Unable to expand environment variable in host setting: 'https://rinkeby.infura.io/v3/$WEB3_INFURA_PROJECT_ID'

I checked the brownie-config.yaml file and .env for typing errors but haven't found anything.

brownie-config.yaml

dotenv: .env
wallets:
  from_key: ${PRIVATE_KEY}

I already created an infura api and add it in the .env file as export WEB3_INFURA_PROJECT_ID=abc12345656789.

If I run the command brownie run scripts/deploy.py everything works fine so I can exclude any typo. Does someone have an idea what's the problem?

I use Brownie v1.17.2

3 Answers

I believe you are not loading environment variable in your file. install python-dotenv

  pip install python-dotenv

In your deploy.py if .env is in the same directory:

import os
from dotenv import load_dotenv 

#default directory for .env file is the current directory
#if you set .env in different directory, put the directory address load_dotenv("directory_of_.env)
load_dotenv()

then use it like this:

 private_key=os.getenv("PRIVATE_KEY")

Typically, this means your environment variables are not set correctly, and it looks like in this case it's your WEB3_INFURA_PROJECT_ID.

You can fix it by setting the variable in your .env file and adding dotenv: .env to your brownie-config.yaml.

brownie-config.yaml:

dotenv: .env

.env:

export WEB3_INFURA_PROJECT_ID=YOUR_PROJECT_ID_HERE

Remember to save these files.

Additionally, you should be on at least brownie version v1.14.6. You can find out what version you're on with:

brownie --version

If you know how to set environment variables you might want to check if you're setting them correctly.

if you are sure you set the .env correctly you can try sourcing your .env file in the terminal:

source .env

and then try running your script again.

Related