I truly think that no matter what your platform is, you must have access to a comfortable development environment and a working debugger is one of the most important part of it. Remote debugging can be very helpful: it is possible to execute code on a remote machine and benefit from a nice debugging experience locally in your favorite code editor. In this blog post I propose to review the setup of Python remote debugging with the portable and popular code editor VSCode.
Actually VSCode documentation provides some very short instructions. In this blog post we will provide more explanations. We will assume that we do not have any security constraints. We will discuss in appendix how we could solve this using SSH portforwarding.
We assume that the reader is familiar with the usage of a debugger in VSCode. In addition, we assume that the reader knows how to logon on a remote machine using SSH.
In this blog post we used an Ubuntu Azure Virtual Machine. Let us present precisely what remote debugging is. In this post, the name remote stands for our Ubuntu VM on Azure while the client is our local, e. MACOS, computer. Therefore, it is important to keep exactly the same. The remotely executed Python waits until the client debugging agent is attached. Obviously network communication is involved here and that is actually the major pitfall when configuring remote debugging. We will try to simplify things so the debugging experience becomes crystal clear.
To make things simpler we decided to show an example where the Python process is executed on a remote machine whose IP address is We use the good old port 80 for the communication the usual port for http. Before doing anything else we need to make sure that our remote VM network configuration is ok. We will make sure that machine Firstly, using an SSH session on remote machine we will start a webserver using the following Python3 command.
You may need elevated privilege for listening on port 80 for real production usage give this privilege to the current user, do not sudo the process. Secondly on a client terminal you should be able request your machine using wget spider mode to avoid file download. You should get response from the server. If you see some errors, you mat need to open the 80 port in firewall configuration, see instructions here for Azure.
Python debug configurations in Visual Studio Code
Make sure you can contact your machine on port 80 by running a one line Python server. At this stage your network configuration is ok. You can stop the Python command that runs the webserver. Make sure that you have the VSCode Python extension installed. Follow the instructions here to add a new Debug configuration in your launch.
It is important to understand that this configuration is only for VSCode. The host corresponds to the machine where the remote Python process is ran. The port corresponds to the port that will be used by the remote process to communicate with the client debugging agent, in our case it is As explained in the introduction, the Python file must be the same on client and on remote machine.
There is one exception yet, the line ptvsd. Indeed, it tells the Python process to pause and wait that the client is attached to continue. Of course in order to execute it you may need to install dependencies for example using Pip so it executes on the remote machine. I suggest that you force the install of version 3.Version 1. Read about the new features and fixes from March. The Python extension supports debugging of a number of types of Python applications.
For a short walkthrough of basic debugging, see Tutorial - Configure and run the debugger. Also see the Flask tutorial.
Both tutorials demonstrate core skills like setting breakpoints and stepping through code. For general debugging features such as inspecting variables, setting breakpoints, and other activities that aren't language-dependent, review VS Code debugging.
This article addresses only those considerations that are specific to Python, mainly Python-specific debugging configurationsincluding the necessary steps for specific app types and remote debugging.
A configuration drives VS Code's behavior during a debugging session. Configurations are defined in a launch. Note In order to change debugging configuration, your code must be stored in a folder. If you don't yet have any configurations defined, you'll see "No Configurations" in the drop-down list, and a dot on the settings icon:. A configuration menu will open from the Command Palette allowing you to choose the type of debug configuration you want for the opened file.
For now, in the Select a debug configuration menu that appears, select Python File. The details of configuration properties are covered later in this article under Standard configuration and options. Additional configurations are also described in this article under Debugging specific app types.
By default, VS Code shows only the most common configurations provided by the Python extension. You can select other configurations to include in launch. When you use the command, VS Code prompts you with a list of all available configurations be sure to scroll down to see all the Python options :.
Selecting the Node. See Debugging specific app types for details on all of these configurations. During debugging, the Status Bar shows the current configuration on the lower left; to its right is the current debugging interpreter.
Subscribe to RSS
Selecting the configuration brings up a list from which you can choose a different configuration:. By default, the debugger uses the same python. To use a different interpreter for debugging specifically, set the value for pythonPath in launch. Alternately, select the named interpreter on the Status Bar to select a different one, which updates python. When you first create launch. The specific settings are described in the following sections. You can also add other settings, such as argsthat aren't included in the standard configurations.
Tip : It's often helpful in a project to create a configuration that runs a specific startup file. For example, if you want to always launch startup. Provides the fully qualified path to the python program's entry module startup file.
I am having my remote ubuntu Also, in the integrated terminal I can ssh to remote system and run the program using remote python interpreter which is installed on virtual environment like:. I went through the docs suggesting the use of ptvsd extension which asks to have 2 copies of source files one on local, another one server but I am not sure how to configure it in this scenario. Appreciate the help. Edit 1: As, I said I have gone through the docs but I am not clear how to configure in this scenario.
But I have only one copy of source file i. I have just mounted it on my local file system. So, should I give my local ip address or it should be remote system ip and which port number should I use as I am having only one copy of source and hence ptvsd, how is it going to communicate.
Also, in the configuration, what should I use for localRoot location and remoteRoot location. You need to put the remote ip address in both ptvsd. You also need to change the remoteRoot value to the path of the directory where the script is located in the remote machine e. Finally, open an ssh connection: ssh -L localhostrun your script in the remote machine and attach the debugger in the local machine.
Please see the official docs on how to do remote debugging. Learn more. Asked 2 years, 4 months ago. Active 2 months ago. Viewed 5k times. Kumar Kumar 4 4 silver badges 16 16 bronze badges.
Active Oldest Votes. Brett Cannon Brett Cannon 5, 1 1 gold badge 19 19 silver badges 20 20 bronze badges. I have gone through the official docs but I am not able to figure it out, Please see my edit.
Please read the documentation again; it was recently updated and answers your questions. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.The extension supports debugging of a number of types of python applications. However these are in active development. Debugging a standard python application is possible by adding the standard configuration settings in the launch. This setting points to the fully aualified path of the python program to be debugged.
Resulting in the debugging of the active file in the editor. Entering the name of the Python file is also supported.
Debugging: Remote Debuging
However you need to ensure the file name is fully qualitified path. You have two options:. This setting points to the python interpreter to be used for debugging purposes. The setting "stopOnEntry":true will cause the debugger to break at the first line of the python program being debugged.
If this is not desired, then change the value from true to false. The default value of this setting is true. This setting controls the display of a terminal console window for debugging. By default this is turned off with a value of "none".
If a terminal console window is to be displayed whilst debugging, then use one of the following settings:. For further details on debugging terminal console apps, go here. This directory can be used to control specify the current directory of the python program being debugged. This defaults to the workspace root project directory. If this setting is missing, then all output from the program such as those generated by Print commands will not be displayed in the debugger output window.
If this setting is missing, then no additional environment variables are set. Whether this is set or not, the debugger process will inherit the environment variables.Visual Studio Code Remote - SSH
Python in Visual Studio Code. Github Examples Documentation.Version 1. Read about the new features and fixes from March. One of the key features of Visual Studio Code is its great debugging support.
VS Code's built-in debugger helps accelerate your edit, compile and debug loop. VS Code has built-in debugging support for the Node. Tip: The extensions shown above are dynamically queried. Select an extension tile above to read the description and reviews to decide which extension is best for you. The following documentation is based on the built-in Node. It is helpful to first create a sample Node.
You can follow the Node. Once you have a simple application set up, this page will take you through VS Code debugging features. The Run view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings. If running and debugging is not yet configured no launch. However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details.
VS Code keeps debugging configuration information in a launch. To create a launch. VS Code will try to automatically detect your debug environment, but if this fails, you will have to choose it manually:. Note : You can debug a simple application even if you don't have a folder open in VS Code, but it is not possible to manage launch configurations and set up advanced debugging.
Note that the attributes available in launch configurations vary from debugger to debugger. Hover help is also available for all attributes. Do not assume that an attribute that is available for one debugger automatically works for other debuggers too. If you see green squiggles in your launch configuration, hover over them to learn what the problem is and try to fix them before launching a debug session. Review all automatically generated values and make sure that they make sense for your project and debugging environment.Visual Studio can launch and debug Python applications locally and remotely on a Windows computer see Remote debugging.
It can also debug remotely on a different operating system, device, or Python implementation other than CPython using the ptvsd library. When using ptvsd, the Python code being debugged hosts the debug server to which Visual Studio can attach. This hosting requires a small modification to your code to import and enable the server, and may require network or firewall configurations on the remote computer to allow TCP connections.
You can easily create a Linux virtual machine on Azure and access it using Remote Desktop from Windows. Ubuntu for the VM is convenient because Python is installed by default; otherwise, see the list on Install a Python interpreter of your choice for additional Python download locations.
On the remote computer, create a Python file called guessing-game. Install the ptvsd package into your environment using pip3 install ptvsd. It's a good idea to record the version of ptvsd that's installed in case you need it for troubleshooting; the ptvsd listing also shows available versions.
Enable remote debugging by adding the code below at the earliest possible point in guessing-game. Save the file and run python3 guessing-game. Create a copy of the remote file on the local computer and open it in Visual Studio.
It doesn't matter where the file is located, but its name should match the name of the script on the remote computer. Optional To have IntelliSense for ptvsd on your local computer, install the ptvsd package into your Python environment. On older versions of Visual Studio these commands are named Transport and Python remote debugging. If you happen to start another program on the remote computer after populating this list, select the Refresh button.
Visual Studio then switches into debugging mode while the script continues to run on the remote computer, providing all the usual debugging capabilities. After you do so, Visual Studio on your local computer stops at that breakpoint, shows local variables, and so on:.
When you stop debugging, Visual Studio detaches from the program, which continues to run on the remote computer. Make sure that you've selected Python remote ptvsd for the Connection Type Python remote debugging for Transport with older versions.
Check that the secret in the Connection Target or Qualifier exactly matches the secret in the remote code. Check that you're opened the remote debugging port on the remote computer, and that you've included the port suffix in the connection target, such as Check that the version of ptvsd installed on the remote computer as returned by pip3 list matches that used by the version of the Python tools you're using in Visual Studio in the table below. If necessary, update ptvsd on the remote computer.
The following information applies only to remote debugging with ptvsd 3. With ptvsd 3. You enter this secret when attaching the remote debugger.Version 1.
Read about the new features and fixes from March. Once connected to a server, you can interact with files and folders anywhere on the remote filesystem. No source code needs to be on your local machine to gain these benefits since the extension runs commands and other extensions directly on the remote machine.
This lets VS Code provide a local-quality development experience — including full IntelliSense completionscode navigation, and debugging — regardless of where your code is hosted. See the Remote Development with Linux article for information prerequisites and tips for getting community supported distributions up and running.
Install the Remote Development extension pack. See the Tips and Tricks article for details. If VS Code cannot automatically detect the type of server you are connecting to, you will be asked to select the type manually. Once you select a platform, it will be stored in VS Code settings under the remote.
VS Code will keep you up-to-date using a progress notification and you can see a detailed log in the Remote - SSH output channel. Tip: Connection hanging or failing? See troubleshooting tips for information on resolving common problems. After you are connected, you'll be in an empty window. You can always refer to the Status bar to see which host you are connected to.
Remote debugging Python with VSCode
From here, install any extensions you want to use when connected to the host and start editing! The default configuration does not include a keyboard shortcut for this command. You can also simply exit VS Code to close the remote connection. If you have a set of hosts you use frequently or you need to connect to a host using some additional options, you can add them to a local file that follows the SSH config file format.
To make setup easy, the extension can guide you through adding a host without having to hand edit this file. Finally, you'll be asked to pick a config file to use. You can also set the "remote. The extension takes care of the rest! See Tips and Tricks for details on generating the key shown here. You can manually edit this file with anything the SSH config file format supports, so this is just one example. The Remote Explorer allows you to both open a new empty window on the remote host or directly open a folder you previously opened.
Expand the host and click on the Open Folder icon next to the folder you want to open on the host.
This ensures you have smooth experience and allows you to install any needed extensions for a given workspace on an SSH host from your local machine. This way, you can pick up exactly where you left off, from a different machine complete with your extensions. If you install an extension from the Extensions view, it will automatically be installed in the correct location.