Visual studio code 2010. Install VS Code and the Remote WSL extension. Visit the VS Code install page and select the 32 or 64 bit installer. Install Visual Studio Code on Windows (not in your WSL file system). When prompted to Select Additional Tasks during installation, be sure to check the Add to PATH option so you can easily open a folder in WSL using the code command.
- You can find the remote debugger (msvsmon.exe) on a computer with Visual Studio Community, Professional, or Enterprise already installed. For some scenarios, the easiest way to set up remote debugging is to run the remote debugger (msvsmon.exe) from a file share.
- 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.
- Learn how to use Visual Studio Code for remote development using the Remote-SSH plugin to seamlessly sync your IDE with your code running on a remote system.
- Visual Studio Code Remote Extension; This allowed me to edit my Raspberry Pi files from within Visual Studio Code. So, I get all the joys of writing code directly on my Raspberry Pi, but with all the bells-and-whistles of Visual Studio Code (VSC). For the most part, setup is pretty straightforward. But the Pi side can get tricky, so I’m going.
Visual Studio Code Remote Wsl
![Remote Remote](/uploads/1/3/8/2/138217005/370235576.jpg)
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.
For an introduction to remote debugging, see Deep Dive: Cross-platform remote debugging (youtube.com, 6m22s), which is applicable to both Visual Studio 2015 and 2017.
Set up a Linux computer
The following items are needed to follow this walkthrough:
- A remote computer running Python on an operating system like Mac OSX or Linux.
- Port 5678 (inbound) opened on that computer's firewall, which is the default for remote debugging.
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.
For details on creating a firewall rule for an Azure VM, see Open ports to a VM in Azure using the Azure portal.
Prepare the script for debugging
- On the remote computer, create a Python file called guessing-game.py with the following code:
- Install the
ptvsd
package into your environment usingpip3 install ptvsd
.NoteIt'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.py, before other code. (Though not a strict requirement, it's impossible to debug any background threads spawned before the
enable_attach
function is called.) - Save the file and run
python3 guessing-game.py
. The call toenable_attach
runs in the background and waits for incoming connections as you otherwise interact with the program. If desired, thewait_for_attach
function can be called afterenable_attach
to block the program until the debugger attaches.
Tip
In addition to
enable_attach
and wait_for_attach
, ptvsd also provides a helper function break_into_debugger
, which serves as a programmatic breakpoint if the debugger is attached. There is also an is_attached
function that returns True
if the debugger is attached (note that there is no need to check this result before calling any other ptvsd
functions).Attach remotely from Python Tools
In these steps, we set a simple breakpoint to stop the remote process.
- 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.
- Select Debug > Attach to Process.
- In the Attach to Process dialog that appears, set Connection Type to Python remote (ptvsd). (On older versions of Visual Studio these commands are named Transport and Python remote debugging.)
- In the Connection Target field (Qualifier on older versions), enter
tcp://<ip_address>:5678
where<ip_address>
is that of the remote computer (which can be either an explicit address or a name like myvm.cloudapp.net), and:5678
is the remote debugging port number. - Press Enter to populate the list of available ptvsd processes on that computer:If you happen to start another program on the remote computer after populating this list, select the Refresh button.
- Select the process to debug and then Attach, or double-click the process.
- Visual Studio then switches into debugging mode while the script continues to run on the remote computer, providing all the usual debugging capabilities. For example, set a breakpoint on the
if guess < number:
line, then switch over to the remote computer and enter another guess. 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. ptvsd also continues listening for attaching debuggers, so you can reattach to the process again at any time.
Connection troubleshooting
- 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 the IP address in the Connection Target (or Qualifier) matches that of the remote computer.
- 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
:5678
.- If you need to use a different port, you can specify it in the
enable_attach
call using theaddress
argument, as inptvsd.enable_attach(address = ('0.0.0.0', 8080))
. In this case, open that specific port in the firewall.
- If you need to use a different port, you can specify it in the
- 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.Visual Studio version Python tools/ptvsd version 2017 15.8 4.1.1a9 (legacy debugger: 3.2.1.0) 2017 15.7 4.1.1a1 (legacy debugger: 3.2.1.0) 2017 15.4, 15.5, 15.6 3.2.1.0 2017 15.3 3.2.0 2017 15.2 3.1.0 2017 15.0, 15.1 3.0.0 2015 2.2.6 2013 2.2.2 2012, 2010 2.1
Using ptvsd 3.x
The following information applies only to remote debugging with ptvsd 3.x, which contains certain features that were removed in ptvsd 4.x.
- With ptvsd 3.x, the
enable_attach
function required that you pass a 'secret' as the first argument that restricts access to the running script. You enter this secret when attaching the remote debugger. Though not recommended, you can allow anyone to connect, useenable_attach(secret=None)
. - The connection target URL is
tcp://<secret>@<ip_address>:5678
where<secret>
is the string passedenable_attach
in the Python code.
By default, the connection to the ptvsd 3.x remote debug server is secured only by the secret and all data is passed in plain text. For a more secure connection, ptvsd 3.x supports SSL using the
tcsp
protocol, which you set up as follows:- On the remote computer, generate separate self-signed certificate and key files using openssl:When prompted, use the hostname or IP address (whichever you use to connect) for the Common Name when prompted by openssl.(See Self-signed certificates in the Python
ssl
module docs for additional details. Note that the command in those docs generates only a single combined file.) - In the code, modify the call to
enable_attach
to includecertfile
andkeyfile
arguments using the filenames as the values (these arguments have the same meaning as for the standardssl.wrap_socket
Python function):You can also make the same change in the code file on the local computer, but because this code isn't actually run, it isn't strictly necessary. - Restart the Python program on the remote computer, making it ready for debugging.
- Secure the channel by adding the certificate to Trusted Root CA on the Windows computer with Visual Studio:
- Copy the certificate file from the remote computer to the local computer.
- Open Control Panel and navigate to Administrative Tools > Manage computer certificates.
- In the window that appears, expand Trusted Root Certification Authorities on the left side, right-click Certificates, and select All Tasks > Import.
- Navigate to and select the .cer file copied from the remote computer, then click through the dialogs to complete the import.
- Repeat the attach process in Visual Studio as described earlier, now using
tcps://
as the protocol for the Connection Target (or Qualifier). - Visual Studio prompts you about potential certificate issues when connecting over SSL. You may ignore the warnings and proceed, but although the channel is still encrypted against eavesdropping it can be open to man-in-the-middle attacks.
- If you see the remote certificate is not trusted warning below, it means you did not properly add the certificate to the Trusted Root CA. Check those steps and try again.
- If you see the remote certificate name does not match hostname warning below, it means you did not use the proper hostname or IP address as the Common Name when creating the certificate.
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 debugpy library.
Visual Studio Code Remote Ssh
When using debugpy, 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.
Note
For Visual Studio 2019 version 16.4 and earlier, the ptvsd library was used. The debugpy library replaced ptvsd 4 in Visual Studio 2019 version 16.5.
Set up a Linux computer
The following items are needed to follow this walkthrough:
- A remote computer running Python on an operating system like Mac OSX or Linux.
- Port 5678 (inbound) opened on that computer's firewall, which is the default for remote debugging.
Note
This walkthrough is based on Visual Studio 2019 version 16.6.
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.
For details on creating a firewall rule for an Azure VM, see Open ports to a VM in Azure using the Azure portal.
Prepare the script for debugging
- On the remote computer, create a Python file called guessing-game.py with the following code:
- Install the
debugpy
package into your environment usingpip3 install debugpy
.NoteIt's a good idea to record the version of debugpy that's installed in case you need it for troubleshooting; the debugpy listing also shows available versions. - Enable remote debugging by adding the code below at the earliest possible point in guessing-game.py, before other code. (Though not a strict requirement, it's impossible to debug any background threads spawned before the
listen
function is called.) - Save the file and run
python3 guessing-game.py
. The call tolisten
runs in the background and waits for incoming connections as you otherwise interact with the program. If desired, thewait_for_client
function can be called afterlisten
to block the program until the debugger attaches.
Tip
In addition to
listen
and wait_for_client
, debugpy also provides a helper function breakpoint
, which serves as a programmatic breakpoint if the debugger is attached. There is also an is_client_connected
function that returns True
if the debugger is attached (note that there is no need to check this result before calling any other debugpy
functions).Attach remotely from Python Tools
In these steps, we set a simple breakpoint to stop the remote process.
- 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 debugpy on your local computer, install the debugpy package into your Python environment.
- Select Debug > Attach to Process.
- In the Attach to Process dialog that appears, set Connection Type to Python remote (debugpy).
- In the Connection Target field, enter
tcp://<ip_address>:5678
where<ip_address>
is that of the remote computer (which can be either an explicit address or a name like myvm.cloudapp.net), and:5678
is the remote debugging port number. - Press Enter to populate the list of available debugpy processes on that computer:If you happen to start another program on the remote computer after populating this list, select the Refresh button.
- Select the process to debug and then Attach, or double-click the process.
- Visual Studio then switches into debugging mode while the script continues to run on the remote computer, providing all the usual debugging capabilities. For example, set a breakpoint on the
if guess < number:
line, then switch over to the remote computer and enter another guess. 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. debugpy also continues listening for attaching debuggers, so you can reattach to the process again at any time.
Connection troubleshooting
- Make sure that you've selected Python remote (debugpy) for the Connection Type
- Check that the secret in the Connection Target exactly matches the secret in the remote code.
- Check that the IP address in the Connection Target matches that of the remote computer.
- Check that you've opened the remote debugging port on the remote computer, and that you've included the port suffix in the connection target, such as
:5678
.- If you need to use a different port, you can specify it in the
listen
, as indebugpy.listen((host, port))
. In this case, open that specific port in the firewall.
- If you need to use a different port, you can specify it in the
- Check that the version of debugpy 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 debugpy on the remote computer.Visual Studio version Python tools/debugpy version 2019 16.6 1.0.0b5 2019 16.5 1.0.0b1
Note
Visual Studio 2019 version 16.0-16.4 utilized ptvsd, not debugpy. The process in this walkthrough for those versions is similar, but the function names are different. Visual Studio 2019 version 16.5 uses debugpy, but the function names were the same as those in ptvsd. Instead of
listen
, you would use enable_attach
. Instead of wait_for_client
, you would use wait_for_attach
. Instead of breakpoint
, you would use break_into_debugger
.Using ptvsd 3.x for legacy debugging
Visual Studio 2017 versions 15.8 and later use a debugger based on ptvsd version 4.1+. Visual Studio 2019 versions 16.5 and later use a debugger based on debugpy. These versions of the debugger are compatible with Python 2.7 and Python 3.5+. If you're using Python 2.6, 3.1 to 3.4, or IronPython, Visual Studio shows the error, Debugger does not support this Python environment. The following information applies only to remote debugging with ptvsd 3.x.
- With ptvsd 3.x, the
enable_attach
function required that you pass a 'secret' as the first argument that restricts access to the running script. You enter this secret when attaching the remote debugger. Though not recommended, you can allow anyone to connect, useenable_attach(secret=None)
. - The connection target URL is
tcp://<secret>@<ip_address>:5678
where<secret>
is the string passedenable_attach
in the Python code.
By default, the connection to the ptvsd 3.x remote debug server is secured only by the secret and all data is passed in plain text. For a more secure connection, ptvsd 3.x supports SSL using the
tcsp
protocol, which you set up as follows:- On the remote computer, generate separate self-signed certificate and key files using openssl:When prompted, use the hostname or IP address (whichever you use to connect) for the Common Name when prompted by openssl.(See Self-signed certificates in the Python
ssl
module docs for additional details. Note that the command in those docs generates only a single combined file.) - In the code, modify the call to
enable_attach
to includecertfile
andkeyfile
arguments using the filenames as the values (these arguments have the same meaning as for the standardssl.wrap_socket
Python function):You can also make the same change in the code file on the local computer, but because this code isn't actually run, it isn't strictly necessary. - Restart the Python program on the remote computer, making it ready for debugging.
- Secure the channel by adding the certificate to Trusted Root CA on the Windows computer with Visual Studio:
- Copy the certificate file from the remote computer to the local computer.
- Open Control Panel and navigate to Administrative Tools > Manage computer certificates.
- In the window that appears, expand Trusted Root Certification Authorities on the left side, right-click Certificates, and select All Tasks > Import.
- Navigate to and select the .cer file copied from the remote computer, then click through the dialogs to complete the import.
- Repeat the attach process in Visual Studio as described earlier, now using
tcps://
as the protocol for the Connection Target (or Qualifier). - Visual Studio prompts you about potential certificate issues when connecting over SSL. You may ignore the warnings and proceed, but although the channel is still encrypted against eavesdropping it can be open to man-in-the-middle attacks.
- If you see the remote certificate is not trusted warning below, it means you did not properly add the certificate to the Trusted Root CA. Check those steps and try again.
- If you see the remote certificate name does not match hostname warning below, it means you did not use the proper hostname or IP address as the Common Name when creating the certificate.