Question

I'm writing a PyGTK application on Windows, and in a function, I'm trying to open a webpage with the webbrowser module. It should be the simplest thing in the world, but instead of opening in a browser, it prints the HTML source of the page to the console. Does anyone know why this would happen?

The code in question:

oauthURL = ("http://api.twitter.com/oauth/authorize?oauth_token=" + requestToken)
webbrowser.open(oauthURL, 2, True)

I tested it on my Arch Linux laptop just now, and it works fine, so this is a Windows-specific problem. Perhaps Python can't find a browser to use?

Was it helpful?

Solution

What it normally tries to do (on Windows) is using os.startfile(url), which launches the default application for http urls. You can check what this does by typing start http://www.example.com at the command prompt. If this has the same effect, you know what the problem is, and you should reconfigure your default browser in Windows.

This behaviour can be overridden by the user, through the BROWSER environment variable, so you might want to check that if the above doesn't help.

OTHER TIPS

This code snippet can handle opening a web-page in a cross-platform way:

if sys.platform[:3] == 'win':
    try:
        os.startfile(oauthURL)
    except WindowsError as why:
        # your error handling code
        print(why)
else:
    webbrowser.open(oauthURL, 2, True)

It is based on IDLE's EditorWindow.py "python_docs" function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top