# Passing bytes instead strings in PyCall.jl

**URL:** <https://discourse.julialang.org/t/passing-bytes-instead-strings-in-pycall-jl/3880>\
**Category:** General Usage\
**Created:** [May 23, 2017, 2:22pm UTC](https://discourse.julialang.org/t/passing-bytes-instead-strings-in-pycall-jl/3880 "2017-05-23T14:22:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)\
**Post date:** [May 23, 2017, 2:22pm UTC](https://discourse.julialang.org/t/passing-bytes-instead-strings-in-pycall-jl/3880/1 "2017-05-23T14:22:06Z")

</div>

With Python 3 as a backend, what’s the solution to this error?

```julia
julia> writer("a")
ERROR: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) <class 'TypeError'>
TypeError('expected bytes, str found',)

```

I also tried

```julia
julia> writer(Vector{UInt8}("a"))
ERROR: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) <class 'TypeError'>
TypeError('expected bytes, bytearray found',)

```

---

<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:** [May 24, 2017, 1:08am UTC](https://discourse.julialang.org/t/passing-bytes-instead-strings-in-pycall-jl/3880/2 "2017-05-24T01:08:14Z")

</div>

Try defining:

```julia
pybytes(b) = PyObject(PyCall.@pycheckn 
    ccall(PyCall.@pysym(PyCall.PyString_FromStringAndSize),
                                             PyPtr, (Ptr{UInt8}, Int),
                                             b, sizeof(b)))

```

and then calling `writer(pybytes("a"))` to force it to create a `bytes` object.

In PyCall master, this function is available as `PyCall.pybytes`

---

<div class="post-metadata">

**Author:** ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)\
**Post date:** [May 24, 2017, 4:49pm UTC](https://discourse.julialang.org/t/passing-bytes-instead-strings-in-pycall-jl/3880/3 "2017-05-24T16:49:37Z")

</div>

Thanks!
