1

I am trying to open a webpage using Python, and my editor is VS Code.

This is the code:

import webbrowser

webbrowser.open('google.com')

This won't work while using VS Code, but it does work when I'm using IDLE.

What am I misunderstanding? What have I not done/done incorrectly? I have the Open in Browser extension by TechER, but even with this, I can't open a web browser page while using VS Code, and nothing I've found has been remotely helpful or even hints at what to do.

I'm clearly missing something in VS Code, but I don't have even the slightest clue as to what.

J. M. Arnold
  • 6,261
  • 3
  • 20
  • 38
  • 1
    What does "won't work" mean? Do you get an error message or does the script silently exit? – Lydia van Dyke Jan 24 '21 at 21:03
  • Works normally for me whether I do it with the interactive shell or run a script (via vscode terminal) with no extensions. How are you running it exactly? – Countour-Integral Jan 24 '21 at 21:12
  • What I mean by "won't work" is as I say in the question. I can't open a web browser. No errors, but no webpage. I placed a print('finished') the line following webbrowser.open('google.com') and my print statement is working. So it's not a problem with the code, it's a a problem with webbrowser not opening a webpage. This exact line of code works in IDLE, but not in VScode. – Kyle Christian Feb 06 '21 at 15:37
  • Please have a look at this question and see if the answer could help you: [Python webbrowser.open() to open Chrome browser](https://stackoverflow.com/questions/22445217/python-webbrowser-open-to-open-chrome-browser). – Molly Wang-MSFT Feb 08 '21 at 09:59
  • You *do not* `say in the question`: `I can't open a web browser`. – greybeard Jun 01 '21 at 15:01

2 Answers2

0

Can you check which interpreter is VSCode using because sometimes VSCode downloads the interpreter somewhere else. To change the environment of VSCode you can take a look at https://code.visualstudio.com/docs/python/environments

0

Just Set Your Browser:

import webbrowser

firefoxPath = "C:/Program Files/Mozilla Firefox/firefox.exe %s"

webbrowser.get(firefoxPath).open(firstUrl)

in VScode

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Fio
  • 1