Run python script inside powershell script

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I want to run a python script from inside a powershell script. All of my searches lead me to questions like THIS. I tried using Invoke-Expression, but a command window briefly opens and closes, without the expected output from the python script.

I also tried using the '&' operator, based on THIS question. This time, the script starts, but throws a SyntaxError. There is no such error when I run the python script separately.

EDIT: This is the command I run:

& $python $MyPythonScript 

and the variables are set like this:

python="C:\Python33\python.exe" MyPythonScript="D:\MyPythonScript.py" 

This is the output:

File "D:\MyPythonScript.py", line 140 print "type=%s; mean = %s; finalVariance = %s; stdDev = %s; max(x) = %s; min(x) = %s; max(y) = %s; min(y) = %s; timeDiff = %s;" %(type, mean(distance), finalVariance, stdDev,    max(x), min(x), max(y), min(y), timeDiff)                                                                                                                               ^ SyntaxError: invalid syntax 

回答1:

That's a Python syntax error, not a PowerShell syntax error. Put the string interpolation in parentheses:

print ("type=%s; ... = %s;" % (type, ..., timeDiff))


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!