<p>Try this:</p>
<pre class="lang-py prettyprint-override"><code>import bitcoin, base58, ecdsa, requests, smtplib, multiprocessing, blockchain
import gc
import sys
gc.collect()

while True:

    private_key = random_key()
    public_key = privtopub(private_key)
    address = pubtoaddr(public_key)

    with open(&quot;btc.txt&quot;, &quot;r&quot;) as m:
        add = m.read().split()
        for ad in add:
                continue
        if address in add:
            print(&quot;Found&quot;+ &quot; &quot; +address+ ' ' +private_key)
            data = open(&quot;data.txt&quot;,&quot;a&quot;)
            data.write(print(address+ ' ' +private_key+ ' ' +&quot;\n&quot;)) 
            data.close()

        else:
            print(address+ ' ' +private_key)
del (address)
del (private_key)
os.execv(sys.executable, ['python'] + [sys.argv[0]])
</code></pre>

CC BY-SA 4.0
2022-10-12T08:47:38.190Z
2022-10-17T19:00:20.623Z
2022-10-17T19:00:20.623Z
crypto saver
<p>I have done <code>pip install bitcoin</code> and then wrote the below code:</p>
<pre><code>from bitcoin import *
private_key = random_key()
print(private_key)
</code></pre>
<p>Now I am getting this error:</p>
<pre><code>Traceback (most recent call last):
  File &quot;/Users/praveen/Documents/code/sandbox/python/bitcoinkey.py&quot;, line 1, in &lt;module&gt;
    from bitcoinkey import *
  File &quot;/Users/praveen/Documents/code/sandbox/python/bitcoinkey.py&quot;, line 2, in &lt;module&gt;
    private_key = random_key()
NameError: name 'random_key' is not defined
</code></pre>
<p>Any idea why 'random_key' is not defined?</p>
<p>Thanks,
Just getting started with tinkering.</p>

CC BY-SA 4.0
2022-02-06T00:28:37.330Z
By pip installing bitcoin you are installing an unmaintained Bitcoin library https://github.com/vbuterin/pybitcointools. I'd recommend you use another. https://github.com/ofek/bit, https://github.com/petertodd/python-bitcoinlib, https://github.com/mcdallas/cryptotools etc
Michael Folkson
CC BY-SA 4.0
2022-02-06T07:27:26.283Z
Thank you ! I will test this further
spraveenitpro
<private-key><python>
Using the Bitcoin python library to generate Private Key [Noob question]
spraveenitpro
# Using the Bitcoin python library to generate Private Key [Noob question]

(Asked by: username_0 on 2022-02-05)

I have done pip install bitcoin and then wrote the below code:
from bitcoin import *
private_key = random_key()
print(private_key)

Now I am getting this error:
Traceback (most recent call last):
  File "/Users/praveen/Documents/code/sandbox/python/bitcoinkey.py", line 1, in 
    from bitcoinkey import *
  File "/Users/praveen/Documents/code/sandbox/python/bitcoinkey.py", line 2, in 
    private_key = random_key()
NameError: name 'random_key' is not defined

Any idea why 'random_key' is not defined?
Thanks,
Just getting started with tinkering.

**Tags:** private-key, python

> **Comments:**
> **username_1**: By pip installing bitcoin you are installing an unmaintained Bitcoin library https://github.com/vbuterin/pybitcointools. I'd recommend you use another. https://github.com/ofek/bit, https://github.com/petertodd/python-bitcoinlib, https://github.com/mcdallas/cryptotools etc  
> **username_0**: Thank you ! I will test this further  

---

## 1 answers

### Answer by username_2 on 2022-10-12. Score: 0

Try this:
import bitcoin, base58, ecdsa, requests, smtplib, multiprocessing, blockchain
import gc
import sys
gc.collect()

while True:

    private_key = random_key()
    public_key = privtopub(private_key)
    address = pubtoaddr(public_key)

    with open("btc.txt", "r") as m:
        add = m.read().split()
        for ad in add:
                continue
        if address in add:
            print("Found"+ " " +address+ ' ' +private_key)
            data = open("data.txt","a")
            data.write(print(address+ ' ' +private_key+ ' ' +"\n")) 
            data.close()

        else:
            print(address+ ' ' +private_key)
del (address)
del (private_key)
os.execv(sys.executable, ['python'] + [sys.argv[0]])


---

