1

My ultimate goal is to run a Python script (the code need to call some subprocess) without terminal.

I tried with python launcher on Mac. The code works correctly when the "Run in a Terminal window" is checked.

(Notice that the shell=False is default here.) However, it won't run the subprocess correctly when I uncheck that option.

I also tried to run in with Mac OS automator (Run Shell Script) — negative too.

Here is a small sample code.

import subprocess

with open("record","w") as f:
    subprocess.call(["which","ipython"], stdout = f)  # this line fails
    subprocess.call(["open","."])  # this line always works

If I run from terminal, it creates a record file, which shows the ipython path. However when I run it from automator or python launcher without terminal window, it creates an empty file.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jason Liu
  • 55
  • 1
  • 8
  • you could try to redirect all standard streams: stdin, stdout, stderr (use [`subprocess.DEVNULL` or its analog](http://stackoverflow.com/a/11270665/4279)) – jfs Oct 10 '15 at 09:30
  • Thank you Sebastian. However I still need to parse stdout in my case. – Jason Liu Oct 11 '15 at 03:29
  • if you need stdout as a string; use `output = check_output(cmd, stdin=DEVNULL, stderr=DEVNULL)` – jfs Oct 11 '15 at 03:30
  • Thank you again! Unfortunately I need to keep the process alive and monitor the stdout. I used non-blocking stream in my situation (based on this [link](http://eyalarubas.com/python-subproc-nonblock.html) ). The full scope of my project is to create a GUI for ipython notebook. The project is [here](https://github.com/jiayiliu/inlauncher/blob/master/gui.py) – Jason Liu Oct 14 '15 at 02:15
  • Here's a [portable way "to keep the process alive and monitor the stdout"](http://stackoverflow.com/a/32682520/4279). If you need to support only Unix systems then you could use more straightforward [`createfilehandler()`-based solution](http://stackoverflow.com/questions/15362372/display-realtime-output-of-a-subprocess-in-a-tkinter-widget#comment43779465_15362372). A GUI for ipython notebook is a completely different question: look at how the current web-gui is done -- use the provided interface to communicate with ipython notebook kernels. – jfs Oct 14 '15 at 05:51

0 Answers0