Python

錯誤:找到非十六進制數字

  • October 12, 2021

當我嘗試執行此腳本時,我似乎正在執行以下錯誤:,

from brownie import AdvancedCollectible, accounts, network, config

def main():
   dev = accounts.add(config['wallets']['from_key'])
   print(dev)

我的brownie-config.yaml:

# exclude SafeMath when calculating test coverage
# https://eth-brownie.readthedocs.io/en/v1.10.3/config.html#exclude_paths
reports:
 exclude_contracts:
   - SafeMath
dependencies:
 - smartcontractkit/chainlink-brownie-contracts@1.0.2
 - OpenZeppelin/openzeppelin-contracts@3.4.0
compiler:
 solc:
   remappings:
     - '@chainlink=smartcontractkit/chainlink-brownie-contracts@1.0.2'
     - '@openzeppelin=OpenZeppelin/openzeppelin-contracts@3.4.0'
dotenv: .env
wallets:
 from_key: ${PRIVATE_KEY}
 from_mnemonic: ${MNEMONIC} 

錯誤:

brownie run scripts/advanced_collectible/deploy_advanced.py 

Brownie v1.14.3 - Python development framework for Ethereum

NftdemoProject is the active project.

Launching 'ganache-cli --port 8545 --gasLimit 12000000 --accounts 10 --hardfork istanbul --mnemonic brownie'...

Running 'scripts/advanced_collectible/deploy_advanced.py::main'...
 File "brownie/_cli/run.py", line 49, 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/advanced_collectible/deploy_advanced.py", line 4, in main
   dev = accounts.add(config['wallets']['from_key'])
 File "brownie/network/account.py", line 132, 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 246, 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 694, in _parsePrivateKey
   return self._keys.PrivateKey(HexBytes(key))
 File "hexbytes/main.py", line 23, in __new__
   bytesval = to_bytes(val)
 File "hexbytes/_utils.py", line 17, in to_bytes
   return hexstr_to_bytes(val)
 File "hexbytes/_utils.py", line 50, in hexstr_to_bytes
   return binascii.unhexlify(ascii_hex)
Error: Non-hexadecimal digit found

我確保我的 .env 文件中的密鑰設置為export PRIVATE_KEY='0x...",那麼為什麼它無法從中提取變數,或者我在這裡錯過了一些步驟?任何意見,將不勝感激。謝謝!

我的 Windows 系統也有同樣的錯誤。當我對我的私鑰進行硬編碼時,brownie-config.yaml from_key: 0x...一切正常。所以,我認為python部分的問題是,當它試圖獲取變數時。此外,嘗試在控制台中使用也echo $Env:PRIVATE_KEY可以正常工作。

我剛剛遇到了同樣的錯誤,應該修復它的是python包python-dotenv。一旦你安裝了它,python 應該能夠讀取那些 .env 文件。

引用自:https://ethereum.stackexchange.com/questions/98811