Getting PyCharm to recognize python on the windows linux subsystem (bash on windows)

35,650

Solution 1

Using PyCharm Professional with WSL Python on Win10 Starting SSH

PyCharm can only be configured to use WSL Python as a Remote Interpreter (this is due to lack of other public API).

  • Install Win10 build 14361 or later. You also can upgrade your current Insider Preview.
  • Install wsl (something like lxrun /install` && lxrun /update )
  • Run bash.exe
  • Update to latest version sudo apt-get update && sudo apt-get upgrade
  • Open /etc/ssh/sshd_config
    • Enable password authentication (unless you want to use public keys). Open /etc/ssh/sshd_config , and set PasswordAuthentication yes.
    • Since chroot is not implemented in WSL (yet), you also need to set UsePrivilegeSeparation no
    • Save and close it
  • Type sudo $(sudo which sshd) -d to run OpenSSH on foreground (it is much easier for debug). You should see something like Server listening on 0.0.0.0 port 22
  • From another bash.exe session try ssh 127.0.0.1
  • If you see message about ECDSA finger print, answer y . You should see password prompt. If you see it, then your server works correctly.

  • Turn it off with CTRL+C, and start server in daemon mode (sudo service ssh start). Looks like upstart is broken on current WSL, so you would need to run bash.exe, start sshd and keep console window opened since WSL stops when the last client disconnects. You may create wsl_ssh.bat file like bash.exe -c "sudo service ssh start &&& sleep 999d" and use it to launch ssh.

Configuring PyCharm PyCharm should be configured to use WSL as a remote interpreter but without deployment, since each drive on Windows is mapped to an appropriate folder in /mnt/<DRIVE_NAME> in WSL. So, you only need to configure the mapping. For remote interpreters, see configuration-remote-python-interpreters . You should use 127.0.0.1 as hostname, and login and password you entered after first lxrun /install. You also should set C:\ to /mnt/c/ in your mappings. See the video from the previous post.

Author: Ilya Kazakevich
14 Jun 2016, 17:20

https://youtrack.jetbrains.com/issue/PY-19129#comment=27-1469350

Solution 2

I tried working with most solutions but the main issue is that I can't downgrade OpenSSH on Windows to something below 7.5 as is recommended by JetBrains.

Luckily they have solved this issue for us! I have downloaded the Early Access version of Pycharm 2018.3

https://blog.jetbrains.com/pycharm/2018/10/pycharm-2018-3-eap-7/

enter image description here

This is however only available in the professional version.

Solution 3

I'd like to add the answer of bmjjr by stating that this is only available with the PyCharm Professional Edition. The Remote Interpreter feature is not available with the Community Edition, as I sadly found out:

Supported only in Professional Edition

  • Cython
  • Django
  • AppEngine
  • Flask
  • Jinja2
  • Mako
  • web2py
  • Pyramid
  • Profiler
  • SQLAlchemy
  • Diagrams
  • Remote interpreters, remote debugging, Vagrant, Docker
  • Duplicate code detection
  • Code coverage
  • .po files support
  • BDD support
  • Profiler integration
  • Thread Concurrency Visualization

https://www.jetbrains.com/pycharm/features/editions_comparison_matrix.html

Solution 4

Well, I've managed to produce an ugly working hack. You'll have to install python-setuptools and pip manually under the Linux subsystem. Be sure to use the pip version provided by PyCharm, you'll find it at a path similar to:
C:\Program Files (x86)\JetBrains\PyCharm 2016.1.2\helpers\pip-7.1.0.tar.gz

Then setup the following script as "python.bat" under "c:\Python" and point PyCharm to it as an interpreter:

@echo off
@setlocal enableextensions enabledelayedexpansion
:: Requiers pip and setuptools to already be installed on linux subsystem
Set "Pattern= "
Set "Replace=\ "
Set "cdrive=C:"
Set "linpath=/mnt/c"
:: Iterate over arguments, convert paths to linux format and concatinate

set argCount=0
for %%x in (%*) do (
    set /A argCount+=1
    set arg=%%x
    :: Backward slash to forward slash
    SET arg=!arg:\=/!
    :: C drive to /mnt/c/ - default linux subsystem mount point
    SET arg=!arg:%cdrive%=%linpath%!
    :: Space to escaped space
    SET arg=!arg:%Pattern%=%Replace%!
    :: Parethesis to escaped parenteses
    SET arg=!arg:^(=\^(!
    SET arg=!arg:^)=\^)%!
    :: Deqoute voodoo via http://ss64.com/nt/syntax-dequote.html
    SET arg=###!arg!###
    SET arg=!arg:"###=!
    SET arg=!arg:###"=!
    SET arg=!arg:###=!
    if "!args!"=="" (
        set args=!arg!
    ) else (
        set args=!args! !arg!
    )
)
:: Dump it to the interpreter
:: Output is piped inside the Linux subsys, as windows piping for bash seems broken
START "Terrible hack to avoid pipe error" /W /MIN C:\Windows\System32\bash.exe -c "python !args! > /mnt/c/Python/test" 
:: Output resulr from piped file
type c:\Python\test
:: echo !args!
EXIT /B > NUL

Forgive the terrible coding style, as I've never really developed windows batch files before.

You may have to tweak the directory structure to match your system. Also note that the output of any python script called by Python.bat is piped to a temp file under the linux subsystem, then typed back out under windows. For some reason, piping the output of bash.exe via windows causes errors.

Hope this helps.

UPDATE: Wrapped the call to "bash" with "START" in order to avoid terrible pipe handling errors (c.f. https://wpdev.uservoice.com/forums/266908-command-prompt-console-bash-on-ubuntu-on-windo/suggestions/13425768-allow-windows-programs-to-spawn-bash)

Solution 5

Supported via remote int. See last comment: https://youtrack.jetbrains.com/issue/PY-19129

Share:
35,650

Related videos on Youtube

Boaz Arad
Author by

Boaz Arad

Updated on July 09, 2022

Comments

  • Boaz Arad
    Boaz Arad almost 2 years

    While running Linux versions of python, pip etc. "natively" on windows is amazing, I'd like to do so using a proper IDE. Since SSHD compatibility has not been implemented yet, I'm trying get PyCharm to recognize Linux python as a local interpreter.

    After installing the Windows Linux subsystem, typing

    bash -c python
    

    from the windows command line will drop you into a python shell.

    bash -c "echo \"print 'hello world'\" | python" 
    

    works as well, producing "hello world" as output in the windows shell!

    I'm trying to wrap this up as a .bat file and present it to PyCharm as a local interpreter, i.e.

    python.bat:

    C:\Windows\System32\bash.exe -c "echo %1 | python" 
    

    But I keep getting "the sdk seems invalid" for any variation I try. Since I'm not sure exactly what PyCharm is doing to "validate" the SDK, this is hard to overcome.

    • 123
      123 about 8 years
      Did you set up the paths for python ?
    • Boaz Arad
      Boaz Arad about 8 years
      Please note that this is not a typical python on windows install. I am trying to access python within the windows linux subsystem (see en.wikipedia.org/wiki/Windows_Subsystem_for_Linux). This cannot be solved via paths.
    • Ophir Yoktan
      Ophir Yoktan about 8 years
      There's an open feature request for this, you can follow it (and vote): youtrack.jetbrains.com/issue/PY-19129
    • Boaz Arad
      Boaz Arad about 8 years
      Thanks, I'm guessing that this will be solved the minute MS implement the features missing for SSHD to work, then the "remote interpreter" setting should work.
  • breandan
    breandan about 8 years
    If you tried this and received the error: "Windows cannot find 'C:\Windows\System32\bash.exe'. Make sure you typed the name correctly and then try again.", make sure that you are running PyCharm as Administrator. Navigate to the installation directory (something like C:\Program Files (x86)\JetBrains\PyCharm 2016.1.2\bin or C:\Users\<USER>\AppData\Local\JetBrains\Toolbox\apps\PyCharm‌​-P\ch-0\145.1504.1\b‌​in if you are using the JetBrains toolbox app), right click on pycharm.exe or pycharm64.exe, select Properties, Compatibility, then check "Run this program as an administrator".
  • antitoxic
    antitoxic almost 8 years
    Please include an extract from the link. It's better stackoverflow experience and prevents broken links drama.
  • user1946989
    user1946989 over 6 years
    I am getting an error message "invalid python interpreter" from pycharm while trying to add the bat
  • Tyberius
    Tyberius almost 6 years
    I can't find "UsePrivilegeSeparation" in my sshd_config file. Should I just add it or is something wrong with my config file?
  • Seanny123
    Seanny123 over 5 years
    @Tyberius UsePrivelegeSeperation has been deprecated in newer version OpenSSH. This is probably what's happening to you.
  • Tyberius
    Tyberius over 5 years
    @Seanny123 yeah I wound up asking JetBrains directly and got the same answer. On the plus side, I think the most recent build of Pycharm will include native support for accessing WSL.
  • xApple
    xApple over 5 years
    I've tried lots of different paths, and all I'm getting is env: ‘/Python27/python.bat’: No such file or directory