1

I am running this python code from the command line:

# run on command line as: python firstscript.py    
import sys, subprocess 

pid = subprocess.Popen([sys.executable, 'secondscript.py']).pid
sys.exit()

Unfortunately I can't get it to exit all the way to the command line. If I hit the enter key (on OSX) it will finally exit. Is there a way to force the script to exit all the way to the command line without lingering in this weird limbo state? Also, I don't want to redirect stdout or stderr anywhere else because if I do, I lose the ability in secondscript.py to log output to a log file.

Thanks for the help.

2
  • You may find this helpful: stackoverflow.com/questions/4084322/… Commented Oct 22, 2016 at 0:59
  • Any chance firstscript.py has exited to the command line, but secondscript.py kept writing text to the console, hiding the prompt that was printed when the first script exited? Go read your scrollback history carefully. Pressing Enter just makes the terminal print a new, fresh prompt. Commented Oct 22, 2016 at 1:18

2 Answers 2

1

The changes below worked for me:

# run on command line as: python firstscript.py    
import sys, subprocess 

process = subprocess.Popen([sys.executable, 'secondscript.py'])
output = process.communicate()[0]
Sign up to request clarification or add additional context in comments.

Comments

0

You seem to be asking if there is a better way to do this. Check out check_output. I have always found it much more convenient and fool proof compared to the lower level stuff in subprocess.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.