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

pycall

#1

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

#2

Just do what you would have done in python

julia> using PyCall

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

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

#3

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
#4

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


#5

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


#6

confused still…


#7

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…