Is it possible to return a value from one Python file to another?

I am wondering if it is possible to run another file: os.startfile ('File.py') and this file returns a value to the file that calls another file.

For example, you have File1. Is it possible for File1 to call and run File2 and have File2 return a value in File1?

+4
source share
2 answers

Why are you running Python scripts? the usual way is to import one module ("another file") into a Python script and call public functions from there. What is the import module mechanism for? Read the related documentation.

As mentioned in the comments, this question was asked before. Take a look at this answer for more help.

+7
source

Not with os.startfile() , no; it does not provide communication with the running process. However, you can use the subprocess module; This will allow you to send data and receive data from a running process through standard input / output. Or, since the thing you want to call is another Python script, just import another file and call its functions directly or use execfile() .

+2
source

Source: https://habr.com/ru/post/1483674/


All Articles