The goal here is to run a new python file in a new shell from and existing python file in an existing shell. Say i have two files, aaa.py and bbb.py. Lets say for simplicity that all aaa.py does is...
subprocess.call('python bbb.py', shell=True)
...and lets say that bbb.py does is...
print 'It worked'
Now the goal is to run aaa.py in terminal 1 and get it to launch bbb.py in terminal 2. I would expect something like the command below to exist, but can't figure it out.
subprocess.call_in_new_window('python bb.py', shell=True)
You may also find that you have a Start-menu entry such as Start ‣ Programs ‣ Python 3. x ‣ Python (command line) that results in you seeing the >>> prompt in a new window.
To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!
To run the Python Shell, open the command prompt or power shell on Windows and terminal window on mac, write python and press enter. A Python Prompt comprising of three greater-than symbols >>> appears, as shown below. Now, you can enter a single statement and get the result.
You can run any Python script in a command-line interface. Python and associated Python scripts can be run using command-line interfaces. Windows users can use command prompt while Mac and Linux users can make use of Terminal.
There's no way to do this in general from a shell. What you have to do is run the terminal program itself, or some launcher program that does so for you. And the way to do that is different for each terminal program.
In some cases, os.startfile
will do what you want, but this isn't going to be universal.
Also, note in general, you're going to actually need an absolute path to your script, because the new terminal window will be running a new shell and therefore won't necessarily have your same working directory. But I'll ignore that for the examples.
With Windows cmd, the easiest way to do it is the start
shell command. If the thing you start
is any command-line program, including python
, it will get a new cmd window. So, something like:
subprocess.call('start /wait python bb.py', shell=True)
OS X has a similar command, open
. And it's a real program rather than a shell command, so you don't need shell=True
. However, running a command-line program or script with open
doesn't generally open a new terminal window. In fact, the whole point of it is to allow you to run programs as if they were being double-clicked in Finder, which never runs something in the terminal unless it's a .command file.
So, you can create a temporary .command
wrapper file and open
that; something like this (untested):
with tempfile.NamedTemporaryFile(suffix='.command') as f: f.write('#!/bin/sh\npython bb.py\n') subprocess.call(['open', '-W', f.name])
Alternatively, you can explicitly tell open
to use Terminal.app, something like this:
subprocess.call(['open', '-W', '-a', 'Terminal.app', 'python', '--args', 'bb.py'])
Or you can script Terminal.app via AppleEvents. For example:
appscript.app('Terminal').do_script('python bb.py')
The "do script" event opens a new window and runs its argument as a command. If you want more detailed control, open the scripting dictionary in AppleScript Editor and see all the fun stuff you can do.
On Linux or other *nix systems… well, there are 65,102 different desktop environments, launchers, and terminal programs. Do you need to work on all of them?
With gnome-terminal, just running the terminal again gives you a new window, and the -x
argument lets you specify an initial command, so:
subprocess.call(['gnome-terminal', '-x', 'python bb.py'])
Many older terminals try to be compatible with xterm
, which does the same thing with -e
, so:
subprocess.call(['xterm', '-e', 'python bb.py']) subprocess.call(['rxvt', '-e', 'python bb.py'])
… etc.
How do you know which terminal the user is using? Good question. You could walk the like of parent processes from yourself until you find something that looks like a terminal. Or you could just assume everyone has xterm
. Or you could look at how various distros configure a default terminal and search for all of them. Or…
This should probably be a comment, but since I can't yet...
In Windows , you can do:
subprocess.call('python bb.py', creationflags=subprocess.CREATE_NEW_CONSOLE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With