<p>I solved the problem by installing the <code>bitcoin</code> package again.</p>
<p>I was getting this error message with my script <a href="https://github.com/vstoykovbg/doubleslow" rel="nofollow noreferrer">doubleslow-base.py</a>:</p>
<pre><code>Traceback (most recent call last):
  File &quot;./doubleslow-base.py&quot;, line 136, in &lt;module&gt;
    print_the_secrets(digest256_final)
  File &quot;/home/valentin/python/doubleslow/doubleslow_module.py&quot;, line 301, in print_the_secrets
    my_256_bit_secret_WIF = bitcoin.encode_privkey(my_256_bit_secret_HEX,&quot;wif_compressed&quot;)
AttributeError: module 'bitcoin' has no attribute 'encode_privkey'
</code></pre>
<p>After installing it again it works:</p>
<pre><code>$ pip3 install --user bitcoin
Collecting bitcoin
Installing collected packages: bitcoin
Successfully installed bitcoin-1.1.42
</code></pre>
<p>Just in case I installed again all requirements:</p>
<pre><code>$ pip3 install -r requirements.txt
</code></pre>
<p>I am not sure why this solved the problem. The script was working last year (when it was released) on the same system.</p>
<p>I was able to run your script with python2 and python3 by installing <code>bitcoin</code> with <code>pip install --user bitcoin</code> (to work with python2) and by defining <code>i</code>:</p>
<pre><code>#!/usr/bin/python3

import random
import bitcoin
import requests

i=0

while True:
    i += 1
    liste_ilk_basamak = (&quot;89ABCDEF&quot;)
    liste_son15_basamak = (&quot;0123456789ABCDEF&quot;)
    private_key_random = &quot;&quot;.join(random.choice(liste_ilk_basamak) for _ in range(1)) + &quot;&quot;.join(
        random.choice(liste_son15_basamak) for _ in range(15))
    decoded_private_key = bitcoin.decode_privkey(private_key_random, 'hex')
    valid_private_key = 0 &lt; decoded_private_key &lt; bitcoin.N
    wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif')
    public_key = bitcoin.fast_multiply(bitcoin.G, decoded_private_key)
    adress = bitcoin.pubkey_to_address(public_key)

    print(adress)
</code></pre>
<p>The module <code>requests</code> is not being used, so the line <code>import requests</code> can be removed. I added a <code>print</code> to see what happens and to confirm that the script is actually working.</p>
<p>First, I got this error:</p>
<pre><code>$ python3 ./test0.py 
Traceback (most recent call last):
  File &quot;./test0.py&quot;, line 9, in &lt;module&gt;
    i += 1
NameError: name 'i' is not defined
</code></pre>
<p>Obviously, you forgot to define <code>i</code>.</p>

CC BY-SA 4.0
2021-11-23T06:38:46.043Z
2021-11-23T06:52:56.483Z
2021-11-23T06:52:56.483Z
Valentin Stoykov
<p>For this error:</p>
<pre><code>AttributeError: module 'bitcoin' has no attribute 'privtoaddr'
</code></pre>
<p>I uninstalled bitcoin lib:</p>
<pre><code>pip3 uninstall bitcoin
</code></pre>
<p>Then removed folder:</p>
<pre><code>/home/user/.local/lib/python3.11/site-packages/bitcoin
</code></pre>
<p>with all files. Install the lib once again:</p>
<pre><code>pip3 install bitcoin
</code></pre>
<p>Then the problem vanished.</p>

CC BY-SA 4.0
2023-11-04T19:46:23.213Z
2023-11-04T19:46:23.213Z
pbies
<p>Hello everyone when I start this code on PYCHARM looks like no
problem. But when I turned this code from python to exe and exe
don't start and it says: import  error: cannot import name
&quot;privtopub&quot; from 'bitcoin'</p>
<pre><code>import random

import requests

import base58

import hashlib

from bitcoin import privtopub, pubtoaddr

# Some code here

while True:

    i += 1

    first_character = &quot;89ABCDEF&quot;
    first_character.encode('utf-8')
    last_15_character = &quot;0123456789ABCDEF&quot;
    last_15_character.encode('utf-8')
    privateKeyByte = (&quot;000000000000000000000000000000000000000000000000&quot; + &quot;&quot;.join(
        random.choice(first_character) for _ in range(1)) + &quot;&quot;.join(
        random.choice(last_15_character) for _ in range(15))).encode('utf-8')
    privateKey = str(privateKeyByte)[2:-1]
    extendedPrivateKey = (&quot;80&quot; + privateKey)
    extendedPrivateKeyByte = bytearray.fromhex(extendedPrivateKey)
    firstHash = hashlib.sha256()
    firstHash.update(extendedPrivateKeyByte)
    secondHash = hashlib.sha256()
    firstHashFromHex = bytearray.fromhex(firstHash.hexdigest())
    secondHash.update(firstHashFromHex)
    checksum = secondHash.hexdigest()[0:8]
    finalPrivateKey = extendedPrivateKey + checksum
    encodedKey = bytes.fromhex(finalPrivateKey)
    wifKey = base58.b58encode(encodedKey)
    wifKeyString = str(wifKey)[2:-1]
    publicKey = privtopub(privateKey)
    btcAddress = pubtoaddr(publicKey)
</code></pre>
<ul>
<li><p>Last 2 code i mean
<code>publicKey = privtopub(privateKey)</code> and <code>btcAddress = pubtoaddr(publicKey) cannot import from bitcoin</code> ? why ?</p>
</li>
<li><p>Also i had another problem but i solved it turned my privateKeyByte
to byte add this code &quot; .encode('utf-8').&quot; end of the privateKeyByte</p>
</li>
<li><p>When i delete this all code and to get to public adress with
this way :</p>
</li>
</ul>
<pre><code>    import random
    import bitcoin
    import requests
    
    while True:
        i += 1
        liste_ilk_basamak = (&quot;89ABCDEF&quot;)
        liste_son15_basamak = (&quot;0123456789ABCDEF&quot;)
        private_key_random = &quot;&quot;.join(random.choice(liste_ilk_basamak) for _ in range(1)) + &quot;&quot;.join(
            random.choice(liste_son15_basamak) for _ in range(15))
        decoded_private_key = bitcoin.decode_privkey(private_key_random, 'hex')
        valid_private_key = 0 &lt; decoded_private_key &lt; bitcoin.N
        wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif')
        public_key = bitcoin.fast_multiply(bitcoin.G, decoded_private_key)
        adress = bitcoin.pubkey_to_address(public_key)
</code></pre>
<ul>
<li>İ Have This Error : AttributeError: Module &quot;bitcoin&quot; has no attribute 'decode_privkey'</li>
</ul>
<p>I am trying to solve this problem 4 days and I am going to be crazy lost my mind :S help me pls :)</p>

CC BY-SA 4.0
2021-05-15T23:14:14.320Z
I think a link to the library that you are using can be helpful, not all the programmers here use python and know all the details about the library. A link can be helpful.
vincenzopalazzo
CC BY-SA 4.0
2021-11-22T04:59:06.840Z
I have the same problem with the missing attribute 'decode_privkey'. This attribute existed, but now it's not existing. Strange. This happened with my script "doubleslow": https://github.com/vstoykovbg/doubleslow . Maybe "bitcoin" was installed with "pip3 install --user bitcoin".
Valentin Stoykov
CC BY-SA 4.0
2021-11-22T05:02:42.187Z
`$ python3 -m pip show bitcoin
Name: bitcoin
Version: 1.1.42
Summary: Python Bitcoin Tools
Home-page: http://github.com/vbuterin/pybitcointools
Author: Vitalik Buterin
Author-email: vbuterin@gmail.com
License: UNKNOWN
Location: /home/valentin/.local/lib/python3.6/site-packages
Requires: `
Valentin Stoykov
CC BY-SA 4.0
2021-11-22T05:06:29.403Z
Hmm... Strange. The package bitcoin 1.1.42 was released on Jan 11, 2016. Therefore, it was not changed. The attribute 'decode_privkey' should not be missing.
Valentin Stoykov
CC BY-SA 4.0
2021-11-22T05:07:25.060Z
You can try to run your script with this distro: https://github.com/vstoykovbg/cryptopup/

With this distro my script was working without the error about the missing attribute 'decode_privkey'.

Now I am with the latest Ubuntu 18 and this error occurs.
Valentin Stoykov
<bitcoin-core><private-key><hash><python><recover-private-key>
Python Code - import error : cannot import name "privtopub" from 'bitcoin'
Umut Beyaztaş
# Python Code - import error : cannot import name "privtopub" from 'bitcoin'

(Asked by: username_0 on 2021-05-14)

Hello everyone when I start this code on PYCHARM looks like no
problem. But when I turned this code from python to exe and exe
don't start and it says: import error: cannot import name
"privtopub" from 'bitcoin'

```
import random

import requests

import base58

import hashlib

from bitcoin import privtopub, pubtoaddr

# Some code here

while True:

    i += 1

    first_character = "89ABCDEF"
    first_character.encode('utf-8')
    last_15_character = "0123456789ABCDEF"
    last_15_character.encode('utf-8')
    privateKeyByte = ("000000000000000000000000000000000000000000000000" + "".join(
        random.choice(first_character) for _ in range(1)) + "".join(
        random.choice(last_15_character) for _ in range(15))).encode('utf-8')
    privateKey = str(privateKeyByte)[2:-1]
    extendedPrivateKey = ("80" + privateKey)
    extendedPrivateKeyByte = bytearray.fromhex(extendedPrivateKey)
    firstHash = hashlib.sha256()
    firstHash.update(extendedPrivateKeyByte)
    secondHash = hashlib.sha256()
    firstHashFromHex = bytearray.fromhex(firstHash.hexdigest())
    secondHash.update(firstHashFromHex)
    checksum = secondHash.hexdigest()[0:8]
    finalPrivateKey = extendedPrivateKey + checksum
    encodedKey = bytes.fromhex(finalPrivateKey)
    wifKey = base58.b58encode(encodedKey)
    wifKeyString = str(wifKey)[2:-1]
    publicKey = privtopub(privateKey)
    btcAddress = pubtoaddr(publicKey)

```

* Last 2 code i mean
  `publicKey = privtopub(privateKey)` and `btcAddress = pubtoaddr(publicKey) cannot import from bitcoin` ? why ?
* Also i had another problem but i solved it turned my privateKeyByte
  to byte add this code " .encode('utf-8')." end of the privateKeyByte
* When i delete this all code and to get to public adress with
  this way :

```
    import random
    import bitcoin
    import requests
    
    while True:
        i += 1
        liste_ilk_basamak = ("89ABCDEF")
        liste_son15_basamak = ("0123456789ABCDEF")
        private_key_random = "".join(random.choice(liste_ilk_basamak) for _ in range(1)) + "".join(
            random.choice(liste_son15_basamak) for _ in range(15))
        decoded_private_key = bitcoin.decode_privkey(private_key_random, 'hex')
        valid_private_key = 0 < decoded_private_key < bitcoin.N
        wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif')
        public_key = bitcoin.fast_multiply(bitcoin.G, decoded_private_key)
        adress = bitcoin.pubkey_to_address(public_key)

```

* İ Have This Error : AttributeError: Module "bitcoin" has no attribute 'decode\_privkey'

I am trying to solve this problem 4 days and I am going to be crazy lost my mind :S help me pls :)

**Tags:** bitcoin-core, private-key, hash, python, recover-private-key

> **Comments:**
> **username_1**: I think a link to the library that you are using can be helpful, not all the programmers here use python and know all the details about the library. A link can be helpful.  
> **username_2**: I have the same problem with the missing attribute 'decode_privkey'. This attribute existed, but now it's not existing. Strange. This happened with my script "doubleslow": https://github.com/vstoykovbg/doubleslow . Maybe "bitcoin" was installed with "pip3 install --user bitcoin".  
> **username_2**: `$ python3 -m pip show bitcoin Name: bitcoin Version: 1.1.42 Summary: Python Bitcoin Tools Home-page: http://github.com/vbuterin/pybitcointools Author: Vitalik Buterin Author-email: vbuterin@gmail.com License: UNKNOWN Location: /home/username_2/.local/lib/python3.6/site-packages Requires: `  
> **username_2**: Hmm... Strange. The package bitcoin 1.1.42 was released on Jan 11, 2016. Therefore, it was not changed. The attribute 'decode_privkey' should not be missing.  
> **username_2**: You can try to run your script with this distro: https://github.com/vstoykovbg/cryptopup/  With this distro my script was working without the error about the missing attribute 'decode_privkey'.  Now I am with the latest Ubuntu 18 and this error occurs.  

---

## 2 answers

### Answer by username_2 on 2021-11-23. Score: 1

I solved the problem by installing the `bitcoin` package again.

I was getting this error message with my script [doubleslow-base.py](https://github.com/vstoykovbg/doubleslow):

```
Traceback (most recent call last):
  File "./doubleslow-base.py", line 136, in <module>
    print_the_secrets(digest256_final)
  File "/home/username_2/python/doubleslow/doubleslow_module.py", line 301, in print_the_secrets
    my_256_bit_secret_WIF = bitcoin.encode_privkey(my_256_bit_secret_HEX,"wif_compressed")
AttributeError: module 'bitcoin' has no attribute 'encode_privkey'

```

After installing it again it works:

```
$ pip3 install --user bitcoin
Collecting bitcoin
Installing collected packages: bitcoin
Successfully installed bitcoin-1.1.42

```

Just in case I installed again all requirements:

```
$ pip3 install -r requirements.txt

```

I am not sure why this solved the problem. The script was working last year (when it was released) on the same system.

I was able to run your script with python2 and python3 by installing `bitcoin` with `pip install --user bitcoin` (to work with python2) and by defining `i`:

```
#!/usr/bin/python3

import random
import bitcoin
import requests

i=0

while True:
    i += 1
    liste_ilk_basamak = ("89ABCDEF")
    liste_son15_basamak = ("0123456789ABCDEF")
    private_key_random = "".join(random.choice(liste_ilk_basamak) for _ in range(1)) + "".join(
        random.choice(liste_son15_basamak) for _ in range(15))
    decoded_private_key = bitcoin.decode_privkey(private_key_random, 'hex')
    valid_private_key = 0 < decoded_private_key < bitcoin.N
    wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif')
    public_key = bitcoin.fast_multiply(bitcoin.G, decoded_private_key)
    adress = bitcoin.pubkey_to_address(public_key)

    print(adress)

```

The module `requests` is not being used, so the line `import requests` can be removed. I added a `print` to see what happens and to confirm that the script is actually working.

First, I got this error:

```
$ python3 ./test0.py 
Traceback (most recent call last):
  File "./test0.py", line 9, in <module>
    i += 1
NameError: name 'i' is not defined

```

Obviously, you forgot to define `i`.


---

### Answer by username_3 on 2023-11-04. Score: 0

For this error:

```
AttributeError: module 'bitcoin' has no attribute 'privtoaddr'

```

I uninstalled bitcoin lib:

```
pip3 uninstall bitcoin

```

Then removed folder:

```
/home/user/.local/lib/python3.11/site-packages/bitcoin

```

with all files. Install the lib once again:

```
pip3 install bitcoin

```

Then the problem vanished.


---

