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



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?

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


Just do what you would have done in python

julia> using PyCall

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

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


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

Call a python function (under a virtual environment) (use a returned python object) from julia

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


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


confused still…


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…