# Calling a Python script with PyCall

**URL:** <https://discourse.julialang.org/t/calling-a-python-script-with-pycall/36018>\
**Category:** New to Julia\
**Created:** [March 15, 2020, 6:13pm UTC](https://discourse.julialang.org/t/calling-a-python-script-with-pycall/36018 "2020-03-15T18:13:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [March 15, 2020, 6:13pm UTC](https://discourse.julialang.org/t/calling-a-python-script-with-pycall/36018/1 "2020-03-15T18:13:04Z")

</div>

I have some old Python set of scripts, and would like to call a function (just one function) from there into Julia. How can I do it? Win10, conda.

```julia
using PyCall
pushfirst!(PyVector(pyimport("sys")."path"), "")
mys = pyimport("py_test.py")

```

results in

```julia
The Python package py_test.py could not be found by pyimport. Usually this means
that you did not install py_test.py in the Python version being used by PyCall.

PyCall is currently configured to use the Python version at:

C:\Users\eben60\AppData\Local\Continuum\miniconda3\python.exe

and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the py_test.py module.

```

Is **installing** my Python script the only way to get it callable from Julia? If so, what would be the simplest way to do it?

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [March 16, 2020, 7:50pm UTC](https://discourse.julialang.org/t/calling-a-python-script-with-pycall/36018/2 "2020-03-16T19:50:38Z")

</div>

OK, I have found a workaround: Using pyjulia to call Julia function from Python and passing the Julia function the reference to my Python object, like this:

_py\_test.py :_

```julia
class pyob():
    def __init__ (self, x):
        self.a = x

    def amulx(self, x):
        return self.a*x

```

_jul\_test.jl :_

```julia
function jlf(pyfun)
    for i in 1:2
        println(pyfun(i))
    end
end

```

_py\_wrap.py :_

```julia
from py_test import pyob
from julia import Main as JL
JL.include("jul_test.jl")

p = pyob(1.0)
JL.jlf(p.amulx)

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 16, 2020, 7:54pm UTC](https://discourse.julialang.org/t/calling-a-python-script-with-pycall/36018/3 "2020-03-16T19:54:59Z")

</div>

> [@Eben60](#):
>
> pyimport(“py\_test.py”)

I think you meant to write `pyimport("py_test")`.

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [March 16, 2020, 8:42pm UTC](https://discourse.julialang.org/t/calling-a-python-script-with-pycall/36018/4 "2020-03-16T20:42:05Z")

</div>

> [@tkf](#):
>
> `pyimport("py_test")` .

thank you! Now the import doesn’t throw an error.

But now the next question: How do I call an object from the script? Calling `p = mys.py_test.pyob(2)` or `p = mys.pyob(2)` or `p = pyob(2)` results in an error

`ERROR: LoadError: KeyError: key "pyob" not found`

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [March 16, 2020, 9:15pm UTC](https://discourse.julialang.org/t/calling-a-python-script-with-pycall/36018/5 "2020-03-16T21:15:21Z")

</div>

After I restarted Julia REPL, it now works OK:

```julia
p = mys.pyob(2)
@show p.amulx(3.0)
> p.amulx(3.0) = 6.0

```

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [March 17, 2020, 11:01am UTC](https://discourse.julialang.org/t/calling-a-python-script-with-pycall/36018/6 "2020-03-17T11:01:01Z")

</div>

In case the calling Julia script and the called Python script are in the same directory (e.g. ./src ), to properly include the directory:

```julia
using PyCall
scriptdir = @ __DIR__
pushfirst!(PyVector(pyimport("sys")."path"), scriptdir)
mytest = pyimport("test_py")

```

---

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [November 6, 2024, 6:50am UTC](https://discourse.julialang.org/t/calling-a-python-script-with-pycall/36018/7 "2024-11-06T06:50:26Z")

</div>

Hello!

```julia
scriptdir = @ __DIR__
pushfirst!(PyVector(pyimport("sys")."path"), scriptdir)
mytest = pyimport("test_py")

```

What is a good place in the code to include this? I don’t need to execute these commands everytime I would use the Python module. Is there a way to check if the path has been corrected and module imported already?
