# Calling julia functions from python

**URL:** <https://discourse.julialang.org/t/calling-julia-functions-from-python/6885>\
**Category:** General Usage\
**Tags:** python, pyjulia\
**Created:** [November 4, 2017, 8:10pm UTC](https://discourse.julialang.org/t/calling-julia-functions-from-python/6885 "2017-11-04T20:10:18Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [November 5, 2017, 3:58am UTC](https://discourse.julialang.org/t/calling-julia-functions-from-python/6885/2 "2017-11-05T03:58:56Z")

</div>

I think `pyjulia` needs a little love. I distinctly remember figuring this out a long time ago, but am unable to do so in an easy fashion right now.

The best I could come up with is to take advantage of the fact that, when you include a file in julia, the last “value” in the file is returned. If the last thing in the file, is a function, then you get back an anonymous version of that function:

```julia
# test.jl

function myArrayFn{T<:Number}(x::Array{T})
    println("array size: $(size(x))");
    println("max element: $(maximum(x))")
    println("min element: $(minimum(x))")
    return 2x
end

```

```python
In [1]: import julia

In [2]: j = julia.Julia()

In [3]: fn = j.include('test.jl')

In [4]: fn
Out[4]: <PyCall.jlwrap myArrayFn>

In [5]: import numpy as np

In [6]: x = np.array([[1,2,3], [4,5,6]], dtype=np.float64)

In [7]: fn(x)
array size: (2,3)
max element: 6.0
min element: 1.0
Out[7]:
array([[2., 4., 6.],
       [8., 10., 12.]])

```

This only works for the last function in the file.

I tried a number of other things (using `include`, `include_string`, etc.), and nothing seemed to modify the Julia environment available in the python Julia object. It might be that the internals have changed, and that `pyjulia` hasn’t kept up. Or maybe I’m just doing something wrong. I haven’t tried digging into the code to see why.

Anyway, hope this helps, and maybe someone else will respond with better info.

Cheers,  
Kevin

---

_[View the full topic](https://discourse.julialang.org/t/calling-julia-functions-from-python/6885)._
