名称错误:名称“缓冲区”未使用基于 Ant 的框架批处理文件定义
我正在使用 python 脚本执行基于 Ant 的框架批处理文件(Helium.bat)
subprocess.Popen('hlm '+commands, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)< /code>
但是,脚本在执行 .bat 文件时总是会停止并显示以下错误:
import codecs
File "C:\Python25\lib\codecs.py", line 1007, in <module>
strict_errors = lookup_error("strict")
File "C:\Python25\lib\codecs.py", line 1007, in <module>
strict_errors = lookup_error("strict")
File "C:\Python25\lib\encodings\__init__.py", line 31, in <module>
import codecs, types
File "C:\Python25\lib\types.py", line 36, in <module>
BufferType = buffer
NameError: name 'buffer' is not defined
如果我直接在命令行上执行 .bat,则不会有任何问题。
I'm using a python script to execute an Ant based framework batch file(Helium.bat)
subprocess.Popen('hlm '+commands, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
However the script will always stop and display the following error when it executes the .bat file:
import codecs
File "C:\Python25\lib\codecs.py", line 1007, in <module>
strict_errors = lookup_error("strict")
File "C:\Python25\lib\codecs.py", line 1007, in <module>
strict_errors = lookup_error("strict")
File "C:\Python25\lib\encodings\__init__.py", line 31, in <module>
import codecs, types
File "C:\Python25\lib\types.py", line 36, in <module>
BufferType = buffer
NameError: name 'buffer' is not defined
If I execute the .bat directly on command line, there will not be any issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为至少部分问题在于您如何执行批处理文件。尝试一下:
这会将不同的参数传递给
Popen
- 不同之处在于此版本删除了批处理文件中不需要的shell=True
,设置了stderr=subprocess.STDOUT
将stdout
重定向到 stdout 将避免丢失任何错误消息的同一位置,并添加universal_newlines=True
以使输出更具可读性。另一个区别是它读取并打印 Popen 进程的输出,这将有效地使运行批处理文件的 Python 脚本等到它完成执行后再继续——我怀疑这很重要。
I think at least part of the problem is how you're executing the batch file. Give this a try:
This pass different arguments to
Popen
-- the difference are this version removes theshell=True
which isn't needed on a batch file, setsstderr=subprocess.STDOUT
which redirectsstdout
to the same place stdout is going to to avoid missing any error messages, and adds auniversal_newlines=True
to make the output more readable.Another difference is it reads and prints the output from the
Popen
process which will effectively make the Python script running the batch file wait until it's finished executing before continuing on -- which I suspect is important.