Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import numpy throws error: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape

I have installed pyzo and miniconda under Windows 10 and installed numpy and matplotlib using conda install. But when I'm trying to run

import numpy as np 
import matplotlib.pyplot as plt 

I'm getting this error:

Traceback (most recent call last):
  File "<tmp 1>", line 3, in <module>
    import numpy
  File "c:\users\jakub\miniconda3\lib\site-packages\numpy\__init__.py", line 165, in <module>
    from numpy.__config__ import show as show_config
  File "c:\users\jakub\miniconda3\lib\site-packages\numpy\__config__.py", line 5
    lapack_mkl_info={'libraries': ['mkl_lapack95_lp64', 'mkl_core_dll', 'mkl_intel_lp64_dll', 'mkl_intel_thread_dll'], 'define_macros': [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)], 'include_dirs': ['c:\users\jakub\miniconda3\\Library\\include'], 'library_dirs': ['c:\users\jakub\miniconda3\\Library\\lib']}
                                                                                                                                                                                                       ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape

I do not have any non-standard character either in my code nor in the directory structure...I have read many posts referring to similar problems with UTF-8 but this is different as it happens during the initial import.

like image 689
Jakub Avatar asked Jun 28 '16 19:06

Jakub


People also ask

How do I fix Unicodeescape error in Python?

The Python "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position" occurs when we have an unescaped backslash character in a path. To solve the error, prefix the path with r to mark it as a raw string, e.g. r'C:\Users\Bob\Desktop\example. txt' .

What does unicode error Unicodeescape codec can't decode bytes in position 2/3 truncated \Uxxxxxxxx escape mean?

The SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape occurs if you are trying to access a file path and provide the path as a regular string.

How do you fix Unicodeescape codec Cannot decode bytes in position 2/3 truncated <UNK>Uxxxxxxxx escape?

To Solve SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape Error You just need to put r before your path string Just like this pandas. read_csv(r”C:\Users\ssc\Desktop\account_summery. csv”) OR Just Use double quotes and forwardslash character.

What is Unicodeescape?

A unicode escape sequence is a backslash followed by the letter 'u' followed by four hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence with the value specified by the four digits. For example, ”\u0041“ matches the target sequence ”A“ when the ASCII character encoding is used.


2 Answers

When conda installs packages, it replaces the prefix, to make things relocatable. Unfortunately, it does not intelligently escape backslashes, so on Windows, these unescaped backslashes lead to the error you see.

In recent versions of conda, we use forward slashes in prefix replacement, and this issue goes away. If you can update conda, go do that. If not, numpy has prefixes in the following files:

"Lib/site-packages/numpy/distutils/site.cfg"
"Scripts/f2py.py"
"Lib/site-packages/numpy/config.py"
"Lib/site-packages/numpy/distutils/config.py"

check the latter 3 especially, and replace any non-escaped backslashes ( \ ) with either escaped ones ( \\ ) or forward slashes

like image 89
msarahan Avatar answered Sep 20 '22 02:09

msarahan


So, for people having trouble in

import numpy

using Windows 10 + Anaconda:

I replaced all single '\' to double '\\' in

\Lib\site-packages\numpy\__config__.py

I could import numpy after that.

like image 39
Master Uv Puppetz Avatar answered Sep 24 '22 02:09

Master Uv Puppetz