2

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())"""

NykPol
  • 61
  • 3
  • Are you getting a --`TypeError: print_one_number() missing 1 required positional argument: 'my_number'`-- message anywhere, that's what would happen if you ran that Python script by itself. – BatWannaBe Dec 08 '21 at 13:03
  • you define `def print_one_number(my_number):` so you have to run it as `print_one_number(some_value)`. If you run `print_one_number()` then you should get error message. – furas Dec 08 '21 at 13:45
  • do you get error message when you run it in console/terminal? always put full error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information. – furas Dec 08 '21 at 13:46
  • @BatWannaBe, sorry, it was just the mental shortcut. Thank You for pointing this out. I have added the argument here "print_one_number(my_number)", so now it is more precise. – NykPol Dec 08 '21 at 15:20
  • @furas, sorry, it was just the mental shortcut. Thank You for pointing this out. I have added the argument here "print_one_number(my_number)", so now it is more precise. In general, I have not included error message, because I have tried tens of different methods and no-one worked, so I am looking now for general solution of how to run .py script from Julia terminal taking into consideration that .py script expects one variable which is defined in Julia environment. – NykPol Dec 08 '21 at 15:20

1 Answers1

0

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)