# PyCall Workaround for Automatic Conversion, Looking for Alternative

**URL:** https://discourse.julialang.org/t/pycall-workaround-for-automatic-conversion-looking-for-alternative/96855
**Category:** General Usage
**Tags:** question, pycall, syntax
**Created:** [March 30, 2023, 6:34pm UTC](https://discourse.julialang.org/t/pycall-workaround-for-automatic-conversion-looking-for-alternative/96855 "2023-03-30T18:34:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![isaac.rubberducky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaac.rubberducky/32/36791_2.png) [@isaac.rubberducky](https://discourse.julialang.org/u/isaac.rubberducky)
#### Post date: [March 30, 2023, 6:34pm UTC](https://discourse.julialang.org/t/pycall-workaround-for-automatic-conversion-looking-for-alternative/96855/1 "2023-03-30T18:34:16Z")

</div>

So here is a solved problem, but it feels like I have solved it incorrectly, and I would love to know if there is a better way.

I want to create a PyObject using the Body constructor from this library. Here is a basic MWE that works.

```julia
usingPyCall
const astropy_units = pyimport("astropy.units") 
const poliastro_body = pyimport("poliastro.bodies").Body

const AU = 1.49597870691e8 # km
const MU = 1.32712440018e11 # km^3/s^2

Orbit_Builder_Sun = py"[exec('import poliastro.bodies as pbd'), pbd.Body(parent=None,k = $MU * ($astropy_units.km **3 / $astropy_units.s** 2), name='Sun')][-1]"o

println(Orbit_Builder_Sun) # prints -> PyObject Sun (None)

```

The above works because if you run something as a single line of Python, `py"..."o` returns the results as an object. The structure of that line is the way it is to be able to import and run Body on the same line.

So that’s really un-intuitive. Here is what I thought would work but does not:

```julia
Orbit_Builder_Sun = poliastro_body(
    parent=nothing,
     k = MU * (astropy_units.km ^ 3 / astropy_units.s ^ 2),
     name="Sun"
)
println(Orbit_Builder_Sun) # prints -> (nothing, fill(1.32712440018e11), "Sun", nothing, fill(0.0), fill(0.0), fill(0.0), fill(0.0), fill(0.0), fill(0.0), fill(1.9884098709677421e21))

```

The version of the code that I thought would work does not return a PyObject, and so I can no longer pass it to functions that need to use it.

This can’t be the intended workflow, could someone provide some insight into how I could do this in a “better” way

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [March 30, 2023, 7:28pm UTC](https://discourse.julialang.org/t/pycall-workaround-for-automatic-conversion-looking-for-alternative/96855/2 "2023-03-30T19:28:31Z")

</div>

I believe you can replace `poliastro_body(...)` with `pycall(poliastro_body, PyObject, ...)` to force it to return a `PyObject`.

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [March 30, 2023, 7:29pm UTC](https://discourse.julialang.org/t/pycall-workaround-for-automatic-conversion-looking-for-alternative/96855/3 "2023-03-30T19:29:21Z")

</div>

(Or use PythonCall which does no such automatic conversion.)

---

<div class="post-metadata">

### Author: ![isaac.rubberducky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaac.rubberducky/32/36791_2.png) [@isaac.rubberducky](https://discourse.julialang.org/u/isaac.rubberducky)
#### Post date: [March 30, 2023, 7:41pm UTC](https://discourse.julialang.org/t/pycall-workaround-for-automatic-conversion-looking-for-alternative/96855/4 "2023-03-30T19:41:55Z")

</div>

Thanks!
