名称错误:名称“缓冲区”未使用基于 Ant 的框架批处理文件定义

发布于 2024-10-07 07:41:38 字数 777 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菊凝晚露 2024-10-14 07:41:38

我认为至少部分问题在于您如何执行批处理文件。尝试一下:

# execute the batch file as a separate process and echo its output
Popen_kwargs = { 'stdout': subprocess.PIPE, 'stderr': subprocess.STDOUT,
                 'universal_newlines': True }
with subprocess.Popen('hlm '+commands, **Popen_kwargs).stdout as output:
    for line in output:
        print line,

这会将不同的参数传递给 Popen - 不同之处在于此版本删除了批处理文件中不需要的 shell=True,设置了 stderr=subprocess.STDOUTstdout 重定向到 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:

# execute the batch file as a separate process and echo its output
Popen_kwargs = { 'stdout': subprocess.PIPE, 'stderr': subprocess.STDOUT,
                 'universal_newlines': True }
with subprocess.Popen('hlm '+commands, **Popen_kwargs).stdout as output:
    for line in output:
        print line,

This pass different arguments to Popen -- the difference are this version removes the shell=True which isn't needed on a batch file, sets stderr=subprocess.STDOUT which redirects stdout to the same place stdout is going to to avoid missing any error messages, and adds a universal_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文