Windows上的closurebuilder.py错误:“无法解码字节0x8f”

时间:2013-06-06 05:33:01

标签: python windows utf-8 google-closure-compiler

我正在尝试在Windows上运行closurebuilder.py,无论我传递什么文件,我都会从Python中得到同样的错误:

  

python goog \ closure \ bin \ build \ closurebuilder.py --root = goog / closure / goog / --root = closurebuilder-example / --namespace =“myproject.start”

goog\closure\bin\build\closurebuilder.py: Scanning paths...
Traceback (most recent call last):
  File "goog\closure\bin\build\closurebuilder.py", line 262, in <module>
    main()
  File "goog\closure\bin\build\closurebuilder.py", line 200, in main
    sources.add(_PathSource(js_path))
  File "goog\closure\bin\build\closurebuilder.py", line 175, in __init__
    super(_PathSource, self).__init__(source.GetFileContents(path))
  File "C:\Users\Chris\Code\Plain\goog\closure\bin\build\source.py", line 119, in GetFileContents
    return fileobj.read()
  File "C:\System\Python\33\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 6857: character maps to <undefined>

无论我是在尝试构建一个简单的脚本,一个空白文件,还是一些更复杂的东西,都会出现这个错误,抱怨带有字节0x8f的UnicodeDecodeError。我正在使用Visual Studio 2012创建这些文件,并且我尝试使用其默认编码(Windows代码页1252)和UTF-8进行保存。

为了简单起见,我试图在https://developers.google.com/closure/library/docs/closurebuilder

中构建基本示例

如果页面改变了,那么我正在使用的HTML和脚本:

HTML:

<!doctype html>
<html>
  <head>
    <script src="../closure-library/closure/goog/base.js"></script>
    <script src="start.js"></script>
  </head>
  <body>
    <script>
      myproject.start();
    </script>
  </body>
</html>

JS:

goog.provide('myproject.start');

goog.require('goog.dom');

myproject.start = function() {
  var newDiv = goog.dom.createDom('h1', {'style': 'background-color:#EEE'},
    'Hello world!');
  goog.dom.appendChild(document.body, newDiv);
};

// Ensures the symbol will be visible after compiler renaming.
goog.exportSymbol('myproject.start', myproject.start);

可能导致这种情况的原因是什么?

2 个答案:

答案 0 :(得分:8)

文档似乎没有提到需要哪个版本的Python。 Python 3.x使用系统的默认编码自动将文本文件解码为Unicode,这可能导致您在Python 3.3上看到的错误。

试用Python 2.7。

答案 1 :(得分:0)

根据@ janne-karila的建议,降级到Python 2.7是我的解决方案的一部分但是,在进行了更改后,我开始收到以下错误:

WindowsError: [Error 2] The system cannot find the file specified

其解决方案是将java.exe添加到PATH。