让 PyC​​harm 在 windows linux 子系统上识别 python(windows 上的 bash)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37000166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:39:51  来源:igfitidea点击:

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

pythonpycharmwindows-subsystem-for-linux

提问by Boaz Arad

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.

虽然在 Windows 上“本机”运行 Linux 版本的 python、pip 等非常棒,但我想使用合适的 IDE 来这样做。由于尚未实现 SSHD 兼容性,我正在尝试让 PyC​​harm 将 Linux python 识别为本地解释器。

After installing the Windows Linux subsystem, typing

安装 Windows Linux 子系统后,键入

bash -c python

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

从 Windows 命令行会将您放入 python shell。

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

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

也能工作,在 windows shell 中生成“hello world”作为输出!

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

我正在尝试将其包装为 .bat 文件并将其作为本地解释器呈现给 PyCharm,即

python.bat:

蟒蛇.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.

但是对于我尝试的任何变体,我不断收到“sdk 似乎无效”的消息。由于我不确定 PyCharm 究竟在做什么来“验证”SDK,因此这很难克服。

回答by Bob Jordan

Using PyCharm Professional with WSL Python on Win10Starting SSH

在 Win10 上使用 PyCharm Professional 和 WSL Python启动 SSH

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

PyCharm 只能配置为使用 WSL Python 作为远程解释器(这是由于缺少其他公共 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 chrootis not implemented in WSL (yet), you also need to set UsePrivilegeSeparation no
    • Save and close it
  • Type sudo $(sudo which sshd) -dto 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.exesession 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 sshdand keep console window opened since WSL stops when the last client disconnects. You may create wsl_ssh.batfile like bash.exe -c "sudo service ssh start &&& sleep 999d"and use it to launch ssh.

  • 安装 Win10 build 14361 或更高版本。您还可以升级当前的 Insider Preview。
  • 安装 wsl(类似lxrun /install` && lxrun /update
  • bash.exe
  • 更新到最新版本 sudo apt-get update && sudo apt-get upgrade
  • 打开 /etc/ssh/sshd_config
    • 启用密码验证(除非您想使用公钥)。打开/etc/ssh/sshd_config,然后设置PasswordAuthentication yes
    • 由于chroot尚未在 WSL 中实现(尚未),因此您还需要设置UsePrivilegeSeparation no
    • 保存并关闭它
  • 键入sudo $(sudo which sshd) -d以在前台运行 OpenSSH(调试起来要容易得多)。你应该看到类似的东西Server listening on 0.0.0.0 port 22
  • 从另一个bash.exe会话尝试ssh 127.0.0.1
  • 如果您看到有关 ECDSA 指纹的消息,请回答y。您应该会看到密码提示。如果你看到它,那么你的服务器工作正常。

  • CTRL+关闭它C,并以守护进程模式 ( sudo service ssh start)启动服务器。看起来新贵在当前的 WSL 上坏了,所以你需要运行bash.exe,启动sshd并保持控制台窗口打开,因为 WSL 在最后一个客户端断开连接时停止。您可以创建wsl_ssh.bat类似的文件bash.exe -c "sudo service ssh start &&& sleep 999d"并使用它来启动 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.1as 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.

配置 PyCharm PyCharm 应配置为使用 WSL 作为远程解释器但无需部署,因为 Windows 上的每个驱动器都映射到/mnt/<DRIVE_NAME>WSL 中的适当文件夹。因此,您只需要配置映射。对于远程解释器,请参阅configuration-remote-python-interpreters。您应该使用127.0.0.1作为主机名,以及您在 first 之后输入的登录名和密码lxrun /install。您还应该在映射中设置C:\/mnt/c/。请参阅上一篇文章中的视频。

Author: Ilya Kazakevich
14 Jun 2016, 17:20

作者:伊利亚·卡扎克维奇
2016 年 6 月 14 日,17:20

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

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

回答by bitwhys

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.

我尝试使用大多数解决方案,但主要问题是我无法按照 JetBrains 的建议将 Windows 上的 OpenSSH 降级到 7.5 以下。

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

幸运的是他们已经为我们解决了这个问题!我已经下载了 Pycharm 2018.3 的抢先体验版

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

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.

然而,这仅在专业版中可用。

回答by jonathan

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:

我想通过说明这仅适用于 PyCharm Professional Edition来添加 bmjjr 的答案。社区版不提供远程解释器功能,我遗憾地发现:

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
  • 赛通
  • 姜戈
  • 应用引擎
  • 烧瓶
  • Jinja2
  • 真子
  • web2py
  • 金字塔
  • 探查器
  • SQLAlchemy
  • 图表
  • 远程解释器、远程调试、Vagrant、Docker
  • 重复代码检测
  • 代码覆盖率
  • .po 文件支持
  • BDD 支持
  • 探查器集成
  • 线程并发可视化

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

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

回答by Boaz Arad

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

好吧,我已经设法制作了一个丑陋的工作黑客。您必须在 Linux 子系统下手动安装 python-setuptools 和 pip。请务必使用 PyCharm 提供的 pip 版本,您会在类似于以下路径找到它:
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:

然后将以下脚本设置为“c:\Python”下的“python.bat”,并将 PyCharm 作为解释器指向它:

@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.

请原谅糟糕的编码风格,因为我以前从未真正开发过 Windows 批处理文件。

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.

您可能需要调整目录结构以匹配您的系统。另请注意,Python.bat 调用的任何 Python 脚本的输出都通过管道传输到 linux 子系统下的临时文件,然后在 windows 下重新输入。出于某种原因,通过 windows 管道 bash.exe 的输出会导致错误。

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)

更新:用“START”包装对“bash”的调用以避免可怕的管道处理错误(参见https://wpdev.uservoice.com/forums/266908-command-prompt-console-bash-on-ubuntu-on -windo/suggestions/13425768-allow-windows-programs-to-spawn-bash)

回答by user996142

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

通过远程 int 支持。查看最后一条评论:https: //youtrack.jetbrains.com/issue/PY-19129

回答by Islam Mansour

Configure remote interpreter via WSL?#

通过 WSL 配置远程解释器?#

  1. Open the Add Python Interpreter dialog by either way:

    • When you're in the Editor, the most convenient way is to use the Python Interpreter widget in the Status bar. Click the widget and select Add Interpreter ...

    • If you are in the Settings/Preferences dialog (Ctrl+Alt+S), select Project | Project Interpreter. Click the The Configure project interpretericon and select Add.

  2. In the left-hand pane of the dialog, click WSL.

    Use Anaconda:

    .\rootfs\home\$UserName$\anaconda\envs\$Conda_Env$\bin\python3

    Use Pyhton: Adding a WSL interpreter

  3. Select the Linux distribution and specify the path to the python executable in the selected Linux distribution.

  1. 通过以下任一方式打开“添加 Python 解释器”对话框:

    • 当您在编辑器中时,最方便的方法是使用状态栏中的 Python 解释器小部件。单击小部件并选择添加解释器...

    • 如果您在设置/首选项对话框 (Ctrl+Alt+S) 中,请选择项目 | 项目口译员。单击配置项目解释器图标并选择添加。

  2. 在对话框的左侧窗格中,单击 WSL。

    使用蟒蛇:

    .\rootfs\home\$UserName$\anaconda\envs\$Conda_Env$\bin\python3

    使用 Pyhton: 添加 WSL 解释器

  3. 选择 Linux 发行版并指定所选 Linux 发行版中 python 可执行文件的路径。

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

完成后,新的解释器将添加到您的项目中,并且将设置默认的mnt 映射

Newly added WSL interpreter

新添加的 WSL 解释器

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.

请注意,使用 WSL 您无法创建虚拟环境:您安装的所有软件包都将添加到相应的系统解释器中。您将被要求输入您的 sudo 密码。

Entering your sudo password

输入您的 sudo 密码

Reference: JetBrains

参考: JetBrains

回答by user2673238

I used the normal remote ssh intepreter "old method" discussed in this link here:

我使用了此链接中讨论的普通远程 ssh 解释器“旧方法” :

I used this guide: critically I was getting an authentication error which was resolved with this:

我使用了本指南:关键是我收到了一个身份验证错误,错误已解决: