0
>>> webbrowser.open("www.python.org")
False

Is there any other way to get the same functionality of that function in Cygwin?

Bentley4
  • 9,510
  • 22
  • 71
  • 126
  • 1
    Just a shot in the dark here, did you try `webbrowser.open("http://www.python.org")`? – Lanaru Aug 21 '12 at 21:15
  • @Lanaru: Yep, `webbrowser.open("http://www.python.org")` also returns `False`. – Bentley4 Aug 21 '12 at 21:19
  • 1
    Sounds kind of like [this Python bug](http://bugs.python.org/issue1244861), which contains a patch but no-one to test it... – dbr Aug 21 '12 at 21:20
  • Downvotes are probably because you left out crucial information like what version of Python and Cygwin you are using, which web browser you are using, etc. – Michael Hoffman Aug 22 '12 at 01:51
  • @Hoffman Ow, I'm sorry. I used Python 2.7.3 on Cygwin 1.7.16 and tried Firefox and chrome. – Bentley4 Aug 22 '12 at 09:13

2 Answers2

13
export BROWSER=cygstart

before starting Python. Then it should work.

Michael Hoffman
  • 27,420
  • 6
  • 55
  • 80
  • 2
    Works! It is even possible to assign the "BROWSER" env. var. in the Python script if doing `import os; os.environ['BROWSER'] = 'cygstart'` **before** `import webbrowser`. – Morten Zilmer Jan 24 '15 at 11:12
  • Python 3.4.1 does not need changes to "BROWSER" env. var., but e.g. 3.2 did, so looks like the issue has been addressed. – Morten Zilmer Jan 24 '15 at 11:43
  • 3
    I submitted a patch to fix this almost 10 years ago, ["Enable `os.startfile` and `webbrowser.WindowsDefault` on Cygwin"](http://bugs.python.org/issue1244861). It was never accepted. I don't know what changed in Python 3.4.1 but if this works now, I'm glad. – Michael Hoffman Jan 24 '15 at 13:20
  • 1
    Suppose if you don't want set 'BROWSER' env variable, adding following snipper should help, `webbrowser.register('cygstart', None, webbrowser.GenericBrowser('cygstart'), -1)` – Arunprasad Rajkumar May 23 '18 at 08:21
2

Launching a web browser from Cygwin can be done through

cygstart "http://www.google.com"

where google.com is your desired URL.

cygstart launches the default windows program for a path, so this way you get the user’s preferred web browser.

So in Python under Cygwin you might just:

from subprocess import call
call(["cygstart", "http://www.google.com"])

or try another option from here to run the Python-external command.

If it's not just a one-off script you are writing for your own use, you should use platform.system to use the above on Cygwin, and webbrowser.open on other platforms.

Community
  • 1
  • 1
tricasse
  • 1,121
  • 12
  • 17