繁体   English   中英

这里有什么问题?:导入“web3”无法解析导入“solcx”无法解析导入“dotenv”无法解析

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM