apt python error (debpython)
VM Host에서 apt 로 특정 패키지를 설치하던 중 발생한 이슈를 해결하는 과정입니다.
root@BD1-L-KUBESPAWNER-MASTER-001:/tmp# apt-get install wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
wget is already the newest version (1.17.1-1ubuntu1.5).
Suggested packages:
apport-gtk | apport-kde
The following packages will be upgraded:
apport
1 upgraded, 0 newly installed, 0 to remove and 325 not upgraded.
7 not fully installed or removed.
Need to get 0 B/3,289 kB of archives.
After this operation, 12.3 kB of additional disk space will be used.
(Reading database ... 79488 files and directories currently installed.)
Preparing to unpack .../apport_2.20.1-0ubuntu2.30_all.deb ...
Traceback (most recent call last):
File "/usr/bin/pyclean", line 32, in <module>
from debpython.namespace import add_namespace_files
File "/usr/share/python/debpython/namespace.py", line 120
except (IOError, OSError), e:
^
SyntaxError: invalid syntax
dpkg: warning: subprocess old pre-removal script returned error exit status 1
dpkg: trying script from the new package instead ...
Traceback (most recent call last):
File "/usr/bin/pyclean", line 32, in <module>
from debpython.namespace import add_namespace_files
File "/usr/share/python/debpython/namespace.py", line 120
except (IOError, OSError), e:
^
SyntaxError: invalid syntax
dpkg: error processing archive /var/cache/apt/archives/apport_2.20.1-0ubuntu2.30_all.deb (--unpack):
subprocess new pre-removal script returned error exit status 1
Traceback (most recent call last):
File "/usr/bin/pycompile", line 35, in <module>
from debpython.version import SUPPORTED, debsorted, vrepr, \
File "/usr/share/python/debpython/version.py", line 24, in <module>
from ConfigParser import SafeConfigParser
ModuleNotFoundError: No module named 'ConfigParser'
dpkg: error while cleaning up:
subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
/var/cache/apt/archives/apport_2.20.1-0ubuntu2.30_all.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
apt install을 진행하는데, 위와 같은 에러를 직면했다.
apt install을 하는데 왜 Python 에러가 발생할까?
의존성 문제, 파일 충돌, 패키지 충돌, 시스템 업그레이드, 패키지 손상 문제 등 여러 문제가 있는데,
위 상황에서는 의존성 문제로 보인다.
The following packages will be upgraded: apport
apport라는 리눅스 패키지(오류 및 예외 정보를 수집하고 보고하는 도구)를 업그레이드하라고 하는데 이 패키지가 python 오류 수집하는데에서 발생하는 것으로 보인다.
따라서, 나는 Default 파이썬 버전으로 3.7을 쓰고 있었기 때문에 SyntaxError 부분을 python3에 맞게 수정해서 해결하려고 시도했다.
$ vi /usr/share/python/debpython/namespace.py
>> except (IOError, OSError), e: 를 except Exception as e: 로 수정
$ vi /usr/share/python/debpython/version.py
>> ConfigParser 를 configparser로 수정
/usr/share/python/debpython 디렉토리는 Debian 계열의 리눅스 배포판(예: Ubuntu)에서 사용되는 debpython 도구와 관련된 파일과 스크립트를 포함하는 디렉토리이다.
이 디렉토리에는 Python 패키지와 관련된 여러 스크립트, 설정 파일 및 도우미 도구가 있다.
ConfigParser의 경우에는 python2에서 사용하는 패키지로 확인된다.
이로써, 현재 debpython에서는 python2에 대한 파이썬 스크립트를 포함하고 있는 것으로 보인다.
Traceback (most recent call last):
File "/usr/bin/pycompile", line 38, in <module>
from debpython.namespace import add_namespace_files
File "/usr/share/python/debpython/namespace.py", line 28, in <module>
from debpython.pydist import PUBLIC_DIR_RE
File "/usr/share/python/debpython/pydist.py", line 27, in <module>
from string import maketrans
ImportError: cannot import name 'maketrans' from 'string' (/usr/lib/python3.7/string.py)
이 후 발생한 에러 역시 python3에서 패키지를 불러온다. (Python3 부터는 str.maketrans()로 선언해야한다.)
이제부터, debpython 의 파이썬 스크립트들을 python2 기반으로 작성되어있는데 왜 python3에서 모듈을 불러올까 의문이 들었다.
(이 깨달음이 들기전에는 /usr/bin/python 들을 /usr/local/bin 하위로 복사도 해보고 get-pip.py로 pip 모듈을 재설치해보고도 했다…)
/usr/share/python/debpython/ 하위의 python script들은 python2 기반으로 작성되었는데 시스템레벨의 python은 python3로 설치하려할때, 시스템 레벨의 Python이 Python 3로 업그레이드되면서 Python 3와의 호환성 문제로 인해 에러가 발생할 수 있다.
일반적으로 Python 2에서 Python 3로 마이그레이션하는 작업이 필요하며, 이 작업은 스크립트의 코드를 Python 3에서 작동하도록 수정해야하는데, 나는 가장 첫번째로 스크립트의 코드를 변경하는 작업을 했음에도 불구하고 에러가 계속발생했기 때문에, python2로 잘 바라볼 수 있도록 하는 방법을 찾았고, 해결한 부분은 아래와 같다.
해결
Debian 계열의 리눅스 배포판에서 제공되는 Python의 최소 설치 버전 패키지인 python-minimal을 재설치한다.
재설치함으로써, 시스템의 기본 Python 인터프리터가 다시 해당 버전으로 설정된다.
root@BD1-L-KUBESPAWNER-MASTER-001:/tmp# apt-get install --reinstall python-minimal
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
apport libpython-stdlib python
Suggested packages:
apport-gtk | apport-kde python-doc python-tk
The following packages will be upgraded:
apport libpython-stdlib python python-minimal
4 upgraded, 0 newly installed, 0 to remove and 322 not upgraded.
7 not fully installed or removed.
Need to get 173 kB/3,462 kB of archives.
After this operation, 12.3 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://kr.archive.ubuntu.com/ubuntu xenial-updates/main amd64 python-minimal amd64 2.7.12-1~16.04 [28.1 kB]
Get:2 http://kr.archive.ubuntu.com/ubuntu xenial-updates/main amd64 python amd64 2.7.12-1~16.04 [137 kB]
Get:3 http://kr.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython-stdlib amd64 2.7.12-1~16.04 [7,768 B]
Fetched 173 kB in 1s (105 kB/s)
(Reading database ... 79488 files and directories currently installed.)
Preparing to unpack .../apport_2.20.1-0ubuntu2.30_all.deb ...
/var/lib/dpkg/info/apport.prerm: 19: /var/lib/dpkg/info/apport.prerm: pyclean: not found
dpkg: warning: subprocess old pre-removal script returned error exit status 127
dpkg: trying script from the new package instead ...
/var/lib/dpkg/tmp.ci/prerm: 19: /var/lib/dpkg/tmp.ci/prerm: pyclean: not found
dpkg: error processing archive /var/cache/apt/archives/apport_2.20.1-0ubuntu2.30_all.deb (--unpack):
subprocess new pre-removal script returned error exit status 127
/var/lib/dpkg/info/apport.postinst: 13: /var/lib/dpkg/info/apport.postinst: pycompile: not found
pyclean과 pycompile이 없다고 뜨고 , 잘보면 apport 때문에 설치가 안된것으로 확인이 된다. 따라서, apport를 삭제하기 다시 설치해준다.
root@BD1-L-KUBESPAWNER-MASTER-001:/tmp# dpkg --remove --force-remove-reinstreq apport
dpkg: warning: overriding problem because --force enabled:
dpkg: warning: package is in a very bad inconsistent state; you should
reinstall it before attempting a removal
(Reading database ... 79488 files and directories currently installed.)
Removing apport (2.20.1-0ubuntu2.10) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for hicolor-icon-theme (0.15-0ubuntu1) ...
root@BD1-L-KUBESPAWNER-MASTER-001:/tmp# apt-get install apport
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
python : PreDepends: python-minimal (= 2.7.11-1) but 2.7.12-1~16.04 is to be installed
python-minimal을 2.7.11-1 버전으로 다시 설치해준다.
salt-common 관련 에러가 발생하면, apt-get install salt-common 해주고, apt autoremove로 불필요한 패키지를 삭제하여 마무리한다.
결과
root@BD1-L-KUBESPAWNER-MASTER-001:/tmp# apt-get install wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
wget is already the newest version (1.17.1-1ubuntu1.5).
0 upgraded, 0 newly installed, 0 to remove and 324 not upgraded.
사실 그동안 wget은 설치완료였고, 그 후의 Python 관련 에러라서 wget is already the newest version이라고 나오는것임.