1
from web3 import Web3
infura_url="https://ropsten.infura.io/v3/my_key"

w3=Web3(Web3.HTTPProvider(infura_url))
print(w3.isConnected())

It is giving output as

True.

But still, it is not connected to metamask. Also when I try to access smart contract from web3.py metamask doesn't pop-up while it works perfect with remix IDE

1
  • What are you trying to accomplish? This is a standalone script that connects to an Ethereum node while MetaMask is a browser extension. Establishing a network connection is a completely different thing from a webpage "connecting" to MetaMask inside a browser via browser's internal APIs.
    – cameel
    Jan 8, 2022 at 21:11

1 Answer 1

2

Your script establishes a network connection to an Ethereum node running at Infura. This means that you can use the JSON-RPC API to send commands to the node which lets you execute transactions, check balances, etc. web3.py is using that API to let you deploy contracts and interact with them.

MetaMask is a browser extension that can also establish a connection to a blockchain node but you can't directly communicate with it through that node. At best you might be able to execute some transactions whose effects could be noticed by MetaMask and presented in its UI, but that would not actually require being connected to the same Ethereum node.

These connections are a completely different thing than a dapp connecting to the MetaMask wallet. The latter means that an app running in the browser is communicating with MetaMask directly inside that browser. Your script is not doing that. In fact, it can't because you're certainly running it outside of the browser and the browser won't normally let you interact with extensions that way.

If your goal is to execute transactions from your Python script then MetaMask does not really need to be involved. web3.py is all you need. If you want to use the accounts managed by MetaMask, you just need to get their private keys.

See also:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.