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

2024/11/19 19:35:52

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.

Answer

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

https://en.xdnf.cn/q/26405.html

Related Q&A

Whats the difference between nan, NaN and NAN

In numpy there are nan, NaN and NAN. Whats the sense of having all three, do they differ or any of these can be used interchangeably?

Python requests: URL base in Session

When using a Session, it seems you need to provide the full URL each time, e.g.session = requests.Session() session.get(http://myserver/getstuff) session.get(http://myserver/getstuff2)This gets a littl…

size of NumPy array

Is there an equivalent to the MATLAB size() command in Numpy? In MATLAB, >>> a = zeros(2,5)0 0 0 0 00 0 0 0 0 >>> size(a)2 5In Python, >>> a = zeros((2,5)) >>> a ar…

Feature Importance Chart in neural network using Keras in Python

I am using python(3.6) anaconda (64 bit) spyder (3.1.2). I already set a neural network model using keras (2.0.6) for a regression problem(one response, 10 variables). I was wondering how can I generat…

numpy.max or max ? Which one is faster?

In python, which one is faster ? numpy.max(), numpy.min()ormax(), min()My list/array length varies from 2 to 600. Which one should I use to save some run time ?

Nested Json to pandas DataFrame with specific format

I need to format the contents of a Json file in a certain format in a pandas DataFrame so that I can run pandassql to transform the data and run it through a scoring model. file = C:\scoring_model\json…

Iterating over dictionary items(), values(), keys() in Python 3

If I understand correctly, in Python 2, iter(d.keys()) was the same as d.iterkeys(). But now, d.keys() is a view, which is in between the list and the iterator. Whats the difference between a view and …

Is there a method that tells my program to quit?

For the "q" (quit) option in my program menu, I have the following code:elif choice == "q":print()That worked all right until I put it in an infinite loop, which kept printing blank…

Hiding Axis Labels

Im trying to hide the axis labels on the first subplot at 211. Id like to label the figure, not just a subplot (reference: "Isub Event Characteristics"). How can I control font properties lik…

Why does Python preemptively hang when trying to calculate a very large number?

Ive asked this question before about killing a process that uses too much memory, and Ive got most of a solution worked out.However, there is one problem: calculating massive numbers seems to be untouc…