首页 文章

让PyCharm识别windows linux子系统上的python(bash on windows)

提问于
浏览
35

虽然在Windows上运行Linux版本的python,pip等“本机”是惊人的,但我想使用适当的IDE这样做 . 由于尚未实现SSHD兼容性,我正在尝试让PyCharm将Linux python识别为本地解释器 .

安装Windows Linux子系统后,键入

bash -c python

从Windows命令行将您放入python shell .

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

也可以,在windows shell中输出“hello world”作为输出!

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

python.bat:

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

但我一直在尝试“sdk似乎无效” . 由于我不确定PyCharm正在做什么来“验证”SDK,因此很难克服 .

5 回答

  • 4

    我想通过声明这是 only available with the PyCharm Professional Edition 来添加bmjjr的答案 . 社区版不提供远程解释器功能,我遗憾地发现:

    Supported only in Professional Edition

    • Cython

    • Django

    • AppEngine

    • 烧瓶

    • Jinja2

    • Mako

    • web2py

    • 金字塔

    • Profiler

    • SQLAlchemy

    • 图表

    • Remote interpreters, remote debugging, Vagrant, Docker

    • 重复代码发现

    • 代码覆盖率

    • .po文件支持

    • BDD支持

    • Profiler集成

    • 线程并发可视化

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

  • 29

    Using PyCharm with WSL Python on Win10 启动SSH

    PyCharm只能配置为使用WSL Python作为远程解释器(这是由于缺少其他公共API) .

    • 安装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) . 看起来upstart在当前WSL上被破坏了,所以你需要运行bash.exe,启动sshd并保持控制台窗口打开,因为WSL在最后一个客户端断开连接时停止 . 您可以创建wsl_ssh.bat文件,如bash.exe -c“sudo service ssh start &&& sleep 999d”并使用它来启动ssh .

    配置PyCharm PyCharm应该配置为使用WSL作为远程解释器,但是没有部署,因为Windows上的每个驱动器都映射到WSL中/ mnt /中的相应文件夹 . 因此,您只需配置映射 . 有关远程解释程序,请参阅https://www.jetbrains.com/help/pycharm/2016.1/configuring-remote-python-interpreters.html . 您应该使用127.0.0.1作为主机名,以及在首次lxrun / install之后输入的登录名和密码 . 你也应该在你的映射中设置C:到/ mnt / c / . 观看上一篇文章中的视频 .

    作者:Ilya Kazakevich
    2016年6月14日,17:20

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

  • 4

    好吧,我必须在Linux子系统下手动安装python-setuptools和pip . 一定要使用PyCharm提供的pip版本,你会发现它的路径类似于:
    C:\ Program Files(x86)\ JetBrains \ PyCharm 2016.1.2 \ helpers \ pip-7.1.0.tar.gz

    然后在“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
    

    原谅可怕的编码风格,因为我之前从未真正开发过Windows批处理文件 .

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

    希望这可以帮助 .

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

  • 1

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

  • 3

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

    幸运的是他们已经为我们解决了这个问题!我已经下载了Pycharm 2018.3的Early Access版本

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

    enter image description here

相关问题