# Integration of Python with Julia

**URL:** https://discourse.julialang.org/t/integration-of-python-with-julia/6304
**Category:** General Usage
**Tags:** question, pycall
**Created:** [October 7, 2017, 8:53am UTC](https://discourse.julialang.org/t/integration-of-python-with-julia/6304 "2017-10-07T08:53:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gopal2017](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gopal2017/32/31747_2.png) [@gopal2017](https://discourse.julialang.org/u/gopal2017)
#### Post date: [October 7, 2017, 8:53am UTC](https://discourse.julialang.org/t/integration-of-python-with-julia/6304/1 "2017-10-07T08:53:54Z")

</div>

Hi, I want to access data base with Python and want to transfer value retrieved by Python to Julia. Code is very common and available on internet. Pls check it is mentioned below:

```julia-auto
import pyodbc
import DBConfig
import numpy as np
#string connection ="Data Source=KRISHNA\\SQLEXPRESS;Initial Catalog=msdb;Integrated Security=True"
def dbconn():
    cnxn = pyodbc.connect(DBConfig.DBConnectString)
    cursor = cnxn.cursor()

    cursor.execute("SELECT * FROM BOM")
    rows = cursor.fetchall()

    bomid=np.empty(len(rows),dtype=object)
    i=0
    for row in rows:
      
       bomid[i]=row.BOMID
       i+=1
    return bomid

```

* * *

Calling Python from Julia

```julia-auto
module Test
export TestDB
BOMID=Dict()
julia_script_path="............/Julia/"
ENV["PYTHONPATH"]="............../Python/"
using PyCall
@pyimport pyodbc
@pyimport DBConnect
function TestDB()

BOMID=DBConnect.dbconn()
#for i=1:length(BOMID)
# println(string("BOM ID:",BOMID[i]))
#end
return BOMID
end
end

```

* * *

Code is working. But only problem is BOMID is taking the value

```julia-auto
PyCall.PyObject[PyObject Decimal('1234'),PyObject Decimal('1235')]

```

## How to take only “1234” or :“1235”??

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 7, 2017, 9:05pm UTC](https://discourse.julialang.org/t/integration-of-python-with-julia/6304/2 "2017-10-07T21:05:21Z")

</div>

> [@gopal2017](#):
>
> Code is working. But only problem is BOMID is taking the value `PyCall.PyObject[PyObject Decimal('1234'),PyObject Decimal('1235')]` How to take only “1234” or :“1235”?

Good question. In Python, you would call `str` on a `Decimal` object to get it in string form. I just noticed that PyCall wasn’t exporting any equivalent of this, so I added a `pystr` function.

If you checkout the latest PyCall master (`Pkg.checkout("PyCall")`), you can do `pystr.(BOMID)` to convert each object to a string.

---

<div class="post-metadata">

### Author: ![gopal2017](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gopal2017/32/31747_2.png) [@gopal2017](https://discourse.julialang.org/u/gopal2017)
#### Post date: [November 25, 2017, 8:12am UTC](https://discourse.julialang.org/t/integration-of-python-with-julia/6304/3 "2017-11-25T08:12:33Z")

</div>

Please show me the code of pystr function

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [November 25, 2017, 2:07pm UTC](https://discourse.julialang.org/t/integration-of-python-with-julia/6304/4 "2017-11-25T14:07:27Z")

</div>

> [@gopal2017](#):
>
> Please show me the code of pystr function

If you are running Julia with PyCall, you can do `@which pystr(PyObject(1))` to see where the code is, or use `@edit` to open it in an editor. You can also just search for “pystr” in the PyCall github repository. In either case, you’ll quickly see that it is just a call to `PyObject_Str` in the C API, which is equivalent to `str` in Python.
