Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
web3 method
#1
installed web3 api and cannot get it recognized.
from web3 import Web3
nonce = web3.eth.getTransactionCount(acct1)
error = instance of 'Web3' has no 'eth' member
Reply
#2
I guess your own file is named web3.py, right? Because otherwise you will get NameError. Note that you import Web3 (with capital W) from web3 (with lower w). Then in your code you use web3 (with lower w and it is not imported)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
file name is contract.py

from web3 import Web3
#from solc import compile_source
#from web3.contract import ConciseContract
#from web3.auto import Web3, EthereumTesterProvider

# code here
ganache_url = "http://127.0.0.1:7545"
web3 = Web3(Web3.HTTPProvider(ganache_url))

acct1 = "REMOVED"
acct2 = "REMOVED"

private_key = "REMOVED"

nonce = web3.eth.getTransactionCount(acct1)

tx = {
    'nonce': nonce,
    'to'   : acct2,
    'value': web3.toWei(1, 'ether'),
    'gas'  : 2000000,
    'gasPrice': web3/toWei('50', 'gwei')
}

signed_tx = web3.eth.account.signTransaction(tx, private_key)
tx_hash   = web3.eth.sendRawTransaction(signed_tx.sendRawTransaction)
print(tx_hash)
Reply
#4
I think the problem is you use web3 as variable name and it conflicts with the module name. use something different, e.g. w3.
That said, I am not sure when you want to use Web3 instance and when web3.eth. Docs is also bit confusing in this respect, e.g. Web3.eth redirects to wb3.eth
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  web3 adam2001 1 2,347 May-10-2021, 06:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020