简体   繁体   中英

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. PFA the screenshot of the terminal output on VScode. --> 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"]

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

["metadata"] )["output"]["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())

Terminal 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. For example: pip install web3 .

If you are not sure if these packages are installed, you can use pip show... command to view. For example: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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