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"
}
}
]
}
Related
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
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.
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.
I am curious to know if anyone successfully attached a debugger to a python script -
which is actually an Azure Function App running in a container.
I have all the VSCODE extensions but still can't seem to connect to the running container.
I've run the container from docker-compose.debug and from Bash cmd:
$ docker run -p 5678:5678 -it -e AzureWebJobsStorage="UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://host.docker.internal" nfunc:latest
NB I have to set an environment variable AzureWebJobsStorage for the storage.
My launch.json as follows:
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
What am I missing please ..
Can Functions Apps be debugged at all in a container?
Why does it not hit a breakpoint ?
Thank you
In this case, I believe you'll have to do the profiling. You will have to follow these:
If its not a blessed image, then first you would have to install SSH
if you want to get into the container.
Then you will have to make use of tools such as cProfile or other related python modules to profile the code.
Here is a documentation for windows application. You might want to take a look :
https://azureossd.github.io/2017/09/01/profile-python-applications-in-azure-app-services/index.html
This issue has been tracked : https://github.com/Azure/azure-functions-docker/issues/17
Cross-post from https://github.com/pantsbuild/pex/issues/1181 as I didn't really know where to ask this.
I'm using Nginx Unit, which has an ASGI configuration for my FastAPI application. It's working fine, but I have no clue how to use it with a PEX.
I've attached it below, but the salient point is that there is a Python plugin for Nginx Unit which is looking for a "module" and a "callable".
What this looks like now is "apigateway.main:app" similar to uvicorn or whatever other server implementation you're using. Unlike uvicorn, gunicorn, or whatever - I don't think I can package any other tool in my pex file and use PEX_SCRIPT.
Is there any suggestion on if/how I can expose a module and variable outside of the pex?
{
"listeners": {
"*:80": {
"pass": "routes"
}
},
"routes": [
{
"action": {
"pass": "applications/api"
}
}
],
"applications": {
"api": {
"type": "python 3.9",
"path": "/app",
"module": "apigateway.main",
"callable": "app",
"limits": {
"requests": 100
},
"processes": {}
}
},
"access_log": "/var/log/access.log"
}
This was answered in the Github ticket (https://github.com/pantsbuild/pex/issues/1181):
All the traditional language here is meant to segue to the recent venv
pex tool feature released with Pex 2.1.22. If you build your PEX file
with venv support (add --include-tools to the Pex command line) then
you gain the ability to create a venv from your PEX file. Doing so is
a one-time manual step on the target machine or image:
PEX_TOOLS=1 ./my.pex venv /app That will create a virtual environment
containing your app and its dependencies under /app. You can point
Nginx Unit there with home. I've done all this over at
https://github.com/jsirois/pex-issues-1181 to prove it works.