Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Given this code:

from solcx import compile_standard, install_solc
import json

install_solc('0.6.0')

with open('./SimpleStorage.sol', 'r') as f:
    simple_storage_file = f.read()

compiled_sol = compile_standard(
    {
        'language': 'Solidity',
        'sources': {'SimpleStorage.sol': {'content': simple_storage_file}},
        'settings': {
            'outputSelection': {
                '*': {'*': ['abi', 'metadata', 'evm.sourceMap']}
            }
        }
    },
    solc_version = '0.6.0',
)

with open('compiled_code.json', 'w') as file:
    json.dump(compiled_sol, file)

bytecode = compiled_sol['contracts']['SimpleStorage.sol']['SimpleStorage']['evm']['bytecode']['object']
print(f'bytecode: {bytecode}')

I get this output:

Traceback (most recent call last):
  File "deploy.py", line 25, in <module>
    bytecode = compiled_sol['contracts']['SimpleStorage.sol']['SimpleStorage']['evm']['bytecode']['object']
KeyError: 'evm' 

Here is my compiled_code.json:

{
    "contracts": {
        "SimpleStorage.sol": {
            "SimpleStorage": {
                "abi": [
                    {
                        "inputs": [
                            {
                                "internalType": "string",
                                "name": "_name",
                                "type": "string"
                            },
                            {
                                "internalType": "uint256",
                                "name": "_favoriteNumber",
                                "type": "uint256"
                            }
                        ],
                        "name": "addPerson",
                        "outputs": [],
                        "stateMutability": "nonpayable",
                        "type": "function"
                    },
                    {
                        "inputs": [
                            {
                                "internalType": "string",
                                "name": "",
                                "type": "string"
                            }
                        ],
                        "name": "nameToFavoriteNumber",
                        "outputs": [
                            {
                                "internalType": "uint256",
                                "name": "",
                                "type": "uint256"
                            }
                        ],
                        "stateMutability": "view",
                        "type": "function"
                    },
                    {
                        "inputs": [
                            {
                                "internalType": "uint256",
                                "name": "",
                                "type": "uint256"
                            }
                        ],
                        "name": "people",
                        "outputs": [
                            {
                                "internalType": "uint256",
                                "name": "favoriteNumber",
                                "type": "uint256"
                            },
                            {
                                "internalType": "string",
                                "name": "name",
                                "type": "string"
                            }
                        ],
                        "stateMutability": "view",
                        "type": "function"
                    },
                    {
                        "inputs": [],
                        "name": "retrieve",
                        "outputs": [
                            {
                                "internalType": "uint256",
                                "name": "",
                                "type": "uint256"
                            }
                        ],
                        "stateMutability": "view",
                        "type": "function"
                    },
                    {
                        "inputs": [
                            {
                                "internalType": "uint256",
                                "name": "_favoriteNumber",
                                "type": "uint256"
                            }
                        ],
                        "name": "store",
                        "outputs": [],
                        "stateMutability": "nonpayable",
                        "type": "function"
                    }
                ],
                "metadata": "{\"compiler\":{\"version\":\"0.6.0+commit.26b70077\"},\"language\":\"Solidity\",\"output\":{\"abi\":[{\"inputs\":[{\"internalType\":\"string\",\"name\":\"_name\",\"type\":\"string\"},{\"internalType\":\"uint256\",\"name\":\"_favoriteNumber\",\"type\":\"uint256\"}],\"name\":\"addPerson\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"name\":\"nameToFavoriteNumber\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"people\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"favoriteNumber\",\"type\":\"uint256\"},{\"internalType\":\"string\",\"name\":\"name\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"retrieve\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_favoriteNumber\",\"type\":\"uint256\"}],\"name\":\"store\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}],\"devdoc\":{\"methods\":{}},\"userdoc\":{\"methods\":{}}},\"settings\":{\"compilationTarget\":{\"SimpleStorage.sol\":\"SimpleStorage\"},\"evmVersion\":\"istanbul\",\"libraries\":{},\"metadata\":{\"bytecodeHash\":\"ipfs\"},\"optimizer\":{\"enabled\":false,\"runs\":200},\"remappings\":[]},\"sources\":{\"SimpleStorage.sol\":{\"keccak256\":\"0x2d1fb3be1abc6740b1dbd06a57fa5dbb4c40f1f01bb6378bdc5eeb316de0e559\",\"urls\":[\"bzz-raw://8f44f4925f7c11f296c8f5fc22c68f281d2a62afbe119c26ac0309b9d2063b98\",\"dweb:/ipfs/QmSuUT9ouegfFVD5jdmFrhZzg3hSfwL64YyP27USpG6fFA\"]}},\"version\":1}"
            }
        }
    },
    "sources": {
        "SimpleStorage.sol": {
            "id": 0
        }
    }
}
You must be logged in to vote

Hello @spencergoff the reason is you are missing the following:

"outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
                }
            }

"evm.bytecode", give it a try.

Replies: 2 comments · 10 replies

Comment options

Hello @spencergoff the reason is you are missing the following:

"outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
                }
            }

"evm.bytecode", give it a try.

You must be logged in to vote
5 replies
@spencergoff
Comment options

Thanks @cromewar! That worked.

@vicshi06
Comment options

Hey @spencergoff how did you copy and paste your code into the question like that?

@spencergoff
Comment options

@vicshi06 cromewar edited the question so the code block begins with ```Python

@vicshi06
Comment options

Thanks!

@JermyNeutron
Comment options

thanks! For me, I realized I used "emv" instead of "evm" and it would have taken me longer to see that if I hadn't seen your response

Answer selected by spencergoff
Comment options

I have the exact same issue, BUT in my case I didn't miss the "evm.bytecode" in my code, and I still get the same error. I cannot find a solution anywhere on the whole internet. What should I do? Here's the codeÚ

from solcx import compile_standard, install_solc
import json

install_solc("0.6.0")
with open("./SimpleStorage.sol", "r") as file:
    simple_storage_file = file.read()

# compile our solidity
install_solc("0.6.0")
compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": { "outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]}
                }
            },
    },
)
with open("compiled_code.json", "w") as file:
    json.dump(compiled_sol, file)

# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["evm"]["bytecode"]["object"]

# get abi
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]
print(abi)

`

You must be logged in to vote
5 replies
@cromewar
Comment options

Hello @TheoremaLC could you share please the error log here?

@TheoremaLC
Comment options

Sure!

(venv) Babas-MacBook-Pro:web3_py_simple_storage babadag$ python3 deploy.py
Traceback (most recent call last):
File "/Users/babadag/demos/web3_py_simple_storage/deploy.py", line 25, in
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["evm"]["bytecode"]["object"]
KeyError: 'evm'

@cromewar
Comment options

Jum is is weird as it seems is not taking the compiled sol, could you please copy and paste this on your code? (just replace it)

compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
                }
            }
        },
    },
    solc_version="0.6.0",
)

with open("compiled_code.json", "w") as file:
    json.dump(compiled_sol, file)

# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
    "bytecode"
]["object"]
@tkorkmazeth
Comment options

Jum is is weird as it seems is not taking the compiled sol, could you please copy and paste this on your code? (just replace it)

compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
                }
            }
        },
    },
    solc_version="0.6.0",
)

with open("compiled_code.json", "w") as file:
    json.dump(compiled_sol, file)

# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
    "bytecode"
]["object"]

Hey! So I have searched the whole Internet for KeyError: 'evm' too and replaced with the code you have given but it did not work. Please is there any solutions cause I stuck here and can not progress.

@cromewar
Comment options

This is really strange. I'll need more info on this, please answer the following:

  1. Which version of Mac you have ( is the newest one with a m1 chip?)
  2. Which version of python you have installed?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
6 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.