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.
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.
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.
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
I've a Python package package_name which provides a command line application command-line-app-name as console_script:
setup.py:
setup(
...
entry_points={"console_scripts": ["command-line-app-name=package_name.cli:main"]},
...
)
The virtualenv is located in <project>/.venv and managed with pipenv. pipenv managed venvs should support VSCode debugging integration. I've created a debugger configuration launch.json file with setting the Python path to the venv (pythonPath):
{
// 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: command-line-app-name",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"program": "command-line-app-name",
"linux": {
"pythonPath": "${workspaceFolder}/.venv/bin/python",
"args": ["-r", "/home/florian/gitlab/package_name/data/Test_MRM.d"]
},
"windows": {
"pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe",
"args": ["-r", "D:\\MassHunter\\Data\\demo_0000.d"],
},
"console": "integratedTerminal"
}
]
}
The Windows and Linux specific venv python executable and command line arguments should not have an impact. If I run the debugger I get: FileNotFoundError: [Errno 2] No such file or directory: '/home/florian/gitlab/package-name/command-line-app-name'. It seems like I'm miss-interpreting the documentation somehow. I tried to find help w.r.t. vscode-python as well as debugpy without success. How can I debug a console script command line app (instead of a package module)?
console_scripts cannot be debugged out-of-the-box. The solution is to call the entry point function directly instead ("program": "${workspaceRoot}/package_name/cli.py",). This requires to add the if __name__ == '__main__': idiom in the corresponding module (here: cli.py). In my case the command line argument parser used is click. However the pseudo-code should be very similar for other command line parser libs.
package_name/cli.py:
#click.command()
#click.option(...)
def main(<args>, <kwargs>):
...
if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter
.vscode/launch.json:
{
// 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: command-line-app-name",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"program": "${workspaceRoot}/package_name/cli.py",
"linux": {
"pythonPath": "${workspaceFolder}/.venv/bin/python",
"args": ["-r", "/home/florian/gitlab/package_name/data/Test_MRM.d"]
},
"windows": {
"pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe",
"args": ["-r", "D:\\MassHunter\\Data\\demo_0000.d"],
},
"console": "integratedTerminal"
}
]
}
NOTE: The tool used to manage the venv makes a difference. This solution does work in case the venv is managed with pipenv. The solution does not work in case the venv is managed with poetry.
Here is a launch.json that worked for me to debug mkdocs plugins:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "mkdocs",
"args": ["serve"]
}
]
}
mkdocs provides the mkdocs entry point that accepts several arguments such as build and serve. This launch.json allowed me to set a breakpoint in a mkdocs plugin python file and stop at that breakpoing after running the mkdocs build/serve process.
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