# \[ANN\] PythonCall and JuliaCall

**URL:** <https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778>\
**Category:** Package Announcements\
**Tags:** interoperability, python, pythoncall\
**Created:** [February 19, 2022, 10:49pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778 "2022-02-19T22:49:15Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [March 4, 2022, 5:46pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/66 "2022-03-04T17:46:42Z")

</div>

Yea I think that’s basically it. I made a tiny package to do this and also to make IJulia figures work right: [https://github.com/marius311/PyPlotCall.jl](https://github.com/marius311/PyPlotCall.jl) I have no plans to develop this much further, but if someone wanted to claim ownership or build it out, please feel free to.

---

<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 4, 2022, 7:26pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/67 "2022-03-04T19:26:14Z")

</div>

PythonCall used to have a very similar integration with IJulia which got lost in a code reorg. Would be great to reintroduce it.

---

<div class="post-metadata">

**Author:** ![huang\_min](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huang_min/32/34337_2.png) [@huang\_min](https://discourse.julialang.org/u/huang_min)\
**Post date:** [March 5, 2022, 4:18am UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/68 "2022-03-05T04:18:47Z")

</div>

Setting environment variables in PyCall is just one-off. However, for `JULIA_PYTHONCALL_EXE` in PythonCall, I need to set it to @PyCall or the Python path each time when I open the REPL. Is it normal?

---

<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 5, 2022, 11:04am UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/69 "2022-03-05T11:04:17Z")

</div>

That’s right, PythonCall intentionally doesn’t persist these settings. You can always set that env var in your `.profile` or `startup.jl` or etc…

---

<div class="post-metadata">

**Author:** ![huang\_min](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huang_min/32/34337_2.png) [@huang\_min](https://discourse.julialang.org/u/huang_min)\
**Post date:** [March 5, 2022, 12:45pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/70 "2022-03-05T12:45:15Z")

</div>

Thanks for the reply. Though I would prefer to have an argument for us to determine whether it memorizes the last setting.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [March 17, 2022, 1:52pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/71 "2022-03-17T13:52:37Z")

</div>

Say I have a Julia function `Main.f` with two methods `f(v::Array{Float64})` and `f(v:Array{String})`. With `pyjulia` (the Python module `julia`) I can do these calls: `Main.f(['a', 'b'])` and `Main.f([1.0, 2.0])`. In this case the Python lists will be converted to `Array{String}` and `Array{Float64}` and the correct method will be called. That’s the end of the story, I don’t need to do anything more. (The disadvantage is that I do need to do something more if I want to suppress automatic conversion.)

How can achieve the same thing with `juliacall`? For example `Main.f([1.0, 2.0])` will convert the Python list to `PythonCall.PyList{PythonCall.Py}`, which is IIUC a wrapper around the Python object. And there is no matching method for `f`.

Because `PyList <: AbstractVector`, I could write a method `f(v::AbstractVector)`. But, I would still need to look at the elements to find what the eltypes are and then convert and call `f` again. What would that code look like ? Or another option, perhaps better, would be to convert the Python list `['x', 'y']` to a `Vector{String}` from the Python side. This would be wrapped in the Python type `juliacall.VectorValue`. If I then call `Main.f`, I see that the correct method, one of `f(v::Array{Float64})` or `f(v:Array{String})` is indeed called. How can I do this; i.e. convert a Python list to a `VectorValue` ? I see that there is a function `PythonCall.pytype`. I could perhaps write some functions using these tools to do the conversions. Are there any facilities like this already written ?

EDIT: There is a function `PythonCall.pyconvert(T, x)` that converts a (python) object `x` to an object of Julia type `T`. I’d like to write a function `pyconvert(x)` that converts `x` to the most natural type. For example `str` → `String`. What is the most efficient way to write such a function?

---

<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 17, 2022, 3:19pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/72 "2022-03-17T15:19:58Z")

</div>

The intended way to do this is

```python
from juliacall import Main as jl, As as jlas
jl.f(jlas(["foo", "bar"], jl.Vector))

```

The object `jlas(x, T)` is viewed on the Julia side as `pyconvert(T, x)`.

There was [some discussion here](https://github.com/cjdoris/PythonCall.jl/discussions/128) with @icweaver about creating a function `pyconvertnative(x)` to convert only to Julia “native” types.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [March 17, 2022, 4:07pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/73 "2022-03-17T16:07:54Z")

</div>

That doesn’t appear to work for me with juliacall 0.6.1  
I get the following:

```python
In [1]: from juliacall import Main as jl, As as jlas

In [2]: jl.seval('f(x) = nothing')
Out[2]: f (generic function with 1 method)

In [3]: jl.f(jlas(["foo", "bar"], jl.Vector))
---------------------------------------------------------------------------
JuliaError Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 jl.f(jlas(["foo", "bar"], jl.Vector))

File ~/.julia/packages/PythonCall/Z6DIG/src/jlwrap/any.jl:167:30, in __call__ (self, *args, **kwargs)
     28 module = Base
     29 elif mname == 'Core':
---> 30 module = Core
     31 elif mname == 'Main':
     32 module = Main

JuliaError: StackOverflowError:
Stacktrace:
     [1] pyconvert_rule_jlas(#unused#::Type{Vector}, x::PythonCall.Py)
       @ PythonCall ~/.julia/packages/PythonCall/Z6DIG/src/juliacall.jl:52
     [2] (::PythonCall.var"#52#53"{Vector, typeof(PythonCall.pyconvert_rule_jlas)})(x::PythonCall.Py)
       @ PythonCall ~/.julia/packages/PythonCall/Z6DIG/src/convert.jl:207
     [3] macro expansion
       @ ~/.julia/packages/PythonCall/Z6DIG/src/convert.jl:238 [inlined]
     [4] macro expansion
       @ ~/.julia/packages/PythonCall/Z6DIG/src/Py.jl:129 [inlined]
     [5] pytryconvert(#unused#::Type{Vector}, x::PythonCall.Py)
       @ PythonCall ~/.julia/packages/PythonCall/Z6DIG/src/convert.jl:221
--- the last 5 lines are repeated 11424 more times ---
 [57126] pyconvert_rule_jlas(#unused#::Type{Vector}, x::PythonCall.Py)
       @ PythonCall ~/.julia/packages/PythonCall/Z6DIG/src/juliacall.jl:52

```

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [March 17, 2022, 4:17pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/74 "2022-03-17T16:17:33Z")

</div>

The following seems to work, and doesn’t seem grossly inefficient. Does this look reasonable? Am I am reproducing something that already exists, or is there a more idiomatic solution? (perhaps `juliacall.As` is convenient and efficient, once I get it working.) This converts `list` to `Vector` assuming the `pyconvert` succeeds for every element.

```julia
function pyconvert_list(::Type{T}, list) where T
    vec = Vector{T}(undef, length(list))
    for i in eachindex(list)
        vec[i] = PythonCall.pyconvert(T, list[i])
    end
    return vec
end

```

---

<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 17, 2022, 4:55pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/75 "2022-03-17T16:55:31Z")

</div>

That’s a perfectly reasonable function, but I don’t think it’s any different from `pyconvert(Vector{T}, list)`?

Thanks for the bug report, there was a very silly mistake in the conversion code. Fixed on main. I think I’ll make a release today too, so try again in an hour.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [March 17, 2022, 5:11pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/76 "2022-03-17T17:11:33Z")

</div>

> `pyconvert(Vector{T}, list)`

I didn’t try that. I think this is exactly what I’m looking for. A minor problem is, from the python side, `jl.Vector` works, but `jl.Vector{String}` is not valid python syntax. I suppose you could do `jl.seval('Vector{String}')`.

---

<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 17, 2022, 5:16pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/77 "2022-03-17T17:16:14Z")

</div>

> [@jlapeyre](#):
>
> `jl.Vector{String}` is not valid python syntax

You can do `jl.Vector[jl.String]` instead. 🙂

I really should work on the JuliaCall docs, they aren’t great!

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [March 17, 2022, 5:25pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/78 "2022-03-17T17:25:18Z")

</div>

> [@cjdoris](#):
>
> I really should work on the JuliaCall docs, they aren’t great!

I’m sure it will improve. On the upside, the code is pretty readable and discoverable. And getting it in the discourse thread is a start.

---

<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 17, 2022, 6:44pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/79 "2022-03-17T18:44:00Z")

</div>

OK I have just released PythonCall/JuliaCall 0.8.0. To reduce complexity I got rid of `juliacall.As` and replaced it with `juliacall.convert` which you can use like so:

```python
>>> from juliacall import Main as jl, convert as jlconvert
>>> jlconvert(jl.Vector[jl.String], ["foo", "bar"])
<jl ["foo", "bar"]>

```

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [March 18, 2022, 2:49pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/80 "2022-03-18T14:49:15Z")

</div>

This looks like an improvement.

However, compared to v0.6.1, it introduced some problems.

First if I do `juliacall.Main.bogussymbol` from the `ipython` cli, I get a segfault (Because `bogussymbol` is not defined). This doesn’t appear to be a bug in PythonCall per se, because the segfault doesn’t occur if I just use the crude `python` cli. I haven’t yet tried to track down the problem.

Second, I was able to compile a system image of a project and modify the initialization of `juliacall` to load the system image. After updating `juliacall`, I can still do this, but am getting frequent segfaults when I try to use it. I haven’t yet located the problem(s).

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [June 15, 2022, 9:15am UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/81 "2022-06-15T09:15:22Z")

</div>

```julia
from juliacall import Main as jl
import numpy as np
jl.seval("using Plots")
jl.plot(np.random.randn(100))

```

How to make the graph show?

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [June 15, 2022, 11:34am UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/82 "2022-06-15T11:34:41Z")

</div>

somewhat refactored, but this works:

```julia
using PythonCall
plt = pyimport("matplotlib.pyplot")
plt.plot(randn(100))
plt.show()

```

You may also want to consider [PythonPlot.jl](https://github.com/stevengj/PythonPlot.jl)

Edit: this is not an answer to the question (which I misunderstood)

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [June 15, 2022, 11:53am UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/83 "2022-06-15T11:53:23Z")

</div>

I take this example from [JuliaCall is a corresponding Python package to interoperate with Julia, so for example](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778)  
the code runs without errors within python only that it does not show the graph  
But I can’t get the graph to show up!!!

---

<div class="post-metadata">

**Author:** ![rejuvyesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rejuvyesh/32/91_2.png) [@rejuvyesh](https://discourse.julialang.org/u/rejuvyesh)\
**Post date:** [June 15, 2022, 4:14pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/84 "2022-06-15T16:14:51Z")

</div>

You probably want `jl.display()` at the end?

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [June 15, 2022, 4:24pm UTC](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778/85 "2022-06-15T16:24:59Z")

</div>

> [@rejuvyesh](#):
>
> `jl.display()`

```julia
Traceback (most recent call last):
  File "F:/Julia software/Julia Scripts/Julia Flux_getting_started/Example_2a_Calling_Julia_from_Python list type.py", line 21, in <module>
    jl.display()
  File "C:\Users\user\.julia\packages\PythonCall\4eoCM\src\jlwrap\any.jl", line 201, in __call__
    return self._jl_callmethod($(pyjl_methodnum(pyjlany_call)), args, kwargs)
TypeError: Julia: MethodError: no method matching display()
Closest candidates are:
  display(!Matched::REPL.REPLDisplay, !Matched::MIME{Symbol("text/plain")}, !Matched::Any) at C:\Users\user\AppData\Local\Programs\Julia-1.7.3\share\julia\stdlib\v1.7\REPL\src\REPL.jl:257
  display(!Matched::REPL.REPLDisplay, !Matched::Any) at C:\Users\user\AppData\Local\Programs\Julia-1.7.3\share\julia\stdlib\v1.7\REPL\src\REPL.jl:271
  display(!Matched::AbstractDisplay, !Matched::AbstractString, !Matched::Any) at C:\Users\user\AppData\Local\Programs\Julia-1.7.3\share\julia\base\multimedia.jl:216
  ...

```

[Previous page](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778.md?page=3)

[Next page](https://discourse.julialang.org/t/ann-pythoncall-and-juliacall/76778.md?page=5)
