[英]Unable to execute Python script from batch file
I'm trying to run a Python program from the command line so I created a batch file like so: 我试图从命令行运行Python程序,所以我创建了一个批处理文件,如下所示:
@py.exe C:\MyPythonScripts\program.py%*
@pause
The script should print a simple message, but when I type "program" in the Windows Run program cmd appears for a second and closes itself. 该脚本应该显示一条简单的消息,但是当我在Windows运行程序中键入“ program”时,cmd会显示一秒钟并自行关闭。 What am I doing wrong?. 我究竟做错了什么?。
This is the code: 这是代码:
#!python3
def printPicnic(itemsDict,leftWidth,rightWidth):
print('PICNIC ITEMS'.center(leftWidth + rightWidth,'-'))
for k,v in itemsDict.items():
print(k.ljust(leftWidth,'.') + str(v).rjust(rightWidth))
picnicItems = {'sandwiches':4, 'apples': 12, 'cups': 4, 'cookies':8000}
printPicnic(picnicItems,12,5)
printPicnic(picnicItems,20,6)
This is my PATH variable: 这是我的PATH变量:
> C:\Program Files (x86)\Common
> Files\Intel\Shared
> Files\cpp\bin\Intel64;C:\ProgramData\Oracle\Java\javapath;C:\Program
> Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files
> (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS
> Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program
> Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program
> Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files
> (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files
> (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files
> (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files
> (x86)\AMD\ATI.ACE\Core-Static;C:\Program Files
> (x86)\Skype\Phone\;C:\Program
> Files\Java\jdk1.8.0_121\bin;C:\MyPythonScripts;C:\Python34;C:\Program
> Files (x86)\Common Files\Intel\Shared
> Files\cpp\bin\Intel64;C:\ProgramData\Oracle\Java\javapath;C:\Program
> Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files
> (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS
> Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program
> Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program
> Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files
> (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files
> (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files
> (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files
> (x86)\AMD\ATI.ACE\Core-Static;C:\Program Files
> (x86)\Skype\Phone\;C:\Program
> Files\Java\jdk1.8.0_121\bin;C:\Python34\Scripts
Regarding your batch file, I would suggest perform these changes: 关于您的批处理文件,我建议执行以下更改:
You were missing a space inbetween the path
and the %*
, also enclose the path
in quotes to prevent it from breaking when spaces are in the path 您在path
和%*
之间丢失了一个空格,并且还将该path
用引号引起来,以防止path
中的空格破坏它
@echo off
py.exe "C:\MyPythonScripts\program.py" %1 %2 %3 %4 %5 %6 %7 %8 %9
pause
The problem in your batch file is a missing SPACE : 您的批处理文件中的问题是缺少空格 :
@rem THERE MUST BE A SPACE:
@rem |
@rem V
@py.exe C:\MyPythonScripts\program.py %*
@pause
In addition, I strongly recommend to put quotation marks around paths in order to avoid trouble with white-spaces and other spacial characters. 另外,强烈建议在路径两边加上引号,以免出现空格和其他空格字符。
You can do it using the following method : 您可以使用以下方法进行操作:
@ECHO OFF
setlocal
set PYTHONPATH=C:\Python35 (you version of python)
C:\MyPythonScripts\program.py %1<-- (you you have arg)
endlocal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.