PyCharm 2021.2 Help

Using SSH to created a WSL-based Remote Interpreter

You can use Windows Subsystem for Linux (WSL) to work with a Python interpreter available in your Linux distribution.

Fulfill the preliminary steps

  1. Ensure that you have downloaded and installed Python on your computer.

  2. Click the Windows button in the lower-left corner of the screen and start typing System Information. To ensure that your system works well with WSL, upgrade your Windows to the latest available version.

  3. Install the Windows Subsystem for Linux and initialize your Linux distribution as described in the WSL Installation Guide.

  4. If your Linux distribution doesn't come with rsync, you need to install it:

    sudo apt install rsync

Configure remote interpreter via WSL

  1. Do one of the following:

    • Click the Python Interpreter selector and choose Add Interpreter.

    • Press Ctrl+Alt+S to open the project Settings/Preferences and go to Project <project name> | Python Interpreter. Then click the The Configure project interpreter icon and select Add.

      Selecting the Add command to create a new Python interpreter
  2. In the left-hand pane of the dialog, click WSL.

    Adding a WSL interpreter
  3. Select the Linux distribution and specify the path to the python executable in the selected Linux distribution. Typically, you should be looking for wsl.exe but you can specify any non default WSL distro.

Once done, the new interpreter will be added to your project, and the default mnt mappings will be set.

Newly added WSL interpreter

Note that with WSL you cannot create virtual environments: all packages you install will be added to the corresponding system interpreter. You will be asked to enter your sudo password.

Entering your sudo password

PyCharm allows you to use custom Linux distributions run on WSL. PyCharm auto-detects such a distribution and adds it to the Linux Distribution list:

Configure Python Interpreter

Create a WSL-based interpreter for a directory in \\wsl$

If you have any directory with some Python files in \\wsl$, you can open this directory in PyCharm. The IDE will create a project and suggest creating a WSL-based interpreter.

  1. Select File | Open from the main menu. Choose a directory in \\wsl$.

    Open a directory in wsl
  2. If PyCharm cannot discover any pre-configured WSL interpreter, you will see the banner prompting to discover Python in the Ubuntu installation:

    Discover WSL interpreter

    Click Search for Python in Ubuntu to find a Python executable and create a WSL-based interpreter.

  3. Once the interpreter is created, PyCharm notifies you.

    Configured WSL interpreter

Configure Terminal in WSL

  1. To run Terminal in WSL configuration, open project Settings/Preference (Ctrl+Alt+S) and go to Tools | Terminal.

  2. Type wsl.exe in the Shell path field and click Ok.

    Terminal settings
  3. Switch to the Terminal tool window and type any command to inspect the output.

    Terminal in WSL configurations

Configure system settings to enable debugging

Perform the following steps to ensure that the debugger works properly:

  1. Run the Windows PowerShell as administrator.

    Restore firewalls to default
  2. Execute the following command to allow connections using WSL:

    New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow
  3. Then execute the command to renew the firewall rules:

    Get-NetFirewallRule | where DisplayName -ILike "*PyCharm*" | Remove-NetFirewallRule
  4. Now start the debugger session. When the Windows Security Alert popup appears, select the Public networks checkbox.

    Allow public networks
Last modified: 21 May 2021