Skip to content

run and launch

These two functions can be imported from typer directly:

from typer import launch, run

typer.run

run(function)

This function converts a given function to a CLI application with Typer() and executes it.

Example

import typer

def main(name: str):
    print(f"Hello {name}")

if __name__ == "__main__":
    typer.run(main)
PARAMETER DESCRIPTION
function

The function that should power this CLI application.

TYPE: Callable[..., Any]

Source code in typer/main.py
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
def run(
    function: Annotated[
        Callable[..., Any],
        Doc(
            """
            The function that should power this CLI application.
            """
        ),
    ],
) -> None:
    """
    This function converts a given function to a CLI application with `Typer()` and executes it.

    ## Example

    ```python
    import typer

    def main(name: str):
        print(f"Hello {name}")

    if __name__ == "__main__":
        typer.run(main)
    ```
    """
    app = Typer(add_completion=False)
    app.command()(function)
    app()

typer.launch

launch(url, wait=False, locate=False)

This function launches the given URL (or filename) in the default viewer application for this file type. If this is an executable, it might launch the executable in a new session. The return value is the exit code of the launched application. Usually, 0 indicates success.

PARAMETER DESCRIPTION
url

URL or filename of the thing to launch.

TYPE: str

wait

Wait for the program to exit before returning. This only works if the launched program blocks. In particular, xdg-open on Linux does not block.

TYPE: bool DEFAULT: False

locate

If this is set to True, then instead of launching the application associated with the URL, it will attempt to launch a file manager with the file located. This might have weird effects if the URL does not point to the filesystem.

TYPE: bool DEFAULT: False

This function handles url in different operating systems separately
  • On macOS (Darwin), it uses the open command.
  • On Linux and BSD, it uses xdg-open if available.
  • On Windows (and other OSes), it uses the standard webbrowser module.

The function avoids, when possible, using the webbrowser module on Linux and macOS to prevent spammy terminal messages from some browsers (e.g., Chrome).

Examples

    import typer

    typer.launch("https://typer.tiangolo.com/")
    import typer

    typer.launch("/my/downloaded/file", locate=True)
Source code in typer/main.py
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
def launch(
    url: Annotated[
        str,
        Doc(
            """
            URL or filename of the thing to launch.
            """
        ),
    ],
    wait: Annotated[
        bool,
        Doc(
            """
            Wait for the program to exit before returning. This only works if the launched program blocks.
            In particular, `xdg-open` on Linux does not block.
            """
        ),
    ] = False,
    locate: Annotated[
        bool,
        Doc(
            """
            If this is set to `True`, then instead of launching the application associated with the URL, it will attempt to
            launch a file manager with the file located. This might have weird effects if the URL does not point to the filesystem.
            """
        ),
    ] = False,
) -> int:
    """
    This function launches the given URL (or filename) in the default
    viewer application for this file type.  If this is an executable, it
    might launch the executable in a new session.  The return value is
    the exit code of the launched application.  Usually, `0` indicates
    success.

    This function handles url in different operating systems separately:
     - On macOS (Darwin), it uses the `open` command.
     - On Linux and BSD, it uses `xdg-open` if available.
     - On Windows (and other OSes), it uses the standard webbrowser module.

    The function avoids, when possible, using the webbrowser module on Linux and macOS
    to prevent spammy terminal messages from some browsers (e.g., Chrome).

    ## Examples
    ```python
        import typer

        typer.launch("https://typer.tiangolo.com/")
    ```

    ```python
        import typer

        typer.launch("/my/downloaded/file", locate=True)
    ```
    """

    if url.startswith("http://") or url.startswith("https://"):
        if _is_macos():
            return subprocess.Popen(
                ["open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
            ).wait()

        has_xdg_open = _is_linux_or_bsd() and shutil.which("xdg-open") is not None

        if has_xdg_open:
            return subprocess.Popen(
                ["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
            ).wait()

        import webbrowser

        webbrowser.open(url)

        return 0

    else:
        return click.launch(url)