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.
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
Just Set Your Browser:
import webbrowser
firefoxPath = "C:/Program Files/Mozilla Firefox/firefox.exe %s"
webbrowser.get(firefoxPath).open(firstUrl)
in VScode
Related
I am using VS code, Visual code extension for debugging (showing the state of the code, putting marks where to run before running line by line and always showing the state of the parameters etc..) and I am trying to think how I should begin to check what is wrong if my codes with python standard library called webbrowser works only when I am using my debugger extension. My different short python scripts function if I run them while using the debugger extension but not while running them without the debugging extension. I have an example code which worked fine earlier but don't work anymore.
The next code is an example which works if I have copied and address like "Tammiston kauppatie 10, 01510 Vantaa" (which is a Finnish street address to a climbing venue) and it is on my clipboard. This short script should take the address copied from the clipboard and open google maps site showing the address.
import webbrowser, sys, pyperclip
if len(sys.argv) >1:
adress = " ".join(sys.argv[1:])
else:
adress = pyperclip.paste()
webbrowser.open("https://www.google.com/maps/place/" + adress)
At the moment the code above doesn't open my current webbrowser and doesn't give any error messages either.
Any ideas how to make my code to work again? How to start checking what's wrong?
Other maybe useful info:
[tool.poetry.dependencies]
python = "3.9.13"
requests = "^2.28.1"
pyperclip = "^1.8.2"
VS Code:
Version: 1.70.1 (user setup)
OS: Windows
The debugger extension says that it supports the codes with Python language: >=3.7
Also -> using Chrome, up to date version.
After going through the basics again with the help of JialeDu, I found that I had actually used Poetry wrong. I thought I had used "Poetry Run Python " as mentioned in the documentation (https://python-poetry.org/docs/basic-usage/#using-poetry-run) correctly few times and on different occasions (without it working), and other ways to run the code, but while going everything a new, this was the step that had failed. The Debugger extension used virtual environment correctly from the start. After running the files in my folder in different ways I continued debugging elsewhere. Now, after working this out, I can continue the debugging easily and make my scripts work where and how they should. Thank you for the lesson.
In a way: For me, the real answer for this problem is to start the debugging from the basics again and not really trusting that the basics where all checked the first few times correctly and start again still "few times more with the basics". I had actually moved too fast with my debugging and was thinking too broadly about all the possible ways the code to fail.
I've attempted to work a bit more in WSL recently (I've got the most up-to-date version of WSL2 and the Windows 11 insider beta, both of which I updated today)
Everything works great! But plotly has been giving me issues. When I run it from within VSCode (making sure Python Interpreter is set to my correct environment), it spits out the following error:
tcgetpgrp failed: Not a tty
It then opens a tab in my default browser, but it just hangs until eventually failing to connect
Alternatively, if I run it directly from the WSL console (no VScode), it still gives the same error as above, but it DOES correctly open a window in my web browser. It also, for some reason, changes the font of the console?
I'm not 100% sure what the problem is here. I've used WSL for awhile, and never had any issues with displaying plots and things as needed (though, historically, I've used matplotlib... this is the first time I've tried using plotly, but I've used it without problem on native linux and native windows).
Has anyone else had this issue? Or one similar to it? Any idea on what might be wrong?
Figured this out after a bit of reading on other projects that use plotly. The fix is actually very simple, and just requires adding:
export BROWSER="/mnt/c/path/to/browser.exe"
To your ~/.bashrc file. For example, for me this was:
export BROWSER="/mnt/c/Program Files/Google/Chrome/Application/chrome.exe"
I installed python then configured it for vscode by following docs of vscode, but they said I have to right-click on the editor after seeing a run Python in terminal. And I have to click on it.
But it only works in Powershell and when I tried to use it in cmd, then they said ----
D:\Program\Applications\C++>& D:/Compilers-Interpreters/Python38-32/python.exe d:/Program/Applications/C++/app.py
& was unexpected at this time.
D:\Program\Applications\C++>
I am using the C++ folder bc I was also trying to set up C++ by following the tutorial in vscode doc. Now can someone tell me how to fix this & problem????
This is a problem with VSCode, On the VSCode insiders it has been solved. You can wait for the next stable release of VSCode.
You can take this as a workaround for now:
Take adventage of terminal.integrated.shell.windows instead of terminal.integrated.defaultProfile.windows for now.
You can refer to here for more details.
It seems that for some reason & is being added to the beginning of your command, which makes the command invalid.
Upon testing, I actually am running into the same issue, suggesting it could be an issue with VS Code and Python. I would recommend using the debugger to run your program, which still seems to work. Just press F5 and select Python File: Debug the currently active Python file. Another way to do this is to define the launch.json configuration to automatically launch upon pressing F5.
Context:
I am trying to move from PyCharm to VSCode + Python extension.
The autocompletion on VSCode does not as well as on PyCharm.
As I am new with VSCode, I am not sure if it is a configuration issue on my side, or a bug on VSCode Python extension side.
E.g., when using selenium, with the following code snippet:
from selenium import webdriver
browser = webdriver.Chrome()
elem = browser.find_element_by_id('toto')
elem.
PyCharm is able to give me the following suggestions:
But VSCode does not suggest anything:
Can you guys reproduce the issue on your side?
Is this something I can fix on my side? how?
If not, then I'll file an issue.
I am fairly new to programming in python. I installed anaconda and am running iPython (the Jupyter qtconsole) v.4.3.0 and python v.3.6 on a Mac. Currently, I am trying to import a module with functions located in my home directory.
I have looked at stackoverflow and python documentation and found that it could be done with:
%run "Users/myUser/python_functions.py"
or
import python_functions
However, when I try both of these approaches, I get prompted to overwrite the file that I am running or importing:
File `python_functions.py` exists. Overwrite (y/[N])?
This is changing the previous file and not getting the functions I want to be imported.
What may explain this, and what can I do to import my module?
this is wrong but leaving it up for shame
import on ubuntu (and I'm guessing many other unix-like OSs including Mac) is a utility that saves any visible window on an X server and outputs it as an image file. You can capture a single window, the entire screen, or any rectangular portion of the screen.
My guess if you are running the import command in your console, and it's about to take a screenshot and save it over an existing file - python_functions
Before you the use the python import command, start a python interpreter:
$ python
>>>import yourfile
edit: on re-reading your question, I'm not so sure about my guess anymore, but leaving it up until you tell me I'm wrong :)
Running Jupyter qtconsole as an interpreter is likely causing the problem in this scenario. Instead using a IDE or command line interpreter will resolve it .
Since anaconda was installed, trying it with the IDE Spyder executes the code just fine without the overwrite prompt. It works on others (e.g PyCharm, Rodeo, etc.) as well.