分类 排错 下的文章

问题呈现
在windows环境下,从anaconda上下载的安装包,发现安装不成功。

allusers is False
Traceback (most recent call last):
  File "_nsis.py", line 176, in <module>
    main()
  File "_nsis.py", line 148, in main
    mk_menus(remove=False)
  File "_nsis.py", line 50, in mk_menus
    import menuinst
  File "E:\deve\anaconda\lib\site-packages\menuinst\__init__.py", line 23, in <m
odule>
    from .win32 import Menu, ShortCut
  File "E:\deve\anaconda\lib\site-packages\menuinst\win32.py", line 58, in <modu
le>
    "documents": get_folder_path(FOLDERID.Documents),
  File "E:\deve\anaconda\lib\site-packages\menuinst\knownfolders.py", line 215,
in get_folder_path
    return get_path(folder_id, user)
  File "E:\deve\anaconda\lib\site-packages\menuinst\knownfolders.py", line 206,
in get_path
    path = path.decode(codec)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-6: ordin
al not in range(128)

原因分析
检查源代码后发现是因为创建菜单时,有一处路径带有中文“我的文档”引起的。
以下为出错地方的python代码:
1.png
Python在进行编码转换时,会将 unicode 作为中间编码,但 unicode 最大只有128,所以这里当尝试将 ascii 编码字符串转换成中间编码(unicode)时由于超出了其范围,就报出了如上错误。

解决方案
解决方法1:在anacondaLibsite-packagesmenuinstknownfolders.py文件开头添加如下代码

import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
  reload(sys)
  sys.setdefaultencoding(defaultencoding)

解决方法2:在anacondaLibsite-packages目录下添加一个sitecustomize.py文件,文件内容如下:

import sys
sys.setdefaultencoding('utf-8')