Bitcoin Forum
May 07, 2024, 04:59:40 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Cannot import python-bitcoinlib into a simple script  (Read 70 times)
zander1983 (OP)
Member
**
Offline Offline

Activity: 65
Merit: 24


View Profile
December 24, 2022, 04:21:55 PM
Last edit: December 24, 2022, 05:48:15 PM by zander1983
 #1

Hi
Im writing a simple python script to connect to a local bitcoin node and list transactions but I cant get the thing working.

I created a virtual environment with:

    
Code:
virtualenv bitcoinenv

And then activated:

    
Code:
source bitcoinenv/bin/activate

The I installed python-bitcoinlib with:

    
Code:
pip install python-bitcoinlib

I have a simple script:

Code:
    import requests
    import bitcoin.rpc

    # Connect to the local Bitcoin node
    connection = bitcoin.rpc.Proxy()

    # Get a list of unspent transactions
    unspent_transactions = connection.listunspent()

    # Print the unspent transactions
    print(unspent_transactions)

And I run this with:

    
Code:
python bitcoin.py

But I get the error:

Code:
    import bitcoin.rpc
    ModuleNotFoundError: No module named 'bitcoin.rpc'; 'bitcoin' is not a package


Heres more information about my environment. When I run:

    
Code:
which python

I get:

    /Users/me/Bitcoin/exchange/bitcoinenv/bin/python

When I run:
  
    
Code:
echo $VIRTUAL_ENV

I get:

    
Code:
/Users/me/Bitcoin/exchange/bitcoinenv
When I go to `/Users/me/Bitcoin/exchange/bitcoinenv/lib/python3.8/site-packages` and list the contents I see python_bitcoinlib-0.11.2.dist-info amongst others listed (which I think is correct).


When I run `pip --version` I get:


    
Code:
pip 22.3.1 from /Users/me/Bitcoin/exchange/bitcoinenv/lib/python3.8/site-packages/pip (python 3.8)

And when I run `python --version` I get:

    
Code:
Python 3.8.0

Has anyone any idea why I cant import the python-bitcoinlib library?

Thanks





EDIT:

I have changed the script to:

Code:
import bitcoin
from bitcoin.rpc import Proxy

# create a connection to the local bitcoin node
conn = Proxy(
    network='regtest'  # specify the network parameters as 'regtest'
)

# list the transactions in the wallet
print(conn.listtransactions())

When I run with
Code:
python3 scripts.py
I see the error:

Code:
    from bitcoin.rpc import Proxy
ModuleNotFoundError: No module named 'bitcoin.rpc'


This is VERY frustrating! Any ideas?
1715101180
Hero Member
*
Offline Offline

Posts: 1715101180

View Profile Personal Message (Offline)

Ignore
1715101180
Reply with quote  #2

1715101180
Report to moderator
1715101180
Hero Member
*
Offline Offline

Posts: 1715101180

View Profile Personal Message (Offline)

Ignore
1715101180
Reply with quote  #2

1715101180
Report to moderator
1715101180
Hero Member
*
Offline Offline

Posts: 1715101180

View Profile Personal Message (Offline)

Ignore
1715101180
Reply with quote  #2

1715101180
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715101180
Hero Member
*
Offline Offline

Posts: 1715101180

View Profile Personal Message (Offline)

Ignore
1715101180
Reply with quote  #2

1715101180
Report to moderator
1715101180
Hero Member
*
Offline Offline

Posts: 1715101180

View Profile Personal Message (Offline)

Ignore
1715101180
Reply with quote  #2

1715101180
Report to moderator
1715101180
Hero Member
*
Offline Offline

Posts: 1715101180

View Profile Personal Message (Offline)

Ignore
1715101180
Reply with quote  #2

1715101180
Report to moderator
WhyFhy
Hero Member
*****
Offline Offline

Activity: 1430
Merit: 513



View Profile
December 24, 2022, 04:29:05 PM
 #2

VIRTUAL_ENV is likely running from the wrong directory if bitcoin.rpc ModuleNotFoundError: is a line 1 error.

  BTC
.
BTC
.
 BTC
.
BTC
/]..[banned mixer]..
██
██
██
██
██
██
██

██

██

██

██
/]YOUR OPPORTUNITY TO
HAVE BITCOIN BUSINESS

██
██
██
██
██
██
██

██

██

██

██
.
  BTC
. BTC
.
.
 
BTC
  BTC
zander1983 (OP)
Member
**
Offline Offline

Activity: 65
Merit: 24


View Profile
December 24, 2022, 05:24:31 PM
 #3

VIRTUAL_ENV is likely running from the wrong directory if bitcoin.rpc ModuleNotFoundError: is a line 1 error.


can you expand on this?
WhyFhy
Hero Member
*****
Offline Offline

Activity: 1430
Merit: 513



View Profile
December 24, 2022, 07:37:41 PM
 #4

VIRTUAL_ENV is likely running from the wrong directory if bitcoin.rpc ModuleNotFoundError: is a line 1 error.


can you expand on this?

 I'm guessing your pip or py environment paths are wrong. Or you are actually missing the bitcoin.rpc module from bitcoinlib try this.
 
Code:
pip install --upgrade bitcoin

  BTC
.
BTC
.
 BTC
.
BTC
/]..[banned mixer]..
██
██
██
██
██
██
██

██

██

██

██
/]YOUR OPPORTUNITY TO
HAVE BITCOIN BUSINESS

██
██
██
██
██
██
██

██

██

██

██
.
  BTC
. BTC
.
.
 
BTC
  BTC
zander1983 (OP)
Member
**
Offline Offline

Activity: 65
Merit: 24


View Profile
December 25, 2022, 02:13:49 PM
 #5

VIRTUAL_ENV is likely running from the wrong directory if bitcoin.rpc ModuleNotFoundError: is a line 1 error.


can you expand on this?

 I'm guessing your pip or py environment paths are wrong. Or you are actually missing the bitcoin.rpc module from bitcoinlib try this.
 
Code:
pip install --upgrade bitcoin


an uninstall and reinstall of python-bitcoinlib worked. Thanks.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!