In Visual Studio Code I changed the Default Terminal to Command Prompt.
When I want to debug my Azure Function the following command is executed:
Executing task: .venv\Scripts\activate ; func host start <
However, I think it should put the cmd separator "&&" instead of ";".
Otherwise the func host start will not execute.
Executing task: .venv\Scripts\activate && func host start <
As far as I know it should auto-detect this.
Is there a way to manually change this?
My settings.json:
{
"terminal.integrated.automationShell.windows": "C:\\windows\\System32\\cmd.exe",
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
"Git Bash": {
"source": "Git Bash"
},
"C:\\windows\\System32\\cmd.exe": {
"path": "C:\\windows\\System32\\cmd.exe",
"args": [],
"icon": "terminal-cmd"
},
"JavaScript Debug Terminal": {
"extensionIdentifier": "ms-vscode.js-debug",
"icon": "debug",
"id": "extension.js-debug.debugTerminal",
"title": "JavaScript Debug Terminal"
}
}
}
Thanks in advance!
#########
SOLUTION:
I was able the change the separator by editing the tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "cmd host start",
"type": "shell",
"dependsOn": "pip install (functions)",
"windows": {
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\activate && func host start"
},
"isBackground": true,
"problemMatcher": "$func-python-watch",
},
{
"label": "pip install (functions)",
"type": "shell",
"osx": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": []
}
]
}
Afterwards in the launch.json, I set the preLaunchTask to "cmd host start".
Thank you ejizba. Posting your suggestion as an answer to help other community members.
"The debug configuration is specified in your tasks.json and launch.json files in the .vscode folder. The 'windows' 'command' properties default to a PowerShell separator (;), but you're welcome to switch it to a cmd separator (I believe that would just be &&) .
however, because I think we can potentially refactor the tasks.json so that it doesn't use terminal-specific separators and prevent similar issues in the future ."
For more information please refer this SO THREAD
Related
Try to debug the Python code in Container using VS Code Remote Container Extension.
After the docker build task, I got a "Debug Adapter Executable not provide" error. Where could be wrong?
launch.json
{
"configurations": [
{
"name": "Docker: Python - General",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "general"
}
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "py:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"python": {
"file": "app.py"
}
}
]
}
Looks like it is resolved if you define a local python interpreter even if you want to use docker. Open a .py file and define it with the button right bottom.
I also encountered this problem. After trying a fresh install of the VS Code Docker extension, to no avail, I noticed the actually command being triggered by the "docker-run: debug" task in the tasks.json:
docker run -dt -P --name "loadtiktokcampaigns-dev" --label
"com.microsoft.created-by=visual-studio-code" -v
"/Users/tnightengale/.vscode/extensions/ms-python.python-
2022.12.0/pythonFiles/lib/python/debugpy:/debugpy:ro,z"
--entrypoint "python3" "loadtiktokcampaigns:latest"
The key here is the -v command that is attempting to mount the /debugpy files into the container from the VS Code Python extension files.
For some reason, those files were not actually located at that path on my Mac. Deleting the ms-python.python-2022.12.0/ folder and reinstalling the VS Code Python extension solved the issue for me.
Question
How do I get a vscode task's command to run in a python virtual environment/shell created by another task?
Example
I'm trying to run a couple of tasks in sequence using vscode.
The first task will run the command pipenv shell.
The second task needs to be run in the virtual environment created by the first task.
The second task will only run if I exit the virtual environment created by the first task manually by running exit in the created shell.
Presumably because it is waiting for the first task to complete - but the first task is only complete when the process ends (when I run exit).
tasks.json
{
"tasks": [
{
"label": "first task",
"type": "shell",
"command": "pipenv shell",
"presentation": {
"panel": "shared",
},
},
{
"label": "second task",
"type": "shell",
"command": "<some_command>",
"presentation": {
"panel": "shared",
},
},
{
"label": "combine tasks",
"type": "shell",
"command": "echo hello",
"dependsOrder": "sequence",
"dependsOn": ["first task", "second task"],
},
],
}
The first task to do is activate the virtual environment for the correct terminal you use (Powershell, cmd, bash, ....)
Then run the additional terminal tasks sequentially.
I used Powershell and Python venv for virtual environment
I used these tasks (no terminal was active) and they run in the terminal variant with the > Executing task, Terminal will be reused by tasks texts.
{
"label": "Activate python venv",
"type": "shell",
"command": "c:/Project/Python/.venv/Scripts/Activate.ps1",
"problemMatcher": []
},
{
"label": "pip list",
"type": "shell",
"command": "pip list",
"problemMatcher": []
},
{
"label": "combine Python",
"type": "shell",
"dependsOrder": "sequence",
"dependsOn": ["Activate python venv", "pip list"],
"problemMatcher": []
}
It will list all the packages installed in the virtual environment.
I'm trying to debug my app in a docker container. The app is written in paython 2.7 but VS Code tries to debug it with python 3. Therfore, it cannot resolve packages and throws execptions.
Any idea?
My configurataions:
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"dockerRun": {
"containerName": "account",
"image": "account:latest",
"env": {},
"volumes": [
{
"containerPath": "/code",
"localPath": "${workspaceFolder}"
}
],
"ports": [
{
"containerPort": 8086,
"hostPort": 8086
},
{
"containerPort": 8085,
"hostPort": 8085
}
]
},
"python": {
"args": ["--config=/code/config/account.conf.localstage"],
"file": "skoobe-accountd"
}
},
{
"label": "docker-build",
"type": "docker-build",
"dockerBuild": {
"context": "${workspaceFolder}",
"dockerfile": "${workspaceFolder}/Dockerfile.arm.dev",
"tag": "account:latest"
}
}
]
}
launch.json
{
"configurations": [
{
"name": "Debug Account",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/code"
}
],
"projectType": "general"
}
}
]
}
settings.json
{
"python.linting.pylintEnabled": true,
"python.pythonPath": "/usr/bin/python"
}
Dockerfile
FROM python:2.7.18
ENV AWS_ACCESS_KEY_ID="xxx"
ENV AWS_SECRET_ACCESS_KEY="xxx"
ENV AWS_DEFAULT_REGION="xxx"
ENV SQS_QUEUE="xxx"
ENV SQS_CHANGE_QUEUE="xxx"
# RUN mkdir -p /root/.ssh && \
# chmod 0700 /root/.ssh
# RUN apt-get update && \
# apt-get install openssh-server -y
WORKDIR /code/
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
WORKDIR tools/
RUN python setup.py install
WORKDIR /code/
# RUN pip install -r requirements.txt
CMD ["python", "accountd", "--config=/code/config/account.conf.localstage"]
I found a solution after some investigation:
I modfied lunch.json to the following:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/code"
}
]
}
]
}
Then I added the following lines into the entypoint Python file:
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 5678))
After running the docker container, from VS code IDE, I could run in debug mode.
Environment
I have a Python quickstart in VS Code as part of a multi-root workspace. I have manually added 3 files; tasks.json, settings.json and launch.json.
Unexpected behaviour
When i debug the project, the main script fails because of missing modules. This is because on start-up the modules listed in requirements.txt are not being installed.
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "pipInstall",
"type": "shell",
"osx": {
"command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:python.pythonPath}\\Scripts\\python -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": [],
"options": {
"cwd": "${workspaceFolder}"
}
}
]
}
Launch.json
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\batch-python-quickstart\\src\\python_quickstart_client.py",
"console": "integratedTerminal"
}
]
}
Settings.json
{
"python.pythonPath": "C:\\Python Virtual Environments\\myenvid\\.venv\\Scripts\\python.exe"
}
Why the modules were not installed
The modules specified in the requirement.txt file were not being installed because there was no reference to the tasks.json in the launch.json. To execute the tasks before executing the python app requires "preLaunchTask": "pipInstall" to reference the task named pipInstall in tasks.json.
Code after changes
Note: I also fixed some incorrect paths and moved my virtual environments to a new directory outside my projects.
Settings.json
{
"python.pythonPath": "C:\\thepath\\.venv\\Scripts\\python.exe"
}
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\batch-python-quickstart\\src\\python_quickstart_client.py",
"console": "integratedTerminal",
"preLaunchTask": "pipInstall"
}
]
}
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "pipInstall",
"type": "shell",
"osx": {
"command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:python.pythonPath} -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:python.pythonPath}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": [],
"options": {
"cwd": "${workspaceFolder}\\batch-python-quickstart\\src"
}
}
]
}
Furthermore
It is possible to cascade tasks by adding the same dependency property inside a task; "dependsOn": "othertasklabel". Ideal for a set of build steps.
I have a python virtual environment declared in my workspace settings, everything is fine with it.
Now I have a build task that calls a make target which in turn calls a pip package. When I run it, it doesn't use my venv even though it is the selected interpreter.
I've tried to add a activate venv/... before calling my make command but I get a Permission denied error in the embedded terminal.
How can I use a virtual environment and tasks at the same time?
It might be a little late to answer your question but the trick is to set the command field to point to the virtual environment's python executable instead of the default python. If you set up your .vscode's settings.json correctly you should have something like this in your file:
{
"python.pythonPath": "env\\Scripts\\python.exe",
// other settings ...
}
Having this config in your workspace, you can then create a custom task of type process using ${config:python.pythonPath} as it's command field.
You can read all about it in this section of VSCode's docs.
This example creates a Django python manage.py migrate task:
{
"version": "2.0.0",
"tasks": [
{
"label": "Migrate",
"type": "process",
"command": "${config:python.pythonPath}",
"args": [
"${workspaceFolder}/src/manage.py",
"migrate"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
},
"problemMatcher": []
}
]
}
I tested with the following tasks.json and it works:
{
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"command": "source /home/me/.pyenvs/myenv/bin/activate; make"
}
]
}
First activate virtual environment and then executes make.
I had quite a lot of trouble with this recently myself. I wanted to update package requirements before running code, but I didn't want to implement a complex or maintenance-heavy solution. The following works well for me across Windows, OSX, and Linux environments.
{
"version": "2.0.0",
"tasks": [
{
"label": "pipInstall",
"type": "shell",
"command": "${command:python.interpreterPath}",
"args": ["-m", "pip", "install", "-r", "requirements.txt"]
}
]
}
I believe ${command:python.interpreterPath} points to whatever interpreter path one selects while setting up the environment.
You can modify the shell in tasks.json in a way that works for bash and cmd shells. This worked for me and it correctly prints the full path to the python executable in the virutal environment.
{
"version": "2.0.0",
"linux": {
"options": {
"shell": {
"executable": "bash",
"args": [
"--init-file",
"env/bin/activate",
"-ci",
]
}
},
},
"windows": {
"options": {
"shell": {
"executable": "cmd.exe",
"args": [
"/C",
"env\\Scripts\\activate.bat",
"&"
]
}
}
},
"tasks": [
{
"label": "test shell with virtualenv",
"command": "which python", // where on Windows for testing
"type": "shell",
"group": "build",
"problemMatcher": []
}
]
}
I'm late to the party, but this alternative might be useful. If you use pipenv in stead of standard venv, you can use pipenv run. It will activate the virtualenv before running the process. For example, this works for building sphinx:
{
"version": "2.0.0",
"tasks": [
{
"label": "build html",
"type": "process",
"command": "pipenv",
"args": [
"run",
"sphinx-build",
"-b",
"html",
"${workspaceFolder}",
"${workspaceFolder}/_build/html"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
}
}
]
}
For people who maybe using Poetry a solution is to run
poetry shell
before running VSCode but a better solution is to do the following. This has been mentioned in an earlier answer but not specifically for poetry.
Use Poetry for Python Dependency