Search code examples
pythonpython-3.xunixpipblockchain.info-api

Can't import blockchain.info api library for python


Using this github library: blockchain.info python api

blockchain.py

from blockchain import Blockexplorer
block = blockexplorer.get_block('000000000000000016f9a2c3e0f4c1245ff24856a79c34806969f5084f410680')

When I run python blockchain.py in the command line on my mac I get this error:

Traceback (most recent call last):
  File "blockchain.py", line 1, in <module>
    from blockchain import Blockexplorer
  File "/Users/mbp13/blockchain.py", line 1, in <module>
    from blockchain import Blockexplorer
ImportError: cannot import name 'Blockexplorer'

I checked if the blockchain module is installed by pip freeze and i see blockchain ==1.4.0 in the list. So i assumed it's installed

What am I missing? Thanks


Solution

  • # blockchain.py
    from blockchain import Blockexplorer
    

    I see 2 (two) problems here:

    1. You script is called blockchain.py so from blockchain import tries to import from it instead of the blockchain package. Rename your script so it doesn't overshadow the package name.

    2. You try to import Blockexplorer while the real module is lowercase. The correct import statement (after you rename your script) is

      from blockchain import blockexplorer