NykPol
NykPol

Reputation: 61

Run a python code with arguments from Julia terminal

I would like to call a python script, which has one function and this function expects one argument. Could You please advice how to call this python script from Julia terminal with the argument from Julia environment.
I have tried pyimport, py"""run code""", exec, but unfortunately I have not found the solution yet.
Example:
Let's assume I have a file my_test.py, which I want to run from Julia. my_test.py contains only one function and execution presented below:

def print_one_number(my_number):  
    print(my_number)   
    return my_number  
print_one_number(my_number) 

In Julia I have a variable my_number_to_print. Now I want to run .py script from Julia terminal with the argument my_number_to_print defined in Julia environment. How can I do it?
Thank You!

P.S. If we don't need to "transfer" any arguments from Julia to .py script the below works well: py"""exec(open("my_simple_test.py").read())"""

Upvotes: 2

Views: 399

Answers (1)

Andrew Saydjari
Andrew Saydjari

Reputation: 81

Here is a minimal example where the python function is defined using PyCall as well. However, if you want to use a .py file, you could just import any functions you need from it into the PyCall namespace like you would in python. Then, just call by wrapping in py"function"(args1,args2).

using PyCall
py"""
def print_one_number(my_number):  
    print(my_number)   
    return my_number
"""
my_number_to_print = 10
py"print_one_number"(my_number_to_print)

Upvotes: 0

Related Questions