简体   繁体   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

Can anyone help with these issues?任何人都可以帮助解决这些问题吗? I'm having trouble importing web3, solcx, dotenv, as aforementioned.如前所述,我在导入 web3、solcx、dotenv 时遇到问题。 PFA the screenshot of the terminal output on VScode. PFA 终端 output 在 VScode 上的截图。 --> 1 --> 1

Code:代码:

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"][ "bytecode" ]["object"] [“SimpleStorage”][“evm”][“字节码”][“对象”]

abi = json.loads(
compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"] 

["metadata"] )["output"]["abi"] [“元数据”])[“输出”][“abi”]

w3 = Web3(Web3.HTTPProvider("http://0.0.0.0:8545"))
chain_id = 1337
my_address = "0xdbB4A708755dfD59f9c4b100B2BE23a6d2EB7D57"
private_key = 

"ffdd7a010ab8c089d95a9c2ff24e75b21744b5db26c3cd66d14f8e91c46afcc4" “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())

Terminal Output:终端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'

Please try pip install... command to install these packages.请尝试pip install...命令来安装这些包。 For example: pip install web3 .例如: pip install web3

If you are not sure if these packages are installed, you can use pip show... command to view.如果不确定是否安装了这些包,可以使用pip show...命令查看。 For example: pip show dotenv .例如: pip show dotenv With this command, you can also check the installation location of the package, please make sure that the package is installed in your current environment.通过该命令还可以查看package的安装位置,请确保package安装在您当前的环境中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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