1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@BiX: Very unlikely that codecs.py has a SyntaxError in it -- must be whatever file has the line of code with the no viable alternative at input '""' in it. You're likely getting the glade ImportError because it needs to be re-installed following your Python upgraded (it's not a standard Python library module).
These errors will not occur if I just use the batch file in command line instead of executing it from the python script. Sadly I've tried re-installing glade many times and it still didn't work =/
@BiX: I didn't realize that was what you were doing. Update your question to show how you're executing the batch file -- sounds like that might be at least part of the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.