Popen subprocess does not work inside a SublimeREPL? - python

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

Related

os.system not working, but typing the same thing into the command prompt works

I am trying to run python abaqus through the command prompt using
os.system('abaqus CAE noGUI=ODBMechens')
It doesn't seem to run anything, but if I go to the command prompt myself and type in
abaqus CAE noGUI=ODBMechens
it works. I am using python 2.7 on Windows 10.
Thanks
try using the subprocess module (it's newer) instead:
for example,
subprocess.call(["ls", "-l"])
and in your example, it would be:
subprocess.call('abaqus CAE noGUI=ODBMechens')
More info on the difference between subprocess module and using os.system call:
The Difference between os.system and subprocess calls
You should add before your code
import os
import subprocess
try:
os.environ.pop('PYTHONIOENCODING')
except KeyError:
pass
And then:
cmd = subprocess.Popen('abaqus CAE noGUI=ODBMechens',cwd=jobPath, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True).communicate()[0]
Variable cmd contains your output. I found that this way it works.

Invoking history command on terminal via Python script

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()

Running a bash script from Python

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

Position of prompt >>> in stderr after SystemExit

I have a script _testing_.py containing just raise SystemExit. When I run it from cmdline using py -i _testing_.py, the traceback is printed and the the prompt >>> is printed. However when I run it as subprocess via Popen, i.e. Popen("py -i _testing_.py", stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate() the resulting stderr contains first the prompt >>> and then the traceback. Can you confirm this behaviour and explain it? I'm using Python 3.3.2 on Windows.
It seems thats it's dependent bahaviour. #jentyk's observation is that the order isn't exchanged in Python 2.7 on MacOS.

subprocess.Popen in different console

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

Categories