All category에 해당하는 글 51

windows pip install 문제 해결

Study-programing/Python|2016. 9. 20. 15:34

windows 환경에서 python 을 쓰다 보니,

요상한 에러가 괴롭히는 문제가 발생했습니다... 


"UnicodeDecoderError: 'ascii' codec can't decode byte 0xc0 in position 0: ...."


분석해본 결과 원인은 cp949...

이놈의 python은 한글이 매번 골치네요...


문제가 되는 부분을 수정해서 해결하였습니다.


문제가 되는 코드는 "C:\python27\Lib\ntpath.py" 에 있는 join 이란 함수 입니다.

(2.7.12 기준 85번째 라인)




# Join two (or more) paths.
def join(path, *paths):
    """Join two or more pathname components, inserting "\\" as needed."""
    result_drive, result_path = splitdrive(path)
    for p in paths:
        p_drive, p_path = splitdrive(p)
        if p_path and p_path[0] in '\\/':
            # Second path is absolute
            if p_drive or not result_drive:
                result_drive = p_drive
            result_path = p_path
            continue
        elif p_drive and p_drive != result_drive:
            if p_drive.lower() != result_drive.lower():
                # Different drives => ignore the first path entirely
                result_drive = p_drive
                result_path = p_path
                continue
            # Same drive in different case
            result_drive = p_drive
        # Second path is relative to the first
        if result_path and result_path[-1] not in '\\/':
            result_path = result_path + '\\'
        result_path = result_path + p_path
    ## add separator between UNC and non-absolute path
    if (result_path and result_path[0] not in '\\/' and
        result_drive and result_drive[-1:] != ':'):
        return result_drive + sep + result_path
    return result_drive + result_path

붉게 표시한 두 부분이 문제가 되는데요,

아래와 같이 수정하여 해결 가능합니다.

(전체를 붙여넣기로 수정하셔도 됩니다.)




# Join two (or more) paths.
def join(path, *paths):
    """Join two or more pathname components, inserting "\\" as needed."""
    result_drive, result_path = splitdrive(path)
    for p in paths:
        p_drive, p_path = splitdrive(p)
        if p_path and p_path[0] in '\\/':
            # Second path is absolute
            if p_drive or not result_drive:
                result_drive = p_drive
            result_path = p_path
            continue
        elif p_drive and p_drive != result_drive:
            if p_drive.lower() != result_drive.lower():
                # Different drives => ignore the first path entirely
                result_drive = p_drive
                result_path = p_path
                continue
            # Same drive in different case
            result_drive = p_drive
        # Second path is relative to the first
        if result_path and result_path[-1] not in '\\/':
            result_path = result_path + '\\'
        result_path = result_path + p_path.decode("cp949")
    ## add separator between UNC and non-absolute path
    if (result_path and result_path[0] not in '\\/' and
        result_drive and result_drive[-1:] != ':'):
        return result_drive + sep + result_path
    return (result_drive + result_path).encode("utf-8")



#2016.10.10  위 내용은 PC의 이름이 한글이 때 해결 방법 이다. Username 이 한글일 때는 위 방법으로 해결되지 않는다. 

'Study-programing > Python' 카테고리의 다른 글

Windows 에서 pip가 안돼요 ㅜㅜ 해결법  (0) 2018.05.01
Code Like Pythonist PPT  (0) 2016.11.04

댓글()