proc = subprocess.Popen(['python', 'printbob.py',  'arg1 arg2 arg3 arg4'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print proc.communicate()[0]

There must be a better way of doing it though, since the script is also in Python. It's better to find some way to leverage that than what you're doing.

Answer from user193476 on Stack Overflow
Top answer
1 of 8
24
Its best to import a function and use it. from otherscript import somefunc result = somefunc() print(result)
2 of 8
7
It's possible (and very common) to structure a python module so that it can be run as a standalone script or imported and run from other python code. The usual approach is something like import sys def run(*args, **kwargs): # code here def run_alt(argv): # code to process command-line arguments into the form needed by the "run" function run(processed, arguments, go, here) if __name__ == "__main__": run_alt(sys.argv) Then you can run the module as a script (possibly using the python -m switch) or import it as a module and call "run" from within some other python code. Obviously you should come up with better names than "run" and "run_alt". If the other script doesn't support this and it's not easy to modify it, you can use open to read the source code and use exec to run it. Or you can run the script in a subprocess with the subprocess module (this allows you to run arbitrary system commands, like you would type into a terminal - so for example you could run the command python scriptname.py). You will probably see people warning you away from both of these approaches due to security concerns. However, with all three approaches the security concern is exactly the same: if you got the script from an untrusted source and haven't examined the source code to see what it does, then it might contain malicious code that deletes loads of files or something. But it's just as dangerous to import untrusted python code as it is to exec it.
How to execute a python script? Aug 17, 2021
r/linuxmint
5y ago
Using Python to Open and Run Another Program? May 21, 2021
r/learnpython
5y ago
How to run two python scripts simultaneously? Feb 21, 2022
r/learnpython
4y ago
More results from reddit.com
Discussions

return value from one python script to another - Stack Overflow
Upcoming initiatives on Stack Overflow and across the Stack Exchange network... Further Experimentation with Comment Reputation Requirements · Updated design for the new live activity panel experiment · 9 Calling a python script with input within a python script using subprocess · 4 return a variable value from a subprocess in python · -5 How do you read output (assign output to variables) printed data in python? -2 running ... More on stackoverflow.com
🌐 stackoverflow.com
subprocess - running a python script and using its output file from another python script - Stack Overflow
using python 3.6 I have two python scripts each in a diffrent folder. eg X/p1.py and Y/p2.py p2 creates an output file (called out.txt) I want to run p2 from p1 and use the output file i got from p2 More on stackoverflow.com
🌐 stackoverflow.com
windows - From Python script how to run other Python script? - Stack Overflow
In my main Python script, I want to call another python script to run, as follows: python2 ~/script_location/my_side_script.py \ --input-dir folder1/in_folder \ --output-dir folder1/out_folder/ \ -... More on stackoverflow.com
🌐 stackoverflow.com
How to output to command line when running one python script from another python script - Stack Overflow
The run is completed successfully, ... researched and attempted many different ways to get this to work w/o success. Help would be much appreciated. Thanks. ... If I understand correctly you want to run multiple python scripts synchronously, i.e. one after another.... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 56224030 › execute-a-python-script-from-another-one-and-get-the-output
execute a python script from another one and get the output - Stack Overflow
May 20, 2019 - Also, it's usually a good idea to avoid shell=True if you can and in this case calling check_output(['python','P2.py']) provides the same functionality without the risk. ... I suspect you're being tricked by your IDE into thinking that P2.py has output that it does not have. Sometimes IDEs will print things that are useful for debugging but that shouldn't be confused with the output of your script.
🌐
Delft Stack
delftstack.com › home › howto › python › python run another python script
How to Run Another Python Script in Python | Delft Stack
February 12, 2024 - The subprocess module is capable of spawning new processes and can also return their outputs. This is a new module and is intended to replace several older modules like os.system, which was previously used to run a Python script in another Python script.
🌐
TutorialsPoint
tutorialspoint.com › How-can-I-make-one-Python-file-run-another
How can I make one Python file run another?
April 17, 2025 - Output: Welcome to TutorialsPoint and Have a Happy Learning · The exec() function executes dynamically created Python code. Unlike subprocess.run() or os.system(), which run external scripts as separate processes, exec() executes the code in ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › run-one-python-script-from-another-in-python
Run One Python Script From Another in Python - GeeksforGeeks
July 23, 2025 - In Python, we can run one file ... execution, subprocess module for running a script as a separate process, or os.system() function for executing a command to run another Python file within the same process....
🌐
Python Forum
python-forum.io › thread-10144.html
How to call one python script and use its output in another python script
Hi, I have two python scripts and I want to call 1st python script and use the output of this script in another python script. For example: I have two python scripts t1.py and t2.py t1.py a = 9 print at2.py import t1 b = str(a) print bWhen I ran p...
Find elsewhere
🌐
Technical Feeder
technicalfeeder.com › 2023 › 04 › python-how-to-run-another-python-script-with-arguments
Python How to run another Python script with arguments | Technical Feeder
April 5, 2023 - Another Python script can be run by exec function. However, arguments can't be specified with it. How is it implemented? Okay, use subprocess.call or subprocess.run instead.
🌐
Tutorials24x7
tutorials24x7.com › python › run-another-python-script-with-arguments-in-python-for-optimal-execution
Run Python scripts with arguments for optimal execution. Learn how.
April 29, 2024 - You can run a Python script from another script using the subprocess module. Here, you can use the subprocess.run function to execute the called_script.py script from caller_script.py and pass arg1, arg2, and arg3 (arguments) as command-line ...
🌐
Medium
medium.com › codex › running-a-python-script-using-another-python-script-2a9773464d1b
Running a Python Script using another Python Script (Modules) | by Sid Ghani | CodeX | Medium
July 24, 2022 - In this example we will create 3 python modules or scripts in this example: Script 1 = This creates a folder named “Test Folder” under My Documents · Script 2 = This creates a Text file that goes into the “Test Folder” however Script 1 NEEDS to execute first else this will error. Script 3 = This runs Script 1, then runs Script 2. This essentially is our Execution Script (It will prevent the need to open or edit Script 1 and Script 2 entirely)
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-run-another-python-script-with-arguments-in-python
How to Run Another Python script with Arguments in Python - GeeksforGeeks
April 28, 2025 - While it's typically used for importing modules, we can also use it to execute Python scripts. By importing the script as a module we can call its functions and pass arguments to them.
🌐
Luasoftware
code.luasoftware.com › tutorials › python › simple-guide-to-subprocess
Simple Guide to Subprocess (launch another python script)
December 9, 2020 - NOTE: You will notice the output of script is printed on your console. import subprocessp = subprocess.Popen(['python', 'test.py'])print('pid', p.pid)print('EXIT') Capture output and error, and will wait for process to end.
🌐
Stack Overflow
stackoverflow.com › questions › 73236594 › running-a-python-script-and-using-its-output-file-from-another-python-script
subprocess - running a python script and using its output file from another python script - Stack Overflow
Just return the results from p1.py as Python values; no need to read and parse anything from a file. ... If you just want to read the .txt file, use with open("../Y/out.txt", "r) as f: ... ... With that instruction you are trying to run a txt file. Apart from this are you sure you need subprocess? Isn't it enough to import p2 in p1 and call the function that generates out.txt? Like: from path_to_p2 import function def use_output_from_p2(): function() with open ("Y/out.txt","r") as f: #do whatever
🌐
nixCraft
cyberciti.biz › nixcraft › howto › python › python run external command and get output on screen or in variable
Python Run External Command And Get Output On Screen or In Variable - nixCraft
March 28, 2023 - You need to use the subprocess Python module which allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Another option is to use operating system interfaces provided by Python such as:
🌐
w3tutorials
w3tutorials.net › blog › run-a-python-script-from-another-python-script-passing-in-arguments
How to Run a Python Script from Another Python Script and Pass Command-Line Arguments: A Practical Guide — w3tutorials.net
Capture Output: Log stdout/stderr for debugging (e.g., print("Script output:", result.stdout)). Running a Python script from another is a powerful way to build modular, maintainable workflows. The subprocess module is the best tool for this, offering isolation, flexibility, and robust error handling.
🌐
Esri Community
community.esri.com › t5 › python-questions › output-messages-from-a-script-run-within-another › td-p › 320420
Output messages from a script run within another s... - Esri Community
November 13, 2012 - This may help - I sometimes run into issues depending with printing when migrating scripts between 'run in ArcGIS python console', 'run in a Toolbox tool', and 'run in a stand-alone (CMD shell or IDLE) mode'. This one was developed to run a log file (called fBad - because it's an error log). So, read in your command arguments or over-ride and explictly set the path the log file: if arcpy.GetParameterAsText(2): logBadData = arcpy.GetParameterAsText(2) else: logBadData = "R:\\test_pGDB" ### # Create Output Files ### #output log Bad Data timeStamp = "2012_07_10" # or whatever, use a function to stamp it!
Top answer
1 of 3
2
import subprocess
pro = subprocess.Popen('abc.py')

Is a much better way of fiddling with another scripts ins, outs, and errors.

2 of 3
0

The subprocess module is the option, but the tricky part is to follow the output un parallel with your main loop of gtk, to accomplish that goal you must have to consider the platform that you are dealing, if you are in linux you can easily run another thread and use gtk.gdk.threads_init to use threads in pygtk, but if you are planing to run your application on windows, then you should use generators and gobject.idle_add.

About the widget, use gtk.TextBuffer associated with a gtk.TextView

Here is an example with threads

import gtk
import subprocess
import threading

gtk.gdk.threads_init()

class FollowProcess(threading.Thread):
    def __init__(self, cmd, text_buffer):
        self.tb = text_buffer
        self.child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        super(FollowProcess, self).__init__()

    def run(self):
        while not self.child.poll():
            out = self.child.stdout.read(1)
            if out != '':
                gtk.gdk.threads_enter()
                self.tb.insert_at_cursor(out)
                gtk.gdk.threads_leave()

def destroy(w, cmd):
    cmd.child.terminate()
    gtk.main_quit()

i = 0
def click_count(btn):
    global i
    message.set_text('Calling button %d' %i)
    i += 1

other_command = 'python extranger.py'

w = gtk.Window()
w.resize(400, 400)

message = gtk.Label('Nothing')
tb = gtk.TextBuffer()
tv = gtk.TextView(tb)
scroll = gtk.ScrolledWindow()
scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scroll.add(tv)

box = gtk.VBox()
button = gtk.Button('Active button')

cmd = FollowProcess('python extranger.py', tb)

button.connect('clicked', click_count )

w.connect('destroy', destroy, cmd)
box.pack_start(button, False)
box.pack_start(message, False)
box.pack_start(scroll)


w.add(box)
w.show_all()
cmd.start()
gtk.main()

And in extranger.py

import time
import sys

i = 0
while True:
    print 'some output %d' % i
    sys.stdout.flush() # you need this to see the output
    time.sleep(.5)
    i += 1

Note how the button stills responsive even with the update in parallel.