# How to load and call functions defined in a standalone Python script?

**URL:** <https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601>\
**Category:** General Usage\
**Tags:** pycall\
**Created:** [November 28, 2016, 12:45am UTC](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601 "2016-11-28T00:45:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 28, 2016, 12:45am UTC](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601/1 "2016-11-28T00:45:43Z")

</div>

Can PyCall be used to load definitions from a small Python script into Julia? For example, how can I call `foo()` defined in the Python script below in Julia?

```python
a = 1
b = 2
def foo():
    print("Hello world!")
    return a + b

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 28, 2016, 1:11am UTC](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601/2 "2016-11-28T01:11:29Z")

</div>

Just do what you would have done in python

```julia
julia> using PyCall

julia> push!(pyimport("sys")["path"], pwd());

julia> pyimport("a")[:foo]()
Hello world!
3

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 28, 2016, 4:25am UTC](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601/3 "2016-11-28T04:25:23Z")

</div>

Thank you @yuyichao, I will give the solution a try. 🙂

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [February 7, 2019, 7:50am UTC](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601/4 "2019-02-07T07:50:22Z")

</div>

“path” is the path to the python file, right?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 7, 2019, 3:04pm UTC](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601/5 "2019-02-07T15:04:43Z")

</div>

No. As I said, do what you would’ve done in python (add an path to `sys.path`).

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [February 7, 2019, 3:12pm UTC](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601/6 "2019-02-07T15:12:03Z")

</div>

confused still…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 7, 2019, 3:13pm UTC](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601/7 "2019-02-07T15:13:09Z")

</div>

`pyimport("sys")["path"]` was accessing `sys.path` in python (the syntax might have changed). I’m not really sure what(else) you are asking about…

---

<div class="post-metadata">

**Author:** ![larrylan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/larrylan/32/9731_2.png) [@larrylan](https://discourse.julialang.org/u/larrylan)\
**Post date:** [August 25, 2019, 9:23pm UTC](https://discourse.julialang.org/t/how-to-load-and-call-functions-defined-in-a-standalone-python-script/601/8 "2019-08-25T21:23:39Z")

</div>

Hi yuyichao,

Thank you for your solution. It’s just what I want to find. But 2 years later the syntax may have changed. I tried your suggested code in JuliaPro 1.0.4 win64 and met several errors.  
According to Julia’s suggestion, I changed  
push!(pyimport(“sys”)[“path”], pwd())  
to  
pushfirst!(PyVector(pyimport(“sys”).“path”), “”)  
It worked.  
Bit there’s a warning for  
pyimport(“a”):foo  
Warning: `@pyimport foo` is deprecated in favor of `foo = pyimport("foo")`  
I changed it to  
foo = pyimport(“foo”)  
or  
foo = pyimport(“foo.py”)  
Bot of them did not work.  
I searched on internet and found no answers.  
Do you know the updated syntax to call a Python script .py file?  
I also want to call one of the functions in that file. Something like  
y = foo.fun\_01(5)  
println(“y=”, y)

Your reply will be appreciated.

Thanks,

Larry
