Printing color output - working with the console, but not with the script

I have a strange problem that I cannot rely on. There is a program that I use (and from time to time contributing) that has colored console output. Everything worked fine until I reinstalled Windows. Now I can not get the color output.

This is a script that is used for coloring.

I managed to narrow the problem down to a more or less simple situation, but I have no idea what is wrong.

This is a console prompt that works as expected (the string test is printed in red):

 Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path.insert(0, r'c:\bin\SV\tea\src') >>> from tea.console.color import cprint, Color >>> cprint('test\n', Color.red) test >>> 

But when I run the following script with the same version of python, I get the test output, but not in red (no color, only the default console color):

 import sys sys.path.insert(0, r'c:\bin\SV\tea\src') from tea.console.color import cprint, Color cprint('test\n', Color.red) 
  • The same setup worked before I reinstalled my system.
  • I checked the environment variables in interactive mode and the script are the same.
  • I tried this on the standard Windows command prompt and console, a program that I usually use.
  • The OS in question is Windows 8, and it was also used in Windows 8 before reinstalling.
  • The same code with the same settings works on the computer at work (Windows 7).
  • I have Python 2.7 and Python 3.3 installed (as before). I tried to run the script with a direct call to the python interpreter ( c:\Python27\python.exe ) or using py -2 , but this does not help.
  • IPython and mercurial paint the output properly.

Any ideas what I can try to make this work?

Edit

It may not have been clear, but the script I use for output is the data output in the corresponding link. Here again: https://bitbucket.org/alefnula/tea/src/dc14009a19d66f92463549332a321b29c71d47b8/src/tea/console/color.py?at=default

+4
source share
1 answer

I found a problem and solution.

I believe the problem is due to an error in the x64 ctypes module. I installed Python 2.7 x64 and with this version of the following line (from the script that I am related to the question):

 ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, code) 

returns error code 6 with the description The handle is invalid . After some research, I decided that the problem might be the x64 version of python, so I installed the 32-bit version and everything works as expected.

Since this solves my problem, and I do not have time for a deeper analysis, I will leave it to this, I just want to give some permission for the question.

+4
source

All Articles