I hope this is not a duplicate.
I'm trying to use subprocess.Popen() to open a script in a separate console. I've tried setting the shell=True parameter but that didn't do the trick.
I use a 32 bit Python 2.7 on a 64 bit Windows 7.
To open in a different console, do (tested on Win7 / Python 3):
from subprocess import Popen, CREATE_NEW_CONSOLE
Popen('cmd', creationflags=CREATE_NEW_CONSOLE)
input('Enter to exit from Python script...')
Related
How can I spawn new shells to run python scripts from a base python script?
from subprocess import *
c = 'dir' #Windows
handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()
If you don't use shell=True you'll have to supply Popen() with a list instead of a command string, example:
c = ['ls', '-l'] #Linux
and then open it without shell.
handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE)
print handle.stdout.read()
handle.flush()
This is the most manual and flexible way you can call a subprocess from Python.
If you just want the output, go for:
from subproccess import check_output
print check_output('dir')
To open a new console GUI window and execute X:
import os
os.system("start cmd /K dir") #/K remains the window, /C executes and dies (popup)
On Linux shell=True will do the trick:
command = 'python someFile.py'
subprocess.Popen('xterm -hold -e "%s"' % command)
Doesn't work with gnome-terminal as described here:
https://bbs.archlinux.org/viewtopic.php?id=180103
Related
I use conda to create a Python 2.7 environment including the R package. If I open a Python session in a console, I can check that R is indeed installed with the Popen constructor:
$ python
>>> from subprocess import Popen, PIPE
>>> proc = Popen(["which", "R"], stdout=PIPE, stderr=PIPE)
>>> proc.wait()
0
where the 0 means it is installed. But if I try the same commands from within a Sublime Text 3 REPL running under the exact same Python environment, I get a 1.
Why is this and how can I fix it?
You need to communicate:
proc = Popen(['which', 'python'], stdout=PIPE)
proc.communicate()
('/Users/Kelvin/virtualenvs/foo/bin/python\n', None)
wait just waits for the subprocess to complete and gives you the return code (which is 0 if its successful)
if you get a different error code (1 meaning it failed), I'd look into confirming your virtual environment. try sys.executable
Using python I can get either of these to work:
subprocess.call(['wine', 'cmd'])
os.system("wine cmd")
I'm using Ubuntu and python 3.5, Once I get into the wine cmd prompt I can no longer run commands, non of the ways to run multiple commands that I have seen online work, they don't error out, it just opens the cmd and pauses, I think it treats the cmd once open as a running command and is waiting to move on to the next command which it assumes is for the shell not the wine cmd, how can i then run commands inside the wine cmd once opened?
edit: Basically any time I run a command that requires further user input from within that command, how do I interact inside of that command?
You could build up from DOS through BASH to python as in the example code here. I cut and paste the code into python 2.7 and it worked, but you might like to confirm on 3.5
If you specifically need interaction rather than just running a DOS command then you could use subprocess.Popen.communicate to interact with your script which then interacts with wine/dos.
import subprocess, os, stat
from subprocess import Popen
from subprocess import PIPE
from subprocess import check_output
command_script="/tmp/temp_script.sh"
f1 = open(command_script,'w')
f1.write("#!/bin/bash\n")
#to run a dos command
#f1.write(r'WINEPREFIX=/path/tp/wine/prefix wine cmd /c #mydoscommand argval1'+'\n')
#for example
f1.write(r'wine cmd /c #echo Hello_world'+'\n')
#or to run a specifically pathed executable
#f1.write(r'WINEPREFIX=/path/tp/wine/prefix wine "c:\\Program Files (x86)\\path\\to\\executable.exe" additionalargs '+'\n')
f1.close()
st = os.stat(command_script)
os.chmod(command_script, st.st_mode | stat.S_IEXEC)
p = Popen(command_script, stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
rc = p.returncode
print output
os.remove(command_script)
Have a look at the answers where I nicked some of the code from Running windows shell commands with python and calling-an-external-command-in-python
I am running a python script which is using subprocess to execute "history" command on my Ubuntu terminal. Apparently,I am getting this error
history: not found
I got to know that history can not be invoked by any script by default.
What can I do to overcome this? Or any other possible alternatives.
readline.get_history_item() method isnt working either.
Use this:
from subprocess import Popen, PIPE, STDOUT
e = Popen("bash -i -c 'history -r;history' ", shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = e.communicate()
I need to run a bash script from Python. I got it to work as follows:
import os
os.system("xterm -hold -e scipt.sh")
That isn't exactly what I am doing but pretty much the idea. That works fine, a new terminal window opens and I hold it for debugging purposes, but my problem is I need the python script to keep running even if that isn't finished. Any way I can do this?
I recommend you use subprocess module: docs
And you can
import subprocess
cmd = "xterm -hold -e scipt.sh"
# no block, it start a sub process.
p = subprocess.Popen(cmd , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# and you can block util the cmd execute finish
p.wait()
# or stdout, stderr = p.communicate()
For more info, read the docs,:).
edited misspellings
So I am running a command in my python py file
myNewShell = os.system('start "%s" /d "%s" cmd /f:on /t:0A /k "W:\\Desktop\\alias.bat"' % (myShot, myWorkDir))
This opens up a shell
How exactly would I input something into this shell directly from my python script, thus bypassing your actual cmd.exe. I have a bunch of DOSKEYs set up, such as maya which opens up the maya program. How would I add a line of code to my python script, so that it loads the shell with those aliases and inputs my command directly
Take a look at the powerful and useful subprocess module
You can then do code like this
import subprocess
pro = subprocess.Popen("cmd", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
pro.stdin.write("mybat.bat\n")
pro.stdin.write("myother.bat\n")
pro.stdin.write("start mysillyprogram\n")
pro.stdin.flush()
pro.terminate() # kill the parent