python代码在linux和windows运行不一致问题

问题描述: Windows10,anaconda,python3.6

相同的python代码,在linux运行时提示:

/usr/local/lib/python2.7/dist-packages/torch/serialization.py:255:UnicodeWarning: Unicode unequal comparison failed to convert both arguments toUnicode - interpreting them as being unequal

  iforiginal_source != current_source:

/usr/local/lib/python2.7/dist-packages/torch/serialization.py:286:SourceChangeWarning: source code of class 'torch.nn.modules.conv.Conv2d' haschanged. you can retrieve the original source code by accessing the object'ssource attribute or set `torch.nn.Module.dump_patches = True` and use the patchtool to revert the changes.

然后把Linux运行得到的 .t7文件放到Windows下运行,报错:

Traceback (most recent call last):
  File "main.py", line 66, in <module>
    checkpoint = torch.load('./checkpoint/ckpt.t7')
  File "D:\Anaconda3\lib\site-packages\torch\serialization.py", line 231, in load
    return _load(f, map_location, pickle_module)
  File "D:\Anaconda3\lib\site-packages\torch\serialization.py", line 379, in _load
    result = unpickler.load()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 875: ordinal not in range(128)

坎坷解决路途:

在终端输入:

python -c "import sys; print(sys.getdefaultencoding())"

在windows显示 utf-8, 而在Linux显示 ascii。因为python3.6默认的是utf-8编码,Linux默认的python2.7是ascii编码。

首先我在Linux系统上的py文件头加上:

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

代码来自:http://www.cnblogs.com/CasonChan/p/4669799.html

还是没有解决。

于是,我只能换python版本。

最终可行解决方案:
1、在Linux 自己目录下安装 miniconda:https://conda.io/miniconda.html
2、使用 pip 安装自己需要的工具包。别用conda
3、愉快地使用python3.6 on Linux吧。

猜你喜欢

转载自blog.csdn.net/ciyiquan5963/article/details/78604963