VScode adding tick to dockerRun command - python

I'm trying to get the below to run a docker container command via VScode.
Within tasks.json I have:
{
"label": "docker-run: debug",
"type": "docker-run",
"dependsOn": [
"docker-build"
],
"python": {
"module": "simply.py"
},
"dockerRun": {
"command": "python3 simply.py",
}
},
Which I would expect to be run as
docker container run [unrelated arguments] python3 simply.py
Where python3 simply.py is the command I want to run
Instead I am getting
docker container run [unrelated arguments] python3` simply.py
I'm not sure where VScode is generating this tick after python3 but the result is my code doesn't run.
An example of dockerRun with python3 within command might help indicate what I'm doing wrong.
Also running docker container run [unrelated arguments] python3 simply.py in the terminal works fine.
Thanks
extra:
Not sure if relevant but my launch.json is:
{
"name": "Docker: DEV",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
},
},

This fixed itself upon closing and opening VScode

Related

Use debugger in Visual Studio Code with Docker

I am working on a project where we use Docker to develop an application. Currently I'm running my programmes by typing docker-compose run --rm app in the console.
Then I can run my scrip like this root#ce4d1325fb94:/home/app# python3 example.py. But I would like to use my debugger. Normally I can use it be pressing F5.
I have found Debug containerized apps and Debug Python within a container.
But id does not work.
I get the error Could not find the task 'docker-run: debug' or ConfigError: The project 'Projekte_635c102' is not a valid java project.
What am I doing wrong? Note: I have no clue what Docker exactly is. I was told to use it.
I've done everything exactly as #MingJie-MSFT said. I’m still getting the same Could not find the task 'docker-run: debug' error. I can't create an example since I have no clue what I'm doing. I never worked with Docker or got an explanation on how to use it. I was just told to use the existing code. I can give you the Dockerfile if that is of any help:
# syntax=docker/dockerfile:1
FROM ubuntu:latest
WORKDIR /home/app
# Pass 8 and 63 to tzdata to set up the default time zone
RUN apt-get update && echo 8 63 | apt-get install -y tzdata
RUN apt-get install -y python3-pip libgdal-dev gdal-bin default-jre && pip3 install -r requirements.txt
I also tried to just type docker-run: debug in my console while being in the container.
root#cd70584fb560:/home/app# docker-run debug
and I've got
bash: docker-run: command not found
I've tried to include a "tasks" in my launch.json file. But again no luck. I thought maybe it's trying to find the "preLaunchTask" specified in the launch.json file before. Again I am absolutely clueless here. How is this launch.json file supposed to work? What are those commands supposed to do? I only know a bit of Python and barely anything about using command-line commands.
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
],
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"python": {
"args": ["runserver", "0.0.0.0:8000", "--nothreading", "--noreload"],
"file": "manage.py" // how to I change this to current file?
}
}
]
}
Read the documentation about debug in Docker.
ConfigError: The project 'Projekte_635c102' is not a valid java project.
It identifies your project as a Java project. It seems that your launch.json file has an error. Check your launch.json file. It should look like this:
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
]
}

How to use Mamba Miniforge CLI in VSCode with Windows

I am trying to debug some Python code that must run in a Mamba environment. In order to run the code (but not debug), I can open the Miniforge Prompt command line application, activate my enviroment (mamba activate my_env), and then run my python file (python my_file.py). Running this code is producing an error that I would like to trace back using the Visual Studio Code debugging interface. I am having a problem trying to get this to run in Visual Studio Code, because it cannot seem to run the Miniforge Prompt command line. I am also running on Windows 10.
The default terminal options (for Windows) in VSCode are Powershell and CMD (and Git Bash), which both work fine, however, when I added another terminal method for Miniforge (via the settings.json), it doesn't seem to be working properly.
Here is my settings.json file:
{
...,
"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"
},
"MambaPython": {
"path": [
"${env:windir}\\System32\\cmd.exe"
],
"args": ["\"/K\"", "C:\\ProgramData\\mambaforge\\Scripts\\activate.bat", "C:\\ProgramData\\mambaforge"],
"icon": "terminal-cmd"
}
},
"terminal.integrated.defaultProfile.windows": "MambaPython",
}
I also modified the launch.json to activate the mamba environment once running in the miniforge CLI. Here is 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: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
},
{
"name": "Python: ProjectEnv",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"preLaunchTask": "ProjectEnv_activate",
"args": ["--kwarg_one=Something", "--kwarg_two"],
}
]
}
also, here is the tasks.json file that actually activates the environment:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [{
"label": "ProjectEnv_activate",
"command": "mamba activate ProjectEnv",
"type": "shell"
}]
}
When I execute any code (in run or in debug) in VSCode, it appears to just run with the standard CMD terminal, not in a Mamba environment as specified. If anyone knows how to get this to work, or any way to activate a Mamba environment when debugging python in VSCode, any help would be much appreciated!
I came across this question trying to configure my VS Code terminal to play nice with miniforge. I got it working correctly after just fiddling with the args syntax a bit. I'm using conda, not mamba, but I don't think that should matter for activating the environment.
"Command Prompt": {
"path": [
"${env:windir}\\System32\\cmd.exe"
],
"args": ["/K", "C:\\Users\\johndoe\\AppData\\Local\\miniforge3\\Scripts\\activate.bat","C:\\Users\\johndoe\\AppData\\Local\\miniforge3\\envs\\base" ],
"icon": "terminal-cmd"
},
There is a vscode extension
https://github.com/mamba-org/vscode-micromamba
With this extension one can create mamba environments based on environment.yml file.
Next to mamba environment a .env file is generated. If you use python vscode extension you could configure it to use the .env file. When using terminal window the environment is activated automatically.
I had no luck with the micromamba extension.
I'm using micromamba, but the workaround should be the same.
Try set this alias in your powershell $profile:
Set-Alias conda mamba
Now the env is also activated, when the terminal starts in vs code.

How to run python module command from VSCode tasks

I am trying to run my alembic migration which is in a subdirectory so I am writing VSCode tasks for it but when I run it I get command not found error:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Alembic autogenerated revision",
"type": "shell",
"command": "alembic revision --autogenerate -m init",
"options": {
"cwd": "${workspaceFolder}/my_sub_directory"
}
}
]
}
How can I run it under my virtual environment?
Activating the virtual environment, then installing alembic, you can run alembic revision directly, that's because it's in the virtual environment and able to call the module.
So, when executing alembic command in task, you also need to activate env first.
Copy the activation command and paste it before alembic and separated by ; , the task should be ran successfully:
Unfortunately, I've not been able to find a solution that doesn't require hardcoding the virtualenv path.
I believe an extension would be able to solve this, possibly by adding support for python in the tasks's type field.
A workaround
Use launch configurations instead of tasks for running python scripts. It is a bit weird, but it works.
Here's your code adapted to launch.json schema:
{
"version": "0.2.0",
"configurations": [
{
"name": "Alembic autogenerated revision",
"type": "python",
"request": "launch",
"module": "alembic",
"args": ["revision", "--autogenerate", "-m", "init"],
"cwd": "${workspaceFolder}/my_sub_directory"
}
]
}
Also, consider using input variables for specifying revision messages and other variable things.
Thanks for your responses. To resolve this we can use the selected interpreter path and run the module.
{
"label": "Alembic autogenerated revision",
"type": "shell",
"command": "${command:python.interpreterPath} -m alembic revision --autogenerate -m init",
"options": {
"cwd": "${workspaceFolder}/ts_tools"
}
},
Reference: https://github.com/microsoft/vscode-python/issues/18728
Regards.

How to debug a python function app using a virtual envrionment

I'm trying to do a basic thing: debug a python function app using a virtual environment.
The Visual Studio code documentation does not indicate how to do and I cannot figure it out myself: https://code.visualstudio.com/docs/python/debugging
I run the app from the terminal with the following command:
.venv36\scripts\activate
func start
The app is run from my virtual environment and everything is OK.
To debug, the debugger uses the launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"windows": {
"pythonPath": ".venv36\\Scripts\\python.exe"
},
"preLaunchTask": "func: host start"
}
]
}
Reading the https://code.visualstudio.com/docs/python/environments, I added the following settings to the settings.json:
{
...
"python.pythonPath": ".venv36\\Scripts\\python.exe",
"python.pipenvPath": ".venv36\\Scripts\\pip.exe",
"python.venvPath": ".venv36",
"python.terminal.activateEnvironment": true,
The tasks.json is:
{
"version": "2.0.0",
"tasks": [
{
"type": "func",
"command": "host start",
"problemMatcher": "$func-watch",
"isBackground": true,
"options": {
"cwd": "${workspaceFolder}/AlarmScoringFuncApp"
}
}
]
}
When I start debugging, the app is started but not in the right environment: it is using the system libraries instead of the ones installed for my project in the requirements.txt.
My app crash when it tries to use a library that is not installed in the system environment.

VS Code + Flask: ValueError: sourcecode string cannot contain null bytes

I've just started using VS Code with the Python plugin. I've set up a venv, launched code within that venv, installed all my necessary modules and updated my launch.json to launch flask apps as follows:
{
"name": "Python: Flask (0.11.x or later)",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceFolder}/env/Scripts/flask.exe",
"cwd": "${workspaceFolder}",
"env": {
"FLASK_APP": "${workspaceFolder}/main.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
},
However when I launch the debugger for Flask in VS Code the following appears in my debug console:
ValueError: source code string cannot contain null bytes
However, if I just launch the app from the commandline doing:
set FLASK_APP=main.py
python -m flask
it works just fine. I'm sure it's something stupid I've done but I can't figure out what that is.
Note also that pythonPath points to my venv python installation.
I am seeing the ValueError as well, and haven't found a proper solution for this, but have a workaround, using the "module": "flask":
{
"name": "Python: Flask (0.11.x or later)",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
//"program": "${workspaceFolder}/env/Scripts/flask.exe",
"module": "flask",
"cwd": "${workspaceFolder}",
"env": {
"FLASK_APP": "${workspaceFolder}/main.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
}
It does, however, take some time to start the Flask app each time I start debugging, not sure if this is due to Flask being started via Python. Invoking flask.exe and python.exe -m flask from the command line seems to be evenly fast - notably faster than starting debugging in VS Code.

Categories