PyCharm 2017.3 Help

Configuring Remote Interpreter via WSL

This feature is only supported in the Professional edition.

Prerequisites

We have to prepare our Windows system for work with WSL. So, fulfil the preliminary steps:

  1. First, check that your Windows version and build are appropriate. To do this, click the Windows button in the lower-left corner of the screen and start typing System Information. Choose System Information and see:
    wsl system info

    Windows 10 is eligible for WSL, and build number is greater than 14361.

  2. Second, start PowerShell as an administrator (to do this, click the Windows icon in the lower-left corner of the screen and start typing PowerShell. You'll see the search results. Right-click the command Windows PowerShell and choose Run as administrator on the context menu):
    wsl powershell

    and enter the following command:

    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

    Restart your computer.

  3. Third, having done this, turn on the developer mode: again click the Windows icon in the lower-left corner of the screen and start typing developer. You'll see the search results. Click the Use developer features command:
    wsl developer mode command

    You'll see the settings for developers. Click the option Developer mode:

    wsl developer mode
  4. Next, start cmd (click the Windows button in the lower-left corner of the screen and start typing cmd), and run the bash command. Enter your Linux username and password. Now go make yourself a coffee as this can take some time!
    wsl bash
  5. You have to update to the latest version. To do that, in the cmd console, enter the following command:
    sudo apt-get update

    And then you should upgrade (having patience is important!):

    sudo apt-get upgrade
  6. Now that you've installed Linux on your Windows system, you have to edit the ssh configuration file /etc/ssh/sshd_config: change the fields PasswordAuthentication to yes, and UsePrivilegeSeparation to no.
    wsl sshd config edit

    You can do it usingvim, for example:
    sudo vim /etc/ssh/sshd_config

  7. Finally, you have to start your ssh server in the daemon mode. To do this, type the following command:
    sudo service ssh start
  8. Optionally, to learn which SSH daemon is running and where it is listening, type the following command:
    sudo $(sudo which sshd) -d:
    wsl which sshd

    Launch another cmd session, enter bash command to start a new linux session and then enter ssh 127.0.0.1:

    wsl bash another

    It works! So, close the cmd console.

Uff! All the preliminary steps are done. Pretty complicated, isn't it?

Configuring remote interpreter via WSL

Next, open the Settings dialog, and click the Project Interpreter page.

On this page, click gear icon next to Project Interpreter field, and choose Add Remote:

py remote interpreter choose

The Configure Python Remote Interpreter dialog box opens:

py configure remote interpreter wsl 1

Choose SSH credentials option! Here we'll use the SSH credentials for Linux entered in step 4.

Pay attention to the Python interpreter path field. Click the browse button (browseButton) and choose the required interpreter from the /usr/bin/ directory (/usr/bin/python3).

(Actually, you don't nave to choose the interpreter path from there - it's quite possible to have the virtual environment in one's home directory, for example...)

You have to configure the path mappings to let PyCharm know where to look for files inside of WSL. To do that, click the browse button next to the Path mappings field to open Edit Project Path Mappings dialog. Click add (or press Alt+Insert). Enter the local path (here C:). Next, enter the remote path. To do that, in our case click the browse button next to the added remote path, and choose the mapping :

wsl path mapping

or simply type the code /mnt/c/, because you might not have the directory /c inside /mnt

Running and debugging under WSL

Let's run and debug a script.

To run the script, right-click the editor background and choose Run <script name> on the context menu.

The script runs under the remote interpreter based on WSL.

wsl run

Let's do some debugging: you have to set a breakpoint and launch the debugger session.

  • Place a breakpoint. To do this, click the left gutter at a line where you want to place a breakpoint. See the Creating Line Breakpoints section.
  • To start the debugger session, right-click the editor background and choose Debug <script name>' on the context menu.

The script is debugged under the remote interpreter based on WSL.

wsl debug

Summary

Let's summarize what was done in this tutorial:

  • First, we prepared for installing WSL.
  • Second, we installed ssh, which is required for WSL.
  • In PyCharm, we configured the remote interpreter via WSL.
  • We ran and debugged our script.
Last modified: 29 November 2017

See Also