<p>There is <a href="https://github.com/vbuterin/pybitcointools" rel="nofollow">pybitcointools</a>, which allows you to do just that using the sign(tx, index, priv) function. 
There are others but I at least found this one the easiest to work with.</p>

<p>EDIT: You have many inputs I can see..the following could help</p>

<pre><code>def signAllIns(aTx, priv, nbIns=None):
    signedTx = aTx
    utxo = nbIns

    if utxo == None:
        utxo = len(unspent(privtoaddr(priv)))

    for i in range(utxo):
        signedTx = sign(signedTx, i, priv)
    return signedTx
</code></pre>

<p>It loops over all the inputs of the transaction to sign them all. </p>

CC BY-SA 3.0
2016-07-26T16:07:28.003Z
Thank. I will try it and let you know the result later. At first, I'll find how to convert my "tx hex" to "aTx" object.
zono
CC BY-SA 3.0
2016-07-26T16:13:19.130Z
sure, let me know. The aTx parameter is a tx hex. No need to convert x)
hartmut
CC BY-SA 3.0
2016-07-27T03:29:01.283Z
It works when I set nbIns=None. However I got 'IndexError: list index out of range' when I use another hex. I guess it does not match utxos between the hex and pybitcointool generated one. Do you know how to convert from my hex to utxo? I want to set it as 'nbIns' parameter.
zono
CC BY-SA 3.0
2016-07-27T03:58:27.653Z
I'm sorry to bother you. I think I made it. I will post the result after my transaction is confirmed.
zono
CC BY-SA 3.0
2016-07-27T08:20:58.307Z
Cool. The `nbIns` parameter is an int.
hartmut
CC BY-SA 3.0
2016-07-26T16:02:14.340Z
2016-07-26T16:02:14.340Z
hartmut
<p>I have a unsigned transaction hex. Now I want to sign this transaction input with my private key in Python. Are there any Python libraries?</p>

<p>Transaction Hex</p>

<pre><code>010000000414ae2264cdbe754b9ae4be18d84bfeb4f578af553b9b9e4f9cb2303d04ee9e45000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffffec15d27b74516fefd921cecbe043ea63124d28a3903aef8fb1682ccc926b1c62000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffff9878f76e1424c1d1eeb6b15d06902dd8f0c78f9bdb61263e4ca3ae68c571a292000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acfffffffffdac7f1c83b01a8924a8965d356b804c2608bc261fc18041116ddb4a143bc499000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffff0336150000000000001976a9141485d9d03b41aaa9dca7d70d7f63ff4a0826100e88ac00000000000000001e6a1c246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e92180000000000001976a91458b6e991b45487df810f4d96d5315da739637f1788ac00000000
</code></pre>

<p>Decoded Transaction</p>

<pre><code>{
   "txid":"6c521937d8fa889d71609ebed2e23b694cbca89778c8b8d967167e5a4c9d5b78",
   "size":381,
   "version":1,
   "locktime":0,
   "vin":[
      {
         "txid":"459eee043d30b29c4f9e9b3b55af78f5b4fe4bd818bee49a4b75becd6422ae14",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      },
      {
         "txid":"621c6b92cc2c68b18fef3a90a3284d1263ea43e0cbce21d9ef6f51747bd215ec",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      },
      {
         "txid":"92a271c568aea34c3e2661db9b8fc7f0d82d90065db1b6eed1c124146ef77898",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      },
      {
         "txid":"99c43b144adb6d114180c11f26bc08264c806b355d96a824891ab0831c7facfd",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      }
   ],
   "vout":[
      {
         "value":5.43e-5,
         "n":0,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 1485d9d03b41aaa9dca7d70d7f63ff4a0826100e OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a9141485d9d03b41aaa9dca7d70d7f63ff4a0826100e88ac",
            "reqSigs":1,
            "type":"pubkeyhash",
            "addresses":[
               "12sWrxRY7E7Nhmuyjbz4TtGE9jRewGqEZD"
            ]
         }
      },
      {
         "value":0,
         "n":1,
         "scriptPubKey":{
            "asm":"OP_RETURN 246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e",
            "hex":"6a1c246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e",
            "type":"nulldata"
         }
      },
      {
         "value":6.29e-5,
         "n":2,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac",
            "reqSigs":1,
            "type":"pubkeyhash",
            "addresses":[
               "1965areciqapsuL2hsia2yKkRLfAsH1smG"
            ]
         }
      }
   ]
}
</code></pre>

<hr>

<p>Update 1</p>

<p>Solved thanks to hartmut.</p>

<p>1 Install pybitcointools</p>

<pre><code>git clone https://github.com/vbuterin/pybitcointools.git
cd pybitcointools
sudo python setup.py # I got an error in Python3. So I used Python2.
</code></pre>

<p>2 Sign by using pybitcointools</p>

<pre><code>import bitcoin

def signAllIns(aTx, priv, nbIns=None):
    signedTx = aTx
    utxo = nbIns
    if utxo == None:
        utxo = len(bitcoin.unspent(bitcoin.privtoaddr(priv)))
    for i in range(utxo):
        signedTx = bitcoin.sign(signedTx, i, priv)
    return signedTx

deserializeHex = bitcoin.deserialize(HEX)
ins = deserializeHex['ins']
signedTx = signAllIns(HEX, PRIVATE_KEY, len(ins))

# Broadcast signedTx here
</code></pre>

<signature><python><python-bitcoinlib>
How can I sign transaction hex with Python library
zono
# How can I sign transaction hex with Python library

(Asked by: username_0 on 2016-07-26)

I have a unsigned transaction hex. Now I want to sign this transaction input with my private key in Python. Are there any Python libraries?

Transaction Hex

```
010000000414ae2264cdbe754b9ae4be18d84bfeb4f578af553b9b9e4f9cb2303d04ee9e45000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffffec15d27b74516fefd921cecbe043ea63124d28a3903aef8fb1682ccc926b1c62000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffff9878f76e1424c1d1eeb6b15d06902dd8f0c78f9bdb61263e4ca3ae68c571a292000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acfffffffffdac7f1c83b01a8924a8965d356b804c2608bc261fc18041116ddb4a143bc499000000001976a91458b6e991b45487df810f4d96d5315da739637f1788acffffffff0336150000000000001976a9141485d9d03b41aaa9dca7d70d7f63ff4a0826100e88ac00000000000000001e6a1c246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e92180000000000001976a91458b6e991b45487df810f4d96d5315da739637f1788ac00000000

```

Decoded Transaction

```
{
   "txid":"6c521937d8fa889d71609ebed2e23b694cbca89778c8b8d967167e5a4c9d5b78",
   "size":381,
   "version":1,
   "locktime":0,
   "vin":[
      {
         "txid":"459eee043d30b29c4f9e9b3b55af78f5b4fe4bd818bee49a4b75becd6422ae14",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      },
      {
         "txid":"621c6b92cc2c68b18fef3a90a3284d1263ea43e0cbce21d9ef6f51747bd215ec",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      },
      {
         "txid":"92a271c568aea34c3e2661db9b8fc7f0d82d90065db1b6eed1c124146ef77898",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      },
      {
         "txid":"99c43b144adb6d114180c11f26bc08264c806b355d96a824891ab0831c7facfd",
         "vout":0,
         "scriptSig":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac"
         },
         "sequence":4294967295
      }
   ],
   "vout":[
      {
         "value":5.43e-5,
         "n":0,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 1485d9d03b41aaa9dca7d70d7f63ff4a0826100e OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a9141485d9d03b41aaa9dca7d70d7f63ff4a0826100e88ac",
            "reqSigs":1,
            "type":"pubkeyhash",
            "addresses":[
               "12sWrxRY7E7Nhmuyjbz4TtGE9jRewGqEZD"
            ]
         }
      },
      {
         "value":0,
         "n":1,
         "scriptPubKey":{
            "asm":"OP_RETURN 246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e",
            "hex":"6a1c246698efc5d81b78ceadf3179316b5eb6cc5c2c347c0b7b42121a94e",
            "type":"nulldata"
         }
      },
      {
         "value":6.29e-5,
         "n":2,
         "scriptPubKey":{
            "asm":"OP_DUP OP_HASH160 58b6e991b45487df810f4d96d5315da739637f17 OP_EQUALVERIFY OP_CHECKSIG",
            "hex":"76a91458b6e991b45487df810f4d96d5315da739637f1788ac",
            "reqSigs":1,
            "type":"pubkeyhash",
            "addresses":[
               "1965areciqapsuL2hsia2yKkRLfAsH1smG"
            ]
         }
      }
   ]
}

```

---

Update 1

Solved thanks to username_1.

1 Install pybitcointools

```
git clone https://github.com/vbuterin/pybitcointools.git
cd pybitcointools
sudo python setup.py # I got an error in Python3. So I used Python2.

```

2 Sign by using pybitcointools

```
import bitcoin

def signAllIns(aTx, priv, nbIns=None):
    signedTx = aTx
    utxo = nbIns
    if utxo == None:
        utxo = len(bitcoin.unspent(bitcoin.privtoaddr(priv)))
    for i in range(utxo):
        signedTx = bitcoin.sign(signedTx, i, priv)
    return signedTx

deserializeHex = bitcoin.deserialize(HEX)
ins = deserializeHex['ins']
signedTx = signAllIns(HEX, PRIVATE_KEY, len(ins))

# Broadcast signedTx here

```

**Tags:** signature, python, python-bitcoinlib


---

## 1 answers

### Answer by username_1 on 2016-07-26. Score: 4 | Accepted answer

There is [pybitcointools](https://github.com/vbuterin/pybitcointools), which allows you to do just that using the sign(tx, index, priv) function.
There are others but I at least found this one the easiest to work with.

EDIT: You have many inputs I can see..the following could help

```
def signAllIns(aTx, priv, nbIns=None):
    signedTx = aTx
    utxo = nbIns

    if utxo == None:
        utxo = len(unspent(privtoaddr(priv)))

    for i in range(utxo):
        signedTx = sign(signedTx, i, priv)
    return signedTx

```

It loops over all the inputs of the transaction to sign them all.

> **Comments:**
> **username_0**: Thank. I will try it and let you know the result later. At first, I'll find how to convert my "tx hex" to "aTx" object.  
> **username_1**: sure, let me know. The aTx parameter is a tx hex. No need to convert x)  
> **username_0**: It works when I set nbIns=None. However I got 'IndexError: list index out of range' when I use another hex. I guess it does not match utxos between the hex and pybitcointool generated one. Do you know how to convert from my hex to utxo? I want to set it as 'nbIns' parameter.  
> **username_0**: I'm sorry to bother you. I think I made it. I will post the result after my transaction is confirmed.  
> **username_1**: Cool. The `nbIns` parameter is an int.  

---

