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

2024-05-15 08:42:54 发布

您现在位置:Python中文网/ 问答频道 /正文

虽然在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,所以这很难克服。


Tags: pipecho版本bashhelloworldlinuxwindows
3条回答

将PyCharm与WSL Python一起用于Win10 启动SSH

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

  • 安装Win10 build 14361或更高版本。您还可以升级当前的内部预览。
  • 安装wsl(类似lxrun/Install`&;lxrun/update)
  • 运行bash.exe
  • 更新至最新版本sudo apt get Update&;sudo apt get upgrade
  • 打开/etc/ssh/sshd_配置
    • 启用密码身份验证(除非要使用公钥)。打开/etc/ssh/sshd_config,并设置PasswordAuthentication yes。
    • 由于chroot还没有在WSL中实现,因此还需要设置UsePrivilegeSeparation no
    • 保存并关闭
  • 键入sudo$(sudo which sshd)-d在前台运行OpenSSH(调试起来容易得多)。您应该看到类似“服务器监听0.0.0.0端口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:`to/mnt/C/`。请参阅上一篇文章的视频。

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

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

我尝试过使用大多数解决方案,但主要问题是,我不能将Windows上的OpenSSH降级到JetBrains推荐的7.5以下。

幸好他们帮我们解决了这个问题! 我已经下载了Pycharm 2018.3的早期访问版本

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

enter image description here

好吧,我成功地制造了一个丑陋的工作黑客。 您必须在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下键入back out。出于某种原因,通过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

相关问题 更多 >