1
from solcx import compile_standard, install_solc
from web3 import Web3
import json


install_solc("0.8.0")


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


# Compile our solidity

compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {"*": ["abi", "metadata", "evm.bytecode", "enm.sourceMap"]}
            }
        },
    },
    solc_version="0.8.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"]

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

# for connecting to ganache

w3 = Web3(Web3.HTTPProvider("http://0.0.0.0:7545"))
chain_id = 5777
my_address = "not fill here"
private_key = "not fill here"

# Create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)

I'm getting an error while importing web3 as "Import "web3" could not be resolved"

I'm working in the venv and all package install with pip btw.

How can I fix the problem?

Shibumi
  • 11
  • 1
  • 4
  • Does import web3 work for you? The issue may have to do with your web3 version or with the virtual environment: https://ethereum.stackexchange.com/questions/94046/web3-py-attributeerror-eth-object-has-no-attribute-get-block – Raisin Oct 06 '21 at 21:35

4 Answers4

2

It works regardless of the VS Code error, better still you can restart your VS Code and the error will be gone

Zia
  • 161
  • 4
1

hi I'm also doing that same course, its not at all problem unless u installed web3.py successfully its fine VS code will show it as error but Web3 will work fine when every u run

python deploy.py

enter image description herein terminal

ToXe
  • 11
  • 1
0

For me it wasn't working with the error. So, the solution for me was installing web3 globally on my local system, outside of the virtual env using pip3 install web3 command. I'm using python3 on Ubuntu 20.04 LTS.

0

change your version of python interpreter To " python 3.8.9 " to fix it.