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.
Related
I am following a tutorial from visual studio code on creating a docker web app to run my python script in.
for your information it is visible in the following link:
https://code.visualstudio.com/docs/containers/quickstart-python
I followed the django tutorial before this on installing it correctly and tested it so that it works. After allowing vs code to create all my necessary files, i tried to run it via my launch.json file.
{
// 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: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"runserver"
],
"django": true,
"justMyCode": true
},
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
]
}
when i try this, i instantly get returned:
> docker build --rm --pull -f "H:\Dockerfile" --label "com.microsoft.created-by=visual-studio-code" -t "h:latest" "H:\" <
unable to prepare context: path "H:\"" not found
The terminal process failed to launch (exit code: 1).
I installed everything on my H drive, and after this error I added the H to my PATH by adding H:\ to it. But unfortunately the error persists.
does anyone have a clue on how I could solve this?
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
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 read many similar questions but I couldn't find any answer for me. I also went through the documentation: here and here.
I'm running an Ubuntu container on Windows and I read about the limitation in Networking.
I ran the container with -p 3000:3000
The error => connect ETIMEDOUT
This is my launch.json
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 3000,
"host": "172.17.0.2",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/somefolder/somefolder",
"remoteRoot": "/rootfolder/"
}
]
},
This is my tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "test:latest",
"dockerfile": "${workspaceFolder}/somefolder/somefolder",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"python": {
"module": "C:\\...\\somefolder"
}
}
]
}
You can try using the remote extensions as it should handle all of this transparently.
This is the extension for Visual Studio Code to remote debug in containers. You can attach a python script to a running container and it will import all the needed dependencies.
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