Create /home/bob/foobar.py in Visual Studio
Code with the VS Code Docker
Extension
import ptvsd
import time
ptvsd.enable_attach(address = ('0.0.0.0', 5678))
ptvsd.wait_for_attach()
time.sleep(2)
print("all righty then")
Set a breakpoint on the last line.
Debug|Add Configuration
In launch.json add to "configurations"
{
"name": "Python Attach (Remote Debug ptsvd default)",
"type": "python",
"request": "attach",
"pathMappings": [
{
"localRoot": "/home/bob", // You may also manually specify the directory containing your source code.
"remoteRoot": "/home/bob" // Linux example; adjust as necessary for your OS and situation.
}
],
"port": 5678, // Set to the remote port.
"host": "0.0.0.0" // Set to your remote host's public IP address.
},
$ python -m pip install --user --upgrade ptvsd
$ python foobar.py
Start the debugger with configuration "Python Attach (Remote Debug ptsvd default)". It stops at the breakpoint.
But if I run mpirun
$ mpirun --allow-run-as-root -np 2 -H localhost:2 -bind-to none -map-by slot -x PATH -mca pml ob1 -mca btl ^openib python ./foobar.py
I get the error socket.error: [Errno 98] Address already in use
Is there a way to assign multiple ports for an arbitrary number of processes in launch.json and in foobar.py?
This is not currently possible but has been added by the ptvsd team as a possible enhancement. Vote it up if this is important to you. Upvotes for this team seem to move features up the to-do list.
Related
Is there a way for remotely debugging Python3 with IntelliJ? I couldn't find any options for it. With VS Code, it's as simple as having this file:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "172.18.0.5",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
"justMyCode": true
}
]
}
and everything works like a (py)charm!
How can i do it with IntelliJ?
I examined all of JetBrains' tutorials, but none seemed to fit what I was searching for.
IntelliJ IDEA and PyCharm both support Remote Debugging with Python, just follow the guide here:
https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html
If you are using IntelliJ IDEA, you may install the Python plugin first, by Settings > Plugins > Marketplace, inputting Python, and then clicking the Install button.
And here is the step to step guide on Remote debugging with the Python remote debug server configuration https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html#remote-debug-config:
Let IDEA/PyCharm host a debugger server on the machine your IDE runs on locally:
Note: This step is a lot different compared with VS Code(Sorry I know little about that tool), in VS Code it's using the package https://github.com/microsoft/debugpy, but it first will call python -m debugpy --listen 0.0.0.0:5678 ./myscript.py to create a debug server on the remote machine/docker, then the Client(here is VS Code) connect to it. But in IDEA/PyCharm it's just the opposite: Python Debug server is listening on the local machine, and the remote script will connect to the IDE's server.
From the main menu, choose Run| Edit Configuration.... The Run/debug configurations dialog opens. You have to click Add configuration on the toolbar, and from the list of available configurations, select Python Debug Server.
Please change the port to a different number between 0 and 65535, default value 0 will not work here, then remember the steps above the IDE host name area.
More info about the IDE host name: the user may specify another host if the debug server is not in the same machine (default is the localhost). In your case you want to connect it from a container, you may change it to the IP of your current machine.
You may also set up the Path Mapping: by clicking the folder selector and then selecting the real folder path as ${workspaceFolder}.
Then click OK button to save it, and then you click the run button to let the debug server run up and listen on remote code to connect to it, it will display something like this in the Debug tool window:
Starting debug server at port 12,345
Waiting for process connection…
Use the following code to connect to the debugger:
import pydevd_pycharm
pydevd_pycharm.settrace('some ip address', port=12345, stdoutToServer=True, stderrToServer=True)
Install and run debug server client in remote machine/container
On your container, execute the following one-time commands in the terminal(In case you are using IDE 2022.3):
pip3 install pydevd-pycharm~=223.8214.51
Then modify the Python file you want to debug by adding lines at the begging of the source file(THIS STEP IS MANDATORY), for eg:
#==============this code added==================================================================:
import pydevd_pycharm
pydevd_pycharm.settrace('some ip address', port=12345, stdoutToServer=True,
stderrToServer=True)
# ... The other codes remain as is
Then you may start remote debugging by just executing the below command in the terminal of the remote machine/container:
python3 myfile.py
Return to the local IDE and begin debugging
Just after step 2, now the local IDE will begin the debugging.
And the final result may look like this:
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
I have a container running on my host with a python process and I have the vs code installed on the host. Is it possible to debug the python process from the host installed vscode ? If yes, how can this be achieved ?
Since I do not know what python process you have running, I'll use FastAPI as an example.
First, you need to define a new configuration in the file <project_root>/.vscode/launch.json. To access this file, you can go to the Run & Debug section on the activity bar, and press the cog/wheel at the top.
Add a new item in the configurations like shown below.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
}
]
}
Now you need to include debugpy. If you are using a requirements file, you can just add it there. If you use Poetry, you can run poetry add debugpy in your terminal (the Dockerfile example shows how you can use Poetry in a docker image).
First option - In your Dockerfile you need to run debugpy and make it listen for port 5678.
Now the debugger will start every time you run the container and await attachment.
FROM python:3.10.0
# Set the working directory:
WORKDIR /usr/src
# Copy the pyproject.toml file (or requirements.txt) to cache them in the docker layer:
COPY [ "./src/pyproject.toml", "./"]
# Set the python path if needed.
# For example, if main.py is not located in the project root:
ENV PYTHONPATH="$PYTHONPATH:${PWD}"
# Upgrade pip and install Poetry:
RUN pip install --upgrade pip && pip install poetry==1.1.12
# Project initialization using Poetry:
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi
# Run the application:
CMD ["python", "-m", "debugpy", "--wait-for-client", "--listen", "0.0.0.0:5678", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--reload-exclude", "tests"]
EXPOSE 8000 5678
Second option - Set up the debugger in the main.py file by adding the lines below. This means that the debugger will be attached to the container when it starts; if the debugger variable is set to True.
debugger = True
if __name__ == "__main__":
if debugger:
import debugpy
debugpy.listen(("0.0.0.0", 5678))
debugpy.wait_for_client()
uvicorn.run(
"main:app",
host="localhost",
port=8000,
log_level="info",
reload=True,
)
Both examples need the Dockerfile to expose the port 5678.
Now, after running the container, the debugger will "pause" and await attachment. To attach the debugger, go back to Run & Debug section on the activity bar in VSCode and select Python: Remote Attach from the dropdown menu and press play.
Now you should be able to use the debugger inside the docker container.
I want to debug my Django application using a docker container in Visual Studio Code.
Microsoft published a guide how to do that, which I followed step by step:
https://code.visualstudio.com/docs/containers/quickstart-python
But when I try to run the debugger, I get the following error message:
Timed out waiting for launcher to connect
Here is what I did step by step:
I initialized a simple Django application using django-admin startproject helloworld
In VS Code I opened the folder including the manage.py
Opened Command Palette Ctrl + Shift + P, and then selected Docker: Add Docker Files to Workspace...
Select Application Platform Python: Django
Include Docker Compose files No
Relative path to the app's entrypoint manage.py
What ports does your app listen on? 8000
VS Codes then creates several files (see below).
When I try to start the debugger (like in the guide), I get the following error message:
The terminal doesn't show any error messages, but the commands executed:
.vscode/launch.json:
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
]
}
.vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "dockerdebugging:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"python": {
"args": [
"runserver",
"0.0.0.0:8000",
"--nothreading",
"--noreload"
],
"file": "manage.py"
}
}
]
}
Dockerfile:
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim-buster
EXPOSE 8000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE 1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1
# Install pip requirements
ADD requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
ADD . /app
# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "helloworld.wsgi"]
requirements.txt:
# To ensure app dependencies are ported from your virtual environment/host machine into your container, run 'pip freeze > requirements.txt' in the terminal to overwrite this file
django==3.0.3
gunicorn==20.0.4
VS Code Version: 1.47.1
Python Extension Version: v2020.7.94776
The idea of debugging in vs code is to:
use debugpy to launch your code with a port, say 5678
in vscode to 'Remote Attach' to that process.
I may be wrong but what i can see from your code, you didn't attach to your process.
I wrote the way I did here, I use docker-compose and docker. My way is different with yours but you will get the idea...:)
My issue were missing packages. Docker usually works fine, I haven't had any issues before at all.
I originally installed docker like described in the official documentation:
https://docs.docker.com/engine/install/ubuntu/
But after I tried installing the docker.io package, debugging worked fine in VS Code:
sudo apt install docker.io
There's no errors compared to the vscode tutorial in your project. Cause the error is timeout waiting for luncher to connect, try restart docker service and reload your window in vscode.
Here is a clue and a workaround:
The instructions I followed had me open VS code inside my ubuntu WLS2 instance. Note: my app is just a generic python app, not django.
If I click this and change it to open as a windows folder, then fun debug, everything suddenly works for me. (It spins up the docker and connects to it with debug, does breakpoints etc.)
I think what is happening for me is that "sometimes" docker is starting the container/app on its own WSL instance and my ubuntu instance cannot route to it for the debug.
I say sometimes, because sometimes it works. I think it might be related to which application (docker, ubuntu, vscode) I start first when I boot my machine.
I've messed with the docker, resources, WSL integration settings, the windows firewall, and restarted various things.
I'm sure a proper fix is not that complicated, however running VS code in native windows is enough of a fix for me. I don't see why the added complexity of starting the VS code session inside WSL is actually necessary.
I have a virtual machine running a python process and I want to debug it from my host.
My virtual machine ip is 192.168.10.20.
As per the instructions I set up my launch.json as
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "/Users/wrkspace",
"remoteRoot": "/home/wrkspace",
"port": 3000,
"secret": "my_secret",
"host": "192.168.10.20"
},
I have two files,one on my host machine and on my vm called test.py which is a helloworld asking for a user input
My host machine has this at the top of the file
ptvsd.enable_attach("my_secret", address = ('192.168.10.20'3000))
# Enable the line of source code below only if you want the application
to wait until the debugger has attached to it
#ptvsd.wait_for_attach()
While on my vm, where I want to debug
ptvsd.enable_attach("my_secret", address = ('192.168.10.20'3000))
# Enable the line of source code below only if you want the application
to wait until the debugger has attached to it
ptvsd.wait_for_attach()
Once I run my attached debugger and then I kick of the test on my vm by going python test.py. It looks like it does something but then fails with
There was an error in starting the debug server.
Error="code":"ENETUNREACH","errno":"ENETUNREACH","syscall":"connect","address":"192.168.10.20","port":3000}
There was an error in starting the debug server. Error = {"code":"ENETUNREACH","errno":"ENETUNREACH","syscall":"connect","address":"192.168.10.20","port":3000}
Is there something I'm missing?
A few steps to trouble shoot these:
1) test the debug port to make sure it is open using "nc -zv test.com 30302"
2) make sure your webserver is not reloading the app after debugger was connected to it. this is the option for flask: " use_reloader=False"