好吧,我已经设法产生了一个丑陋的工作黑客。您必须在 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”的调用,以避免可怕的管道处理错误(参见https://wpdev.uservoice.com/forums/266908-command-prompt-console-bash-on-ubuntu-on -windo/suggestions/13425768-allow-windows-programs-to-spawn-bash)