Python Popen failing to use proper encoding in Windows PowerShell Python Popen failing to use proper encoding in Windows PowerShell powershell powershell

Python Popen failing to use proper encoding in Windows PowerShell


Try to change the encoding to cp1252. Popen in Windows wants shell commands encoded as cp1252. This seems like a bug, and it also seems fixed in Python 3.X through the subprocess module: http://docs.python.org/library/subprocess.html

import subprocesssubprocess.Popen(["hg", "--cwd", self.path, "--encoding", "UTF-8"] + list(args), stdout=PIPE, stderr=PIPE)

update:

Your problem maybe can be solved through smart_str function of Django module.

Use this code:

from django.utils.encoding import smart_str, smart_unicode# the cmd should contain sthe string with the commsnd that you want to executesmart_cmd = smart_str(cmd)subprocess.Popen(smart_cmd)

You can find information on how to install Django on Windows here.You can first install pip and then you can install Django by startinga command shell with administrator privileges and run this command:

pip install Django

This will install Django in your Python installation's site-packages directory.


After using from __future__ import unicode_literals I started getting the same error but in a different part of the code:

out, err = [x.decode("utf-8") for x in  proc.communicate()]

Gave the error

UnicodeDecodeError: 'utf8' codec cant decode byte 0xe3 in position 33 ....

Indeed, x was a byte string with \xe3 (which is ã in cp1252) included. So instead of using x.decode('utf-8'), I used x.decode('windows-1252') and that gave me no bugs. To support any kind of encoding, I ended up using x.decode(sys.stdout.encoding) instead. Problem solved.

And that was in Python 3.2.2 with the Windows 7 Starter computer, but Python 2.7 on the same computer also worked normally.