Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

python's webbrowser launches IE, instead of default browser, on Windows relative path

I'm attempting to launch a local html file from python in the default browser (right now my default is Google Chrome if I double-click on a .html file, Chrome launches.)

When I use python's webbrowser.open(), IE launches instead, with a blank address bar.

Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import webbrowser
>>> filename = 'test.html'
>>> webbrowser.open('file://'+filename)
True
>>> print(webbrowser.get().__class__.__name__)
WindowsDefault

I've checked my default programs and they look correct. I'm on Win 7 SP1. Why is Chrome not launching?

Update: The code will be running on unknown OS's and machines, so hardcoding or registering browsers or path updates are not options. I'm thinking that parsing the url for file:// and then doing an os.path.exists check and os.path.realpath might be the answer.

question from:https://stackoverflow.com/questions/5916270/pythons-webbrowser-launches-ie-instead-of-default-browser-on-windows-relative

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My main issue was a bad URL by attempting prepend file:// to a relative path. It can be fixed with this:

webbrowser.open('file://' + os.path.realpath(filename))

Using webbrowser.open will try multiple methods until one "succeeds", which is a loose definition.

The WindowsDefault class calls os.startfile() which fails and returns False. I can verify that by entering the URL in the windows run command and seeing an error message rather than a browser.

Both GenericBrowser and BackgroundBrowser will call subprocess.Popen() with an exe which will succeed, even with a bad URL, and return True. IE gives no indication of the issue, all other browsers have a nice messages saying they can't find the file.

  1. GenericBrowser is set by the environment variable BROWSER and is first.
  2. WindowsDefault is second.
  3. BackgroundBrowser is last and includes the fall back IE if nothing else works.

Here is my original setup:

>>> import webbrowser
>>> webbrowser._tryorder
['windows-default',
 'C:\Program Files\Internet Explorer\IEXPLORE.EXE']
>>> webbrowser._browsers.items()
[('windows-default', [<class 'webbrowser.WindowsDefault'>, None]),
 ('c:\program files\internet explorer\iexplore.exe', [None, <webbrowser.BackgroundBrowser object at 0x00000000022E3898>])]
>>>

Here is my setup after modifiying the environment variables:

C:>path=C:Program Files (x86)Mozilla Firefox;%path%

C:>set BROWSER=C:UsersScottAppDataLocalGoogleChromeApplicationchrome.exe

C:>python
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import webbrowser
>>> webbrowser._tryorder
['C:\Users\Scott\AppData\Local\Google\Chrome\Application\chrome.exe',
 'windows-default',
 'firefox',
 'C:\Program Files\Internet Explorer\IEXPLORE.EXE']
>>> webbrowser._browsers.items()
[('windows-default', [<class 'webbrowser.WindowsDefault'>, None]),
 ('c:\program files\internet explorer\iexplore.exe',[None, <webbrowser.BackgroundBrowser object at 0x000000000235E828>]),
 ('firefox', [None, <webbrowser.BackgroundBrowser object at 0x000000000235E780>]),
 ('c:\users\scott\appdata\local\google\chrome\application\chrome.exe', [None, <webbrowser.GenericBrowser object at 0x000000000235E8D0>])]
>>>

The webbrowser._tryorder gives the list of browsers tried. Registering chrome or adding a BROWSER env var or modifiying my path all would have gotten me the correct browser with a better error message.

Thanks for the help guys, I couldn't have solved this without your ideas.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...