Search code examples
pythonpyinstaller

How to make my PyInstaller exe program open on the default browser


I'm using

pyinstaller.exe --onedir --noupx spec_an.py

and it's working fine and creating my .exe file. The file I'm turning into an exe is a flask project I'm working on. When I run it, it launches at localhost, but does not open any window. Therefore the user then has to go and open their window, type in localhost and port 5000, etc. This obviously is not ideal and would like for it to open the default browser when started.


Solution

  • You can use webbrowser library for doing this:
    Here's the example code:

    import webbrowser
    
    webbrowser.open("<url>")
    

    You can just add this script in the top of your python script. It will open default browser. And open that specific url provided in .open()

    Or you can directly open webbrowser from terminal as well. But I don't think it is convenient way for you but you can do this by:

    python -m webbrowser -t "<url>"