Return data from python script to another python scrip

I would like to call a python script from a different python script and pass the data back to the original on a windows machine.

Main python Script:
import OtherPythonScript

results = Execute OtherPythonScript

OtherPythonScript
do x:
    record results to y

return y
LVL 1
m_travisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peprCommented:
Put the functionality to a function inside the OtherPythonScript
def my_function():
    # get the y value somehow
    return y

Open in new window

and call the function from the other script -- almost as you guessed
import OtherPythonScript
result = OtherPythonScript.my_function()

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Walter RitzelSenior Software EngineerCommented:
I think @pepr solution will work if you can change your script into a function, but I think this may not be your final solution depending on your requirements.

Maybe a little more context on your problem could help us to direct you better.
gelonidaCommented:
A little context would help to give the right advice for your case.

My first impression is, that for most cases pepr gave exactly the right answer. (especially if the script that you want to call is under your control). Just import and call the function


Some options you have are:
1) import the python script as module and execute the functions required (pepr's suggestion if you can modify the code)
2) Same as above but start in a separate thread
3.) import the other module and use the multiprocessing module for data exchange
3.) start script as a subprocess, collect it's stdout
4.) start the other script / process, write the results to a file and read the file when the script is finished
5.) start the other script as an RPC server (XMLRPC), and perform RPC calls
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.