3

I want know how to pass information from one python program to another. Basically, I will open one python program which uses the os.system(file) command to execute another pytgon program. Let's take an example:

The "Parent" program:

import file  #file is the child
import os
num=int(input("Enter number: "))
if num%2==0:
    os.system('python file.py')
else:
    pass

Now the "Child" program:

name=input("Enter Name: ")
age=int(input("Enter age: "))
print("Hi",name)

So in the example, when the user enters an even number, the program starts up the child program, which in turn will ask the user his name and age.

Now my question is this: If I want to bring back the information (name and age) entered in the child program back to the parent program, how do I do it?

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • 1
    Does this answer your question? [What is the best way to call a script from another script?](https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script) – deadshot Sep 07 '20 at 06:56
  • 2
    One way programs communicate is through their standard input and standard output streams. Alternatively, for input, you could pass arguments to the other script. In either case, I highly recommend you don't use `os.system` and instead use the `subprocess` module. – juanpa.arrivillaga Sep 07 '20 at 06:57
  • 2
    Note, `import child` doesn't make sense if you are going to be calling it as a seperate process. Indeed, what you *ideally* should do with two python modules like this is write them so that they can be imported, and their public API exposes whatever functionality you want. In which case, just ditch `os` and `subprocess` and just `import file; value = file.foobar(whatever)` and you have `value` – juanpa.arrivillaga Sep 07 '20 at 07:01
  • 1
    "Now my question is this: If I want to bring back the information (name and age) entered in the child program back to the parent program, how do I do it?", save contents to a file in your child program, read that file in your parent program. That would be one of the multiple ways, you can do fancier things like sockets or IPC if your use case really needs such things. – J. García Sep 07 '20 at 07:02
  • 1
    Does this answer your question? [Passing values between modules in Python](https://stackoverflow.com/questions/11428651/passing-values-between-modules-in-python) – Tomerikoo Sep 07 '20 at 07:05
  • @J. García can you pls show me how to what you are saying, that is, saving contents to the file and reading it in the parent program? – Sumathi Iyengar Sep 07 '20 at 07:37

2 Answers2

7

Using import, you can access variables defined in another module. But in this case, the clean way to do this will be to put the code in the "child" [see footnote] inside a function, so that it is not run when the module is imported (which should usually be near the top of the module that does the import) but when you actually want to call the function -- so that the questions are only asked if you actually enter an even number. For example:

main.py

import myfile

num = int(input("Enter number: "))
if num%2==0:
    name, age = myfile.get_name_and_age()
    print("The name is ", name)

myfile.py

def get_name_and_age():
    name=input("Enter Name: ")
    age=int(input("Enter age: "))
    print("Hi",name)
    return name, age
  • Note: I have kept your use of the word "child" above based on the subprocess that you were using -- but the approach shown here is not using parent and child processes. Everything is run in the same python process if you do it this way.
alani
  • 12,573
  • 2
  • 13
  • 23
  • My main purpose for this question was to have a main program with a mysql database connection and this child program, which will be a Tkinter user interface, note that I want them to be separate programs, and I needed a way to bring back information entered in the child program to the original program which will in turn update the database. Thanks to all for your support. Peace! – Sumathi Iyengar Sep 07 '20 at 11:58
0
  1. Shared memory objects - platform depended feature. - not good solution.
  2. Simple files. - bad solution.
  3. ZeroMQ https://www.google.com/url?sa=t&source=web&rct=j&url=https://zeromq.org/&ved=2ahUKEwjFw9zJy63xAhVBlosKHTx1AWQQFjABegQICBAC&usg=AOvVaw29tSOVdy8XLINq5KCJc0WD , GCP Pub/Sub https://www.google.com/url?sa=t&source=web&rct=j&url=https://cloud.google.com/pubsub&ved=2ahUKEwjukOLhy63xAhVyoosKHZXXA-cQFjAAegQIGxAC&usg=AOvVaw380hk9SKYYO-19Aiqvhmj7 AWS SNS https://aws.amazon.com/sns/ ... - may be the best solution for.
  4. Man, use Flask and REST approach... or RPC, but REST is better