Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resume program (or exit) after opening webbrowser?

I'm making a small Python program, which calls the webbrowser module to open a URL. Opening the URL works wonderfully.

My problem is that once this line of code is reached, the problem is unresponsive. How do I get the program to proceed past this line of code and continue to execute? Below the problematic line is the problematic line, in context:

if viewinbrowser == "y":
    print "I can definitely do that. Loading URL now!"
    webbrowser.open_new(url)
    print "Exiting..."
    sys.exit()

The program does not get as far as executing the print "Exiting...", which I added because I noticed the program wasn't leaving the if statement for some reason.

I am running this program from the command line, in case that's important. Edit: I am running on Kubuntu 9.04 i386, using KDE 4.3 via backports. I use Firefox 3.5 as my default browser, declared in the System Settings for KDE, and it is called correctly by the program. (At least, a new tab opens up in Firefox with the desired URL—I believe that is the desired functionality.) /Edit

Also, I assume this problem would happen with pretty much any external call, but I'm very new to Python and don't know the terminology to search for on this site. (Searching for "python webbrowser" didn't yield anything helpful.) So, I apologize if it's already been discussed under a different heading!

Any suggestions?

like image 833
conorsch Avatar asked Jul 19 '09 04:07

conorsch


2 Answers

This looks like it depends on which platform you're running on.

  • MacOSX - returns True immediately and opens up browser window. Presumably your desired behavior.
  • Linux (no X) - Open up links textmode browser. Once this is closed, returns True.
  • Linux (with X) - Opens up Konquerer (in my case). Returns True immediately. Your desired behavior.

I'm guessing you're on Windows, which, as another commentor mentioned doesn't have fork. I'm also guessing that the webbrowser module uses fork internally, which is why it's not working for you on Windows. If so, then using the threading module to create a new thread that opens the webbrowser might be the easiest solution:

>>> import webbrowser
>>> import threading
>>> x=lambda: webbrowser.open_new('http://scompt.com')
>>> t=threading.Thread(target=x)
>>> t.start()
like image 90
Edward Dale Avatar answered Nov 09 '22 09:11

Edward Dale


The easiest thing to do here is probably to fork. I'm pretty sure this doesn't work in Windows unfortunately, since I think their process model might be different from Unix-like operating systems. The process will be similar, though.

import os
import sys

pid = os.fork()
if pid:
    # we are the parent, continue on
    print("This runs in a separate process from the else clause.")

else:
    # child runs browser then quits.
    webbrowser.open_new(url)
    print("Exiting...")
    sys.exit()
like image 22
McPherrinM Avatar answered Nov 09 '22 08:11

McPherrinM