Solidity

出現“無法解析導入“web3””錯誤

  • March 26, 2022
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)

我在導入 web3 時遇到錯誤,因為“導入“web3”無法解決”

我在 venv 工作,所有軟體包都使用 pip btw 安裝。

我該如何解決這個問題?

嗨,我也在做同樣的課程,除非你成功安裝了 web3.py,否則它完全沒有問題,它的精細 VS 程式碼會將其顯示為錯誤,但是當你每次執行時,Web3 都會正常工作

> > > > > > 蟒蛇部署.py > > > > > > > > >

在此處輸入圖像描述在終端

無論 VS Code 錯誤如何,它都能正常工作,更好的是您可以重新啟動 VS Code,錯誤就會消失

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