FAQ
After reading several web pages and mailing list threads, I've learned
that the webbrowser module does not really support opening local
files, even if I use a file:// URL designator. In most cases,
webbrowser.open() will indeed open the default web browser, but with
Python 2.6 on my Fedora 10 system, it opens a text editor instead. On
Python 2.5, it opens the default web browser.

This is a problem because my Python script creates a local HTML file
and I want it displayed on the web browser.

So is there any way to force webbrowser.open() to always use an actual
web browser?

--
Timur Tabi
Linux kernel developer at Freescale

Search Discussions

  • Jonathan Temple at Jan 16, 2010 at 5:08 pm

    On Jan 15, 8:14?pm, Timur Tabi wrote:
    After reading several web pages and mailing list threads, I've learned
    that the webbrowser module does not really support opening local
    files, even if I use a file:// URL designator. ?In most cases,
    webbrowser.open() will indeed open the default web browser, but with
    Python 2.6 on my Fedora 10 system, it opens a text editor instead. ?On
    Python 2.5, it opens the default web browser.

    This is a problem because my Python script creates a local HTML file
    and I want it displayed on the web browser.

    So is there any way to force webbrowser.open() to always use an actual
    web browser?

    --
    Timur Tabi
    Linux kernel developer at Freescale
    Might not be useful, but trying open_new_tab() on...

    Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    [GCC 4.3.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    import webbrowser as wb
    x = wb.get()
    x.basename
    'gnome-open'

    When attempting to use open_new_tab(), I get:

    file:///home/jon/blahblah.html - opens in Firefox
    file:///home/jon/blahblah.txt - opens in gedit

    Jon.
  • Jon Clements at Jan 16, 2010 at 5:20 pm

    On Jan 16, 5:08?pm, Jonathan Temple wrote:
    On Jan 15, 8:14?pm, Timur Tabi wrote:


    After reading several web pages and mailing list threads, I've learned
    that the webbrowser module does not really support opening local
    files, even if I use a file:// URL designator. ?In most cases,
    webbrowser.open() will indeed open the default web browser, but with
    Python 2.6 on my Fedora 10 system, it opens a text editor instead. ?On
    Python 2.5, it opens the default web browser.
    This is a problem because my Python script creates a local HTML file
    and I want it displayed on the web browser.
    So is there any way to force webbrowser.open() to always use an actual
    web browser?
    --
    Timur Tabi
    Linux kernel developer at Freescale
    Might not be useful, but trying open_new_tab() on...

    Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    [GCC 4.3.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.>>> import webbrowser as wb
    x = wb.get()
    x.basename
    'gnome-open'

    When attempting to use open_new_tab(), I get:

    file:///home/jon/blahblah.html - opens in Firefox
    file:///home/jon/blahblah.txt ?- opens in gedit

    Jon.
    Err, I'd just like to mention that "Jonathan Temple" did not post that
    message: He was checking his Google Mail on my machine, and when I
    went to send my post, it got a little confused.

    Cheers,

    Jon.
  • Paul Boddie at Jan 16, 2010 at 9:43 pm

    On 15 Jan, 21:14, Timur Tabi wrote:
    After reading several web pages and mailing list threads, I've learned
    that the webbrowser module does not really support opening local
    files, even if I use a file:// URL designator. ?In most cases,
    webbrowser.open() will indeed open the default web browser, but with
    Python 2.6 on my Fedora 10 system, it opens a text editor instead. ?On
    Python 2.5, it opens the default web browser.
    The webbrowser module seems to have changed. For example, in the
    Python 2.5 standard library, it uses gconftool to query the GNOME
    registry and get the preferred browser, whereas in the Python 2.6
    standard library, it appears to use gnome-open instead (but only in a
    GNOME environment). For KDE, there's a KDE-specific usage of kfmclient
    in the 2.6 library. See here for more:

    http://svn.python.org/view/python/tags/r254/Lib/webbrowser.py?revisiong917&view=markup
    http://svn.python.org/view/python/tags/r264/Lib/webbrowser.py?revisionu707&view=markup
    This is a problem because my Python script creates a local HTML file
    and I want it displayed on the web browser.
    Generally, the desktop-specific tools should know that a browser is
    the appropriate application for an HTML file, and testing with both
    xdg-open, gnome-open and "kfmclient openURL" seems to open browsers on
    HTML files (using file:///...) for me (using KDE, Kubuntu 8.04). Of
    course, this depends on the settings in use on your desktop, but it
    should be noted that using "kfmclient exec" could have the effect you
    describe.
    So is there any way to force webbrowser.open() to always use an actual
    web browser?
    Not that I'm aware of. Sadly, standardisation of applications and
    services - having a command which can open a particular class of
    application (such as "e-mail reader", "Web browser") - seems to be
    absent from the free desktop arena, although I do recall there being a
    preferred applications dialogue in KDE, at least. Maybe this
    information is exposed somehow, and maybe I'll incorporate such stuff
    into the desktop package eventually:

    http://pypi.python.org/pypi/desktop

    Note that the desktop package concerns itself precisely with opening
    files in text editors if that's how the user has configured their
    desktop, whereas the webbrowser module should really only use a Web
    browser, obviously.

    Paul
  • Timur Tabi at Jan 18, 2010 at 9:00 pm

    On Sat, Jan 16, 2010 at 3:43 PM, Paul Boddie wrote:

    Generally, the desktop-specific tools should know that a browser is
    the appropriate application for an HTML file, and testing with both
    xdg-open, gnome-open and "kfmclient openURL" seems to open browsers on
    HTML files (using file:///...) for me (using KDE, Kubuntu 8.04). Of
    course, this depends on the settings in use on your desktop, but it
    should be noted that using "kfmclient exec" could have the effect you
    describe.
    I'm using Gnome, and I have HTML files associated with Firefox.
    However, my default web browser is Seamonkey, and when I do
    webbrowser.open('http://...'), it opens that URL in Seamonkey, not
    Firefox. So if there is some Gnome association between an .html file
    and a text editor, I don't know where it is defined.
    Not that I'm aware of. Sadly, standardisation of applications and
    services - having a command which can open a particular class of
    application (such as "e-mail reader", "Web browser") - seems to be
    absent from the free desktop arena, although I do recall there being a
    preferred applications dialogue in KDE, at least.
    I would be sympathetic to this problem if the API were called
    desktop.open(...). But it's called webbrowser.open(), so it has to be
    certain that a web browser is being at all times. IMHO, any other
    behavior is a bug.

    --
    Timur Tabi
    Linux kernel developer at Freescale
  • Mitchell L Model at Jan 27, 2010 at 6:29 pm
    On Jan 15, 2010, at 3:59 PM, Timur Tabi <timur at freescale.com>
    After reading several web pages and mailing list threads, I've learned
    that the webbrowser module does not really support opening local
    files, even if I use a file:// URL designator. In most cases,
    webbrowser.open() will indeed open the default web browser, but with
    Python 2.6 on my Fedora 10 system, it opens a text editor instead. On
    Python 2.5, it opens the default web browser.

    This is a problem because my Python script creates a local HTML file
    and I want it displayed on the web browser.

    So is there any way to force webbrowser.open() to always use an actual
    web browser?
    I had some discussions with the Python documentation writers that led
    to the following note being included in the Python 3.1 library
    documentation for webbrowser.open: "Note that on some platforms,
    trying to open a filename using this function, may work and start the
    operating system?s associated program. However, this is neither
    supported nor portable." The discussions suggested that this lack of
    support and portability was actually always the case and that the
    webbrowser module is simply not meant to handle file URLs. I had taken
    advantage of the accidental functionality to generate HTML reports and
    open them, as well as to open specific documentation pages from within
    a program.

    You can control which browser opens the URL by using webbrowser.get to
    obtain a controller for a particular browser, specified by its
    argument, then call the open method on the controller instead of the
    module.

    For opening files reliability and the ability to pick a particular
    program (browser or otherwise) to open it with you might have to
    resort to invoking a command line via subprocess.Popen.
  • Timur Tabi at Jan 27, 2010 at 8:31 pm

    On Wed, Jan 27, 2010 at 12:29 PM, Mitchell L Model wrote:

    I had some discussions with the Python documentation writers that led to the
    following note being included in the Python 3.1 library documentation for
    webbrowser.open: "Note that on some platforms, trying to open a filename
    using this function, may work and start the operating system?s associated
    program. However, this is neither supported nor portable."
    Then they should have renamed the API. I appreciate that they're
    finally documenting this, but I still think it's a bunch of baloney.
    You can control which browser opens the URL by using webbrowser.get to
    obtain a controller for a particular browser, specified by its argument,
    then call the open method on the controller instead of the module.
    How can I know which controller (application) the system will use when
    it opens an http URL? I depend on webbrowser.open('http') to choose
    the best web browser on the installed system. Does webbrowser.get()
    tell me which application that will be?
    For opening files reliability and the ability to pick a particular program
    (browser or otherwise) to open it with you might have to resort to invoking
    a command line via subprocess.Popen.
    But that only works if I know which application to open.

    --
    Timur Tabi
    Linux kernel developer at Freescale
  • Mitchell L Model at Jan 27, 2010 at 10:00 pm

    On Jan 27, 2010, at 3:31 PM, Timur Tabi wrote:

    On Wed, Jan 27, 2010 at 12:29 PM, Mitchell L Model
    wrote:
    I had some discussions with the Python documentation writers that
    led to the
    following note being included in the Python 3.1 library
    documentation for
    webbrowser.open: "Note that on some platforms, trying to open a
    filename
    using this function, may work and start the operating system?s
    associated
    program. However, this is neither supported nor portable."
    Then they should have renamed the API. I appreciate that they're
    finally documenting this, but I still think it's a bunch of baloney.
    I agree, but I am pretty sure that, based on the discussions I had
    with the Python
    documenters and developers, that there's no hope of winning this
    argument.
    I suppose that since a file: URL is not, strictly speaking, on the
    web, that it
    shouldn't be opened with a "web" browser. It's just that the "web"
    part of
    "web browser" became more or less obsolete a long time ago since there
    are so many more ways of using browsers and so many more things they can
    do then just browse the web. So if you interpret the name "webbrowser"
    to mean
    that it browses the web, as opposed to files, which means going
    through some
    kind of server-based protocol, the module does what it says. But I
    still like
    the idea of using it to open files, especially when I want the file to
    be opened
    by its associated application and not a browser.
    You can control which browser opens the URL by using webbrowser.get
    to
    obtain a controller for a particular browser, specified by its
    argument,
    then call the open method on the controller instead of the module.
    How can I know which controller (application) the system will use when
    it opens an http URL? I depend on webbrowser.open('http') to choose
    the best web browser on the installed system. Does webbrowser.get()
    tell me which application that will be?
    webbrowser.get() with no arguments gives you the default kind of
    browser controller, just as if you had used webbrowser.open()
    directly.
    For opening files reliability and the ability to pick a particular
    program
    (browser or otherwise) to open it with you might have to resort to
    invoking
    a command line via subprocess.Popen.
    But that only works if I know which application to open.
    Aha. You could use subprocess to specify the application from within
    your Python code,
    but not to indicate "the user's default browser", unless the platform
    has a command for that.
    On OS X, for instance, the command line:
    open file.html
    opens file.html with the application the user has associated with html
    files, whereas
    open -a safari file.html
    will open it with Safari even if the user has chosen Firefox for html
    files. There's
    stuff like this for Windows, I suppose, but hardly as convenient. And
    I think that
    Linux environments are all over the place on this, but I'm not sure.

    webbrowser.get() returns a control object of the default class for the
    user's environment --
    the one that means "use the default browser" so it won't help.
  • Paul Boddie at Jan 27, 2010 at 10:19 pm

    On 27 Jan, 23:00, Mitchell L Model wrote:
    I suppose that since a file: URL is not, strictly speaking, on the ?
    web, that it shouldn't be opened with a "web" browser.
    But anything with a URL is (or should be regarded as being) on the
    Web. It may not be anything more than a local resource and thus have
    no universal or common meaning - someone else may not be able to
    resolve the URL to a file at all, or it may resolve to a different
    file - but it's part of the Web as observed by one party.

    Paul

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 15, '10 at 8:14p
activeJan 27, '10 at 10:19p
posts9
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase