【问题标题】:Weird python encoding error in Vim with makeVim中奇怪的python编码错误与make
【发布时间】:2014-03-16 20:25:24
【问题描述】:

我有文件test.py 的内容:

print u'\u0410'

和Makefile:

test:
    python test.py

当我用 Vim :!python test.py 写代码时,我明白了

А

Press ENTER or type command to continue

但是当我写 :make test 我得到了

python test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    print u'\u0410'
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0410' in position 0: ordinal not in range(128)
make: *** [test] Error 1

Press ENTER or type command to continue

终端中的make test 没问题。

有人知道为什么会这样吗?你能重现吗?或者它只是我的设置?

【问题讨论】:

  • 我可以复制它。我不知道为什么会这样。但它在 shell 中工作方式不同的原因是 :make test 是通过 vim 的内部命令调用 make 。 (所以 vim 可能搞砸了)。这与在 shell 中运行 make 的 :!make test 不同。 (注意!)。好像和这个stackoverflow.com/questions/20923663/…一样的问题
  • 在 makefile 中使用 PYTHONIOENCODING=utf-8 python test.py 可以解决问题。这取自之前链接问题的答案。

标签: python vim encoding makefile


【解决方案1】:

变化:

print u'\u0410'

到:

print u'\u0410'.encode('utf-8')

这里发生的事情是 vim 在 make(从 makeprg 设置)之后添加 2&gt;&amp;1 | tee(从 shellpipe 设置)。所以你的test.py 脚​​本实际上被重定向了。尝试运行$ test.py &gt; /tmp/foo,你会得到同样的错误。解决方案:将您的输出编码为合理的编码,例如utf-8。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-17
    • 2012-12-27
    • 2016-08-15
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多