Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

utf-8 character in user path prevents module from being imported

The following error message is shown as I try to import nltk module

I actually have the 0xb3 (ł) character in my username, but what bothers me is that other modules like re, codecs, etc. are imported successfully.

Is it possible to solve it on Python side (without changing my username system-wide)?

File "C:\Python27\lib\ntpath.py", line 310, in expanduser
    return userhome + path[i:]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb3 in position 13: ordinal not in range(128)
like image 240
pkrysiak Avatar asked Dec 03 '14 11:12

pkrysiak


1 Answers

As in the ntpath.py file there is not an encoding for unicode username , you need to add the following command in expanduser(path) function in ntpath.py :

if isinstance(path, unicode):
    userhome = unicode(userhome,'unicode-escape').encode('utf8')

so expanduser function must be like the following :

def expanduser(path):
    """Expand ~ and ~user constructs.

    If user or $HOME is unknown, do nothing."""
    if isinstance(path, bytes):
        tilde = b'~'
    else:
        tilde = '~'
    if not path.startswith(tilde):
        return path
    i, n = 1, len(path)
    while i < n and path[i] not in _get_bothseps(path):
        i += 1

    if 'HOME' in os.environ:
        userhome = os.environ['HOME']
    elif 'USERPROFILE' in os.environ:
        userhome = os.environ['USERPROFILE']
    elif not 'HOMEPATH' in os.environ:
        return path
    else:
        try:
            drive = os.environ['HOMEDRIVE']
        except KeyError:
            drive = ''
        userhome = join(drive, os.environ['HOMEPATH'])

    if isinstance(path, bytes):
        userhome = userhome.encode(sys.getfilesystemencoding())

    if isinstance(path, unicode):
        userhome = unicode(userhome,'unicode-escape').encode('utf8')

    if i != 1: #~user
        userhome = join(dirname(userhome), path[1:i])

    return userhome + path[i:]
like image 197
Mazdak Avatar answered Oct 05 '22 11:10

Mazdak