[英]What is the issue here?: Import "web3" could not be resolved Import "solcx" could not be resolved Import "dotenv" could not be resolved
任何人都可以幫助解決這些問題嗎? 如前所述,我在導入 web3、solcx、dotenv 時遇到問題。 PFA 終端 output 在 VScode 上的截圖。 --> 1
代碼:
import json
from web3 import Web3
from solcx import compile_standard, install_solc
import os
from dotenv import load_dotenv
load_dotenv()
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
print("Installing...")
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"]
}
}
},
},
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”][“字節碼”][“對象”]
abi = json.loads(
compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]
[“元數據”])[“輸出”][“abi”]
w3 = Web3(Web3.HTTPProvider("http://0.0.0.0:8545"))
chain_id = 1337
my_address = "0xdbB4A708755dfD59f9c4b100B2BE23a6d2EB7D57"
private_key =
“ffdd7a010ab8c089d95a9c2ff24e75b21744b5db26c3cd66d14f8e91c46afcc4”
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
nonce = w3.eth.getTransactionCount(my_address)
transaction = SimpleStorage.constructor().buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
}
)
signed_txn = w3.eth.account.sign_transaction(transaction,
private_key=private_key)
print("Deploying Contract!")
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
print("Waiting for transaction to finish...")
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Done! Contract deployed to {tx_receipt.contractAddress}")
simple_storage =
w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)
print(f"Initial Stored Value
{simple_storage.functions.retrieve().call()}")
greeting_transaction =
simple_storage.functions.store(15).buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce + 1,
}
)
signed_greeting_txn = w3.eth.account.sign_transaction(
greeting_transaction, private_key=private_key
)
tx_greeting_hash =
w3.eth.send_raw_transaction(signed_greeting_txn.rawTransaction)
print("Updating stored Value...")
tx_receipt =
w3.eth.wait_for_transaction_receipt(tx_greeting_hash)
print(simple_storage.functions.retrieve().call())
終端Output:
Traceback (most recent call last):
File "c:\Users\ashvi\demos\web3_py_simple_storage\sam", line 3,
in <module>
from web3 import Web3
ModuleNotFoundError: No module named 'web3'
請嘗試pip install...
命令來安裝這些包。 例如: pip install web3
。
如果不確定是否安裝了這些包,可以使用pip show...
命令查看。 例如: pip show dotenv
。 通過該命令還可以查看package的安裝位置,請確保package安裝在您當前的環境中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.