I have a fairly large python/c++ program that runs as follow:
. set_env.sh -option A -option B
python run.py
The set_env.sh script modifies the PYTHONPATH and does all sort of export in order to point to the right c++ program.
When running these two commands in the terminal, that works just well, however, using the debugger breaks it all.
I try running ". set_env.sh -option A -option B" in preLaunchTask but it seems the debugger can't see the new PYTHONPATH.
How can I make the debugger run the the set_env and consider the new PYTHONPATH ?
I had a similar problem and could solve it by setting environment variables in the "env" field of the launch.JSON file. This shoudl be stored in a .vscode folder in your project root, here is the docs: https://code.visualstudio.com/docs/python/debugging
I discovered the "env" field in this snippet here: https://gist.github.com/slaveofcode/b4c18efc99c16029913cde3fd2dba3ce
Not sure how to configure this dynamically with the -option A -option B however. Hope this helps :)
Inspired by my current area of work - which involved Python.
This is what I do for Flask.
MS Link
https://code.visualstudio.com/docs/python/debugging
For any regular Python debugging
Evidence
import os
for key in os.environ.keys():
print(f"{key}={os.environ.get(key)}")
After doing a F5 in VS Code with a file containing the above snippet:
Update on Feb 18
After receiving a comment I think the OP wants to run a script before debugging the Fask application. The steps that worked for me are below.
Overview
I ended up with the following files:
Add a PowerShell file dosomething.ps1 (my custom script which I intend to launch before every debug session)
Add a task.json (launch dosomething.ps1)
Edit the existing launch.json by linking it with the task in task.json
The file dosomething.ps1
Write-Host "Inside do something"
Write-Host "The value of the environment variable TEMP is $env:TEMPcls"
Write-Host "The current path of the script is $PSScriptroot"
The file tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "my script task",
"type": "shell",
"windows": {
"command": "pwsh -f dosomething.ps1"
},
"problemMatcher": []
}
]
}
Linking the launch.json with task.json
After pressing F5
Related
Please don't flag as duplicate without reading. Seriously not able to find this information anywhere.
I work on projects that make use of "Make". Right now I have to type every make command in the integrated terminal like "make all", "make clean" etc. I'd like to have some keyboard shortcuts that do the work, similar to how default build tasks have one.
I tried command runner extensions but they too have very vague info on how to make these custom commands. Been fiddling around with settings and keybindings json file for few days now. I'm not a web developer so don't know much about working with json files to begin with and configure stuff observing the default templates.
All I find is the default template of
{
key : " ",
command : " "
}
Tried fiddling with this on keybindings.json file but my command is not found.
Any help on this would be much appreciated.
With the command workbench.action.terminal.sendSequence you can send text to the terminal
An example key binding
{
"key": "ctrl+f5", // or any other combo
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "make all\u000D" }
}
Be sure the terminal is at a directory where the command works, or add a cd command in front, you can use variables (credit to Mark for the correction).
If you define multiple tasks to do the stuff you can execute the task with a keybinding
{
"key": "shift+f5", // or any other combo
"command": "workbench.action.tasks.runTask",
"args": "Name_of_task"
}
In the task you can use variables.
Right now I have a few projects I'm running in VSCode, and it's quite tedious to run go test everytime I write new code, I'd rather see if I broke something right away. I know in Javascript I am able to run the tests everytime I save a file, and have the output sent to the terminal.
Right now I am using a "run on save" extension, and I have a config file that looks like:
{
"emeraldwalk.runonsave": {
"commands": [
{
"match": ".*",
"cmd": " go test"
}
]
}
}
But it outputs to the "output" section of VSCode, and I'd like it to output to my terminal.
So is there either:
A way to have this extension output to my terminal
A way to have "go test" run on a loop whenever I save?
Any thoughts would be appreciated.
Yes, this is supported by the Go extension.
Go to Settings => Go, and there is an explicit option for this: "Test on Save".
To see the test result, select the "Go Tests" in the output.
If your intent is to watch code and run tests in an automated manner, then checkout GoConvey. It is amazing. Else, you can use Trigger Task on Save VSCode extension for this.
I'm trying to set up a task for compiling my C++ code in Visual Studio Code. I can't get it to work... but the command it spits out works perfectly fine when I just open the Developer Command Prompt and paste it in.
I've managed to narrow down this problem to the correct environment variables not being set in the shell that VS Code is using (as evidenced by running echo %INCLUDE% just returning %INCLUDE%).
Now I don't exactly know how the Developer Command Prompt differs from a normal Powershell terminal as used by VS Code, so I wouldn't know exactly how to configure it (aside from running vcvarsall.bat), but even if I could, every time I open a new terminal in VS Code the environment variables have reset themselves again.
In essence, the solutions to this problem that I can see are:
Run vcvarsall.bat before every build task.
Unfortunately I'm not familiar enough to know how to execute multiple commands in a row using a tasks.json configuration file.
Configure the shell that VS Code uses to be like the Developer Command Prompt by default.
Unfortunately, I have no clue where to even start with this. I can easily set the shell used to either cmd or PowerShell, but not to the Developer Command Prompt, and neither can I find where to configure its environment variables, nor do I know what the full effects of vcvarsall.bat are so I know which variables to set.
If there's a simpler way of achieving what I'm after, I'd be very happy to hear it. Though regardless, what it comes down to is that I want to know how to configure VS Code in a way that allows me to compile my code from inside the IDE.
I found an answer after a bunch more poking around. My setup (in tasks.json) is now as follows:
"command": "&",
"args": [
"'D:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Auxiliary\\Build\\vcvars32.bat';",
"cl.exe",
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
]
with & you can execute a path that contains spaces. This leads to my vcvars32.bat (Location may be different for other people) which sets up the correct variables without having to enter them manually. ; lets you execute multiple commands one after the other in PowerShell.
The accepted answer did not work for me but was helpful in finding a solution that did work.
Using the task type "process" and then settting the command to the batch file called by the Developer Command Prompt launches the windows command prompt directly (instead of invoking it via powershell).
The first argument is then "&" which means that the compiler will be invoked within the developer command prompt context instead of just being passed to the batch file as an argument.
>{
"version": "2.0.0",
"tasks": [
{
"type": "process",
"label": "cl.exe build active file",
"command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Auxiliary\\Build\\vcvars32.bat",
"args": [
"&"
"cl.exe",
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
You can create a new task type within your tasks.json this way:
{
"version": "2.0.0",
"windows": {
"options": {
"shell": {
"executable": "cmd.exe",
"args": [
"/C",
// The path to VsDevCmd.bat depends on the version of Visual Studio you have installed.
"\"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/VsDevCmd.bat\"",
"&&"
]
}
}
},
"tasks": [
{
"type": "shell",
"label": "cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Don't forget to change type of task to "shell".
Note that the build will start with a couple of sec delay.
See here: https://code.visualstudio.com/docs/cpp/config-msvc#_run-vs-code-outside-the-developer-command-prompt
I looked at this link which talks about changing the default command shell in the integrated terminal of Visual Studio Code. I was able to change it to Git Bash using the below setting in the settings.json file. You can open settings.json file by pressing the Ctrl + , or from File → Preferences → Settings menu:
{
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
}
There is a + sign in the integrated terminal with the help of which you can have multiple terminals running parallelly as shown in the screenshot below:
I want to load a Git Bash shell in terminal # 1, Windows PowerShell in terminal # 2, and so on. What is the relevant configuration to achieve it?
No need to keep changing your default terminal setting. Install the Shell Launcher extension, configure it, and then ctrl-shift-t to select which terminal you want to open inside of VS Code.
As of June 17 '2018, things have become really smooth in Visual Studio (VS) Code when it comes to changing the integrated command shell. I'm enlisting all the options here:
Select a different command shell on the go - Name of the command shell is itself a command to switch the current command shell to the target command shell. For example, let's say my integrated command shell is currently showing Bash, and I want to switch to PowerShell. Then type powershell command and press Enter. The command shell will change to PowerShell. Similarly it works for all other types of command shells installed in VS Code.
More instances of command shell - Press Ctrl + Shift + `(back-tick). Every time you press the keyboard shortcut combination, a new instance of the command shell will get added.
Change the configuration of default command shell type - Press F1 in Visual Studio Code and type or select Terminal: Select Default Shell as shown in the snapshot below.
Once selected, then choose the default command shell of your choice to change the settings permanently:
In recent version (1.15.0) you can
change the settings, then run the terminal, you've set
then change the settings again and run the another type of terminal by pressing the + sign
and so on...
You can put this in your settings file, and uncomment which one you need.
// 64-bit cmd if available, otherwise 32-bit
//"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\cmd.exe"
// 64-bit PowerShell if available, otherwise 32-bit
//"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe"
// Git Bash
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
// Bash on Ubuntu (on Windows)
//"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"
Click on Arrow then click something like Select template profile
so then , will Open the search bar automatically, so... change by your choice
Terminal profiles is what you are looking for - https://code.visualstudio.com/docs/terminal/profiles. It didn't exist by the time the question was posted and the only solution by that time was the somewhat limited "terimnal.integrated.terminal.xxx" solution.
Example configuration:
"terminal.integrated.profiles.windows": {
"Cmd": {
"path": "C:\\Windows\\System32\\cmd.exe",
"icon": "terminal-cmd"
},
"GitBash": {
"path": "C:\\Program Files\\Git\\bin\\bash.exe", "icon": "terminal-bash",
"icon": "terminal-bash"
},
"PowerShell": {
"path": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
"icon": "terminal-powershell"
},
},
I'm using vscode in Windows 10 as my code editor, and want to make an easy way to launch it with the correct conda env to allow debugging.
Currently I am having to open a command prompt, then activate the conda env, then paste the shortcut to vscode into the prompt to execute. Like so:
cmd
activate env-name
"C:\Program Files (x86)\Microsoft VS Code\Code.exe"
I have tried creating a batch file to wrap these calls, but unfortunately once I call "source activate" to start the conda env, the batch commands after this are not executed as it is considered another instance.
Any tips? Other than writing a vscode extension to handle this (which I'm seriously tempted to do, but it's such a simple problem...)
You might want to run source activate env-name as a task in visual studio.
https://code.visualstudio.com/Docs/editor/tasks
tasks.json
{
"version": "0.1.0",
"command": "cmd",
"isShellCommand": true,
"suppressTaskName": true,
"args": [],
"tasks": [
{
"taskName": "development",
"args": ["source", "activate", "env-name"]
}
]
}
The best option I found is to set the python.venvPath parameter in vscode settings to your anaconda envs folder.
"python.venvPath": "/Users/[...]/Anaconda3/envs"
Then if you bring up the command palette (ctl + shift + P on windows/linux, cmd + shift + P on mac) and type Python: Select Workspace Interpreter all your envs will show up and you can select which env to use.
The python extension will also need to be installed for the Select Workspace Interpreter option.
Note: The Select Workspace Interpreter takes around 10 seconds to come up on my computer using the current version of VSCode. My answer was originally posted here.
Using Conda 4.7.5, I was able to change the Target under the VsCode taskbar shortcut Properties from:
"C:\Users\Paul.siersma\AppData\Local\Programs\Microsoft VS Code\Code.exe"
To:
C:\Users\Paul.siersma\Anaconda3\_conda.exe run -p C:\Users\Paul.siersma\Anaconda3 "C:\Users\Paul.siersma\AppData\Local\Programs\Microsoft VS Code\Code.exe"
This uses the run command (marked experimental) and starts VSCode using the base Conda environment. You could specify another environment by changing the -p flag to the environment location eg -p [..]\Anaconda3\envs\myenv