'Brownie does not export .env variables
I'm following a tutorial that uses brownie in python to deploy a contract. If the contract is to be deployed onto a testnet (rinkeby in this case), the program is supposed to get the private key from the environment variables, but when trying to run the deploy.py script as:
brownie run scripts/deploy.py --network rinkeby
I get the following error:
Running 'scripts/deploy.py::main'...
File "brownie/_cli/run.py", line 50, in main
return_value, frame = run(
File "brownie/project/scripts.py", line 103, in run
return_value = f_locals[method_name](*args, **kwargs)
File "./scripts/deploy.py", line 10, in main
deploy_fund_me()
File "./scripts/deploy.py", line 5, in deploy_fund_me
account = get_account()
File "./scripts/helpful_scripts.py", line 19, in get_account
return accounts.add(config["wallets"]["from_key"])
File "brownie/network/account.py", line 142, in add
w3account = web3.eth.account.from_key(private_key)
File "eth_utils/decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "eth_account/account.py", line 250, in from_key
key = self._parsePrivateKey(private_key)
File "eth_utils/decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "eth_account/account.py", line 776, in _parsePrivateKey
raise ValueError(
ValueError: The private key must be exactly 32 bytes long, instead of 0 bytes.
Some background on this error: I have a .env file in the same directory as my brownie-config.yml, containing the following:
export PRIVATE_KEY={my_private_key}
where {my_private_key} is a my actual private key in hex.
The brownie-config.yml looks like this:
dependencies:
# - <organization/repo>@<version>
- smartcontractkit/[email protected]
compiler:
solc:
remappings:
- '@chainlink=smartcontractkit/[email protected]'
dotenv: .env
wallets:
from_key: ${PRIVATE_KEY}
From my understanding, brownie should use the .env file to export the key as an environment variable and then put it into some brownie config file (probably when the code gets compiled?), where brownie will look for it when the contract gets deployed. It also does not change anything if I export this environment variable manually before executing the code.
P.S: The function that seems to cause the error:
def get_account():
if network.show_active() == "development":
return accounts[0]
else:
return accounts.add(config["wallets"]["from_key"])
Solution 1:[1]
I am also getting the same error I tried to directly give the private key 0x.... inside brownie-config.yaml file and it work, but I think this is not the secure way to get the private key.
dotenv: .env wallets: from_key: 0x....
Solution 2:[2]
I have a similar issue and not up to this point in the tutorial... My issue is that when I run the deploy.py script with the account line updates to:
def deploy_simple_storage():
# account = accounts.load("Account")
account = accounts.add(os.getenv("PRIVATE-KEY"))
print(account)
Instead of getting the correct account to print it generates a new one with a new mnemonic and account...
my .env file is just
export PRIVATE_KEY={My Private key prefixed w/ 0x}
and my brownie-config.yaml file is:
dotenv: .env
Looking at the reply to the post, when I create a test script and just use:
import os
def test():
print(os.getenv("PRIVATE-KEY"))
def main():
test()
I get the following results: Brownie v1.16.4 - Python development framework for Ethereum
BrownieSimpleStorageProject is the active project.
Launching 'ganache-cli.cmd --port 8545 --gasLimit 12000000 --accounts 10 --hardfork istanbul --mnemonic brownie'...
Running 'scripts\test.py::main'... None Terminating local RPC client...
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 | user18168385 |
| Solution 2 | USCMigs |
