I am using Visual Studio Code on my Windows 10 PC. I want to change my default terminal from 'Windows PowerShell' to 'Python' (on Windows). Someone help me solve this issue.
Press Ctrl+Shift+P to bring up the command palette and run the Terminal: Select Default Profile command:
If Python does not appear here, add a profile for it manually in your JSON configuration. Press Ctrl+Shift+P and run the Preferences: Open User Settings (JSON) command:
Add the following lines in your JSON config file and save it:
"terminal.integrated.profiles.windows": {
"Python": {
"path": "python",
"args": []
}
},
Afterwards you should be able to select Python as your default terminal profile.
Related
There are at least 20 questions that are somewhat related to my question and have contributed to my knowledge of how to solve this problem, but none seem to cover the entire answer to my specific question. Also, it seems that the Microsoft documentation tells you how the parts function, but I have not found a comprehensive example of how to do all of this. I would like this question to cover it all if possible. I have a manual debug process which I will share below, but I want to further automate it with tasks and entries in the launch.json file so that I don't have to start anything manually. Would someone be willing to share their fully automated version of the exact steps in the automation with me?
This is the Configuration steps I followed to make Windows Visual Studio Code to work with the debugpy remote python
debugger. I have also added how to just use an interactive debug session using docker exec -it with pdb as well.
I have both detailed the files that were changed here and what the changes were so that you can understand how it
currently manually works.
Below I have two different ways of running the python debugger:
Run it from the pdb command line interactively from a terminal window:
Run it manually from the command line in a terminal window, but let Visual Studio Code connect to the debugpy program
and Docker container to do it interactively from Visual Studio Code.
The following procedures are adopted from the following 2 articles I found after reading the Visual Studio Code
documentation and after googling how to enable remote Docker Container Python debugging:
Article 1:
https://stackoverflow.com/questions/40233928/how-to-remote-debug-python-code-in-a-docker-container-with-vs-code#:~:text=In%20your%20Docker%20image%2C%20you%27ll%20need%20to%20make,something%20like%3A%20docker%20run%20-d%20-p%203000%3A3000%20my-image
Article 2:
https://code.visualstudio.com/docs/python/debugging#:~:text=Switch%20to%20the%20Run%20and%20Debug%20view%20%28Ctrl%2BShift%2BD%29%2C,which%20point%20you%20can%20use%20the%20debugger%20normally.
To allow the my.py code to use debugpy debugger used by Visual Studio, I added the following entries in the Docker file:
RUN /usr/bin/pip --no-cache-dir install -U debugpy
The procedure to run the pdb debugger manually and interactively from the Ubuntu 20.04 command line is as follows.
Only use this step or step 6 below but not both. This line runs the pdb debugger (not debugpy ) that Visual Studio uses.
At the Ubuntu 20.04 command line execute the following:
export USER=JOHN DIR=/data OUTPUTFILE=/data/output/output.txt
Here is how I run the python program using pdb passing in a call to the run_user module with 3 environment vars:
docker exec -it my_docker_container /usr/bin/python my.py run_user --my_user=$(USER) --my_dir=$(DIR) --my_file=$(OUTPUTFILE)
Now, to support doing debugpy using Visual Studio Code, I add the following 8888:3001 port to the
docker-compose.yml so that it looks as follows after the change:
webapp:
build: ./
image: my.webapp
container_name: my_docker_container
ports:
- 80:80
- 443:443
#----START OF docker-compose.yml changes
- 8888:3001
#----END OF docker-compose.yml changes
depends_on:
- database
Open the Ubuntu 20.04 command prompt
Procedure to run the debugpy remote module on port 3001 internally and 8888 on host machine so that the Visual Studio
debugger can be used. Note: you cannot run both the interactive pdb debugger of step 3 and this step together as it probably won't work and will hang up your machine for a reboot.
Here is how I run the python program manually using debugpy in Visual Studio
export USER="JOHN" DIR=/data OUTPUTFILE=/data/output/output.txt
docker exec -it my_docker_container /usr/bin/python -m debugpy --listen 0.0.0.0:3001 --wait-for-client my.py run_user --user=$(USER) --my_dir=$(DIR) --my_file=$(OUTPUTFILE)
I Add at least the following to the visual studio config in the ~/.vscode/launch.json as follows (.vscode/launch.json) in order to connect to the debugpy backend:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Debug Docker JMK",
"type": "python",
"request": "attach",
"port" : 8888, // This is the localhost port that will be open on your local machine to talk to the debugger.:
"host" : "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/opt/webapp"
}
],
"logToFile": true
}
]
}
After saving the above changes, to the .vscode/launch.json file, you should be able to press Control+Shift+D and then you will have the debugger entry at the top of you screen with a green right arrow. Alternatively, you could press F5 to bring up the debugger window.
If you are not using Visual Studio code and using the interactive pdb debugger,
instead, after you have ran step 3, the debugger will attach and be paused at the first line of code.
You can then use pdb commands and step with next until you get to the next line of code
or you will have to set an initial breakpoint using import pdb pdb.set_trace().
Do not use import pdb or pdb.set_trace() within a Visual Studio Code
debugging session but attach using Visual Studio Code as described in the other steps as the two are
somewhat incompatible with each other because they were developed at different times in Python's history.
Here is where my exact specific question is asked. Not sure why it was flagged by your stupid bot
What is my exact question to the community and how is it different from other questions?
I am not interested in doing the manual steps at step 3 to run either the pdb debugger, nor the debugpy (at step 6) and would like a fully working example of how to set up the tasks and launch.json entries to automatically run the manual steps at step3 and Step 6 within Visual Studio Code.
While I'm able to run scripts from Ubuntu terminal, the integrated terminal on VS Code doesn't run correctly when it comes to launching GUI applications. Consider these examples:
PIL Example
from PIL import Image
img = Image.open('image.png')
img.show()
Behavior on System Terminal: Launches default image viewer
Behavior on VS Code Integrated Terminal: A warning is printed (Gtk-WARNING cannot open display)
Browser / Plotly Example
import plotly.graph_objects as go
fig = go.Figure(
data=[go.Bar(y=[2, 1, 3])],
layout_title_text="A Figure Displayed with fig.show()"
)
fig.show()
Behavior on System Terminal: Launches default web browser
Behavior on VS Code Integrated Terminal: Nothing
Text Editor Example
git rebase -i origin/main
Behavior on System Terminal: Launches default text editor
Behavior on VS Code Integrated Terminal: Nothing
I have reported this bug here but I'm think it may not be a bug.
Probably the DISPLAY variable is not set in your VS code shell. Find out the value in your working system terminal:
echo $DISPLAY
Then set the value in VS Code via the terminal.integrated.env.<platform> setting. Press Ctrl+Shift+P and search for Preferences: Open Settings (JSON). Add the following entry to the settings file:
"terminal.integrated.env.linux": {
"DISPLAY": "<your-display-value>"
}
Then close and re-open VS Code's terminal. Afterwards, running echo $DISPLAY there should output the same value as in your system terminal. This should make GUI applications launchable.
When I click the play button to run my hello.py program in vscode, I get the following error in the terminal
PF1FEARB:~/tmp$ C:/Python37/python.exe c:/Users/xtn/hello.py
-bash: C:/Python37/python.exe: No such file or directory
Manually running the script in the terminal works
PF1FEARB:~/tmp$ python hello.py
Hello
My python.exe is installed here:
C:\Users\xtn\AppData\Local\bucache\cbdn10897X>where python
C:\Python37\python.exe
Vscode shows the following installed extension
Name: Python
Id: ms-python.python
Description: Linting, Debugging (multi-threaded, remote), Intellisense, Jupyter Notebooks, code formatting, refactoring, unit tests, snippets, and more.
Version: 2020.2.64397
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.python
How do I find out which PATH vscode uses to look for executables?
Did you configure the Python interpreter correctly within the vscode editor window itself? You'll have to configure certain things within the IDE itself to run Python code through the editor window. Following the guide for setting up Python in vscode (https://code.visualstudio.com/docs/python/python-tutorial), perhaps here are a few steps you missed:
1.
install the Python extension for VS Code from the Visual Studio Marketplace. For additional details on installing extensions, see Extension Marketplace. The Python extension is named Python and it's published by Microsoft.
2.
Select a Python interpreter:
Python is an interpreted language, and in order to run Python code and get Python IntelliSense, you must tell VS Code which interpreter to use.
From within VS Code, select a Python 3 interpreter by opening the Command Palette (⇧⌘P), start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too):
The command presents a list of available interpreters that VS Code can find automatically, including virtual environments. If you don't see the desired interpreter, see Configuring Python environments.
Selecting an interpreter sets the python.pythonPath value in your workspace settings to the path of the interpreter. To see the setting, select File > Preferences > Settings (Code > Preferences > Settings on macOS), then select the Workspace Settings tab.
Note: If you select an interpreter without a workspace folder open, VS Code sets python.pythonPath in your user settings instead, which sets the default interpreter for VS Code in general. The user setting makes sure you always have a default interpreter for Python projects. The workspace settings lets you override the user setting.
I was able to apply the same solution in this post. I changed to PowerShell and it works for me now. Command palette -> Terminal: Select Default Shell -> select PowerShell. Restart vscode.
I am writing a simple script in Python using VS Code. I have the python extension installed and I use a conda environment.
To run my script, I use Shift+Enter (Run Selection in Python Terminal). This works.
However, if I change something in the script and do the same, the change is not taken into account. I am forced to delete the terminal and relaunch again.
Is this normal? How do you guys go at it?
The "Run Selection/Line in Python Terminal" option basically opens a Python console and copies the selected lines to it (as if you typed it yourself). When you make changes to your code and repeat the process, it copies those codes to the same console session, so whatever was copied before, still exists and will be used for next runs. You have to make sure to always run the same set of lines or, as what you are doing now, restart the Python console.
Instead of the "Run Selection/Line in Python Terminal" option, I recommend creating a launch configuration for your script, and then using the debugger.
Let's say you have this workspace:
|- myscript.py
|- .vscode
Start by opening your Python file on the editor, then opening the command palette (Ctrl+Shift+P or CMD+Shift+P), then calling Debug: Open launch.json.
Select Python File for simple scripts. That will automatically create a launch.json file under the .vscode folder, with default launch configurations for Python files. Modify it as needed for running your script. For ex., I prefer setting internalConsole for the console.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "run-myscript",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/myscript.py",
"console": "internalConsole",
"args": [],
"env": {}
}
]
}
Then from the Debug panel, select the launch configuration from the dropdown (same name as the "name" you set in launch.json), then hit the Run button.
Check the output from the Debug Console:
One good thing about this setup is that you can set breakpoints in your code, and then go through your code line-by-line, which is quite useful for debugging.
For more info, see Python debug configurations in Visual Studio Code.
I installed package manager and a bunch of packages right after I installed SublimeText3. When I go to Tools -> Command Palette, its opens up the package manager. Also, it said before that its key binding is CTRL + \ but when I ran that, it did nothing. So I set a new key binding for it:
and when I run that key binding, it opens up the package manager. Thats kind of useful for me because CTRL + SHIFT + P does not open the package manager for me (hence why I added it to the user keymap file, it still doesn't work though). I can't actually find package manager in the key bindings file.
So somehow the command palette is tangled up with the package manager. Is there a file where all of the commands are listed and mapped? How would I go about fixing this?
EDIT: I forgot to mention, the package manager I'm talking about is Package Control:
PackageControl provides its list of commands from the command palette itself, so that might be the source of your confusion. In particular:
If you open the command palette (Shift+Ctrl+P by default under Windows/Linux), the package control commands can be found in it prefixed by the text "Package Control:", for example "Package Control: Install Package". Thus you can find the commands there by entering pc: in the command palette.
Package Control modifies the main menu by adding a Preferences > Package Control menu item which, when selected, opens up the command palette pre-populated with the filter that filters down to only see package control commands.
So in answer to your question, there is a file where the various commands are listed out. Commands are added to the command palette by way of .sublime-commands files. If you use PackageResourceViewer you can use it to open up the Default.sublime-commands file provided by the PackageControl project.
This is a simple JSON file that provides the captions and associated commands that make up PackageControl's additions to the command palette. If you wanted to bind a key to invoke a PackageControl action directly, such as installing a package, you can find it's entry in the file to determine the command.
The entry in the file for the install command is:
{
"caption": "Package Control: Install Package",
"command": "install_package"
},
So you could bind this to a key with something like:
{ "keys": ["ctrl+alt+shift+i"], "command": "install_package" }
If instead you wanted to bind a key to do what the Preferences > Package Control menu item does, opening the command palette and showing only the list of Packge Control commands, you can replicate what the menu command does by showing the overlay with the text pre-populated. To get at how that works, you can open up the Main.sublime-menu file provided by PackageControl (also using PackageResourceViewer) to see what command is doing that.
Such a binding would look like this:
{
"keys": ["ctrl+alt+shift+i"],
"command": "show_overlay",
"args": {"overlay": "command_palette", "text": "Package Control: "}
}
Note that this is the same as the default command for opening the command palette, only we are providing an extra option to specify what text the text entry field should be initially populated with.