I am using node js
for my application as well as react.
I am trying to use child_process
to call a python function from my node js code, using this post to guide me. It seems pretty straightforward, until I realized that the child_process
was used to call a python script, not a main method with functions.
I am relatively new to Python, so forgive me if I mix up terminology here and there. Here is a very basic version of my python file below:
import sys
# other imports
def __main__():
one = function_one()
two = function_two()
arr = [one, two]
print(arr)
sys.stdout.flush()
def function_one():
# do stuff, pretend it returns 'hello'
def function_two():
# do stuff, pretend it returns 'world'
if __name__ == '__main__':
__main__()
The end result should be ['hello', 'world']
, but it seems like I am not getting returned anything. As you can see, I am printing arr
and flushing it afterwards, so it should work.
The only way I can get it to work is if my file looks like this:
import sys
print('hello world')
sys.stdout.flush()
As you can see, without the main method or additional functions. Is there a reason to this or am I just implementing it wrongly? Thanks!
EDIT:
After trying a couple different things, I found out that some of my imports were breaking, such as pandas
or seaborn
. It works if I remove those specific imports, even if I'm calling __main__
through the if statement. Any ideas why?