在 Jupyter Notebook 中使用 sys.path.append() 添加路径时,有时会遇到如下错误:

Cell In[33], line 2
  sys.path.append('C:\Users\Administrator\Desktop\.ipynb_checkpoints')
                                                                        ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

这是因为路径中的反斜杠被解释为 Unicode 转义字符,导致编译器无法识别。可以将路径中的反斜杠改为正斜杠,或者在路径前面加上 r,表示路径是 raw string,不会被解释为 Unicode 转义字符。例如:

 sys.path.append(r'C:\Users\Administrator\Desktop\.ipynb_checkpoints')

或者

 sys.path.append('C:/Users/Administrator/Desktop/.ipynb_checkpoints')
解决 Jupyter Notebook 中 'unicodeescape' 解码错误

原文地址: http://www.cveoy.top/t/topic/jN4O 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录