프로그래밍 언어/python(파이썬)

CentOS에서 Python 버전 변경 → update-alternatives 사용

hippo 데브옵스 2022. 6. 28. 01:08

Update-alternatives로 파이썬 버전 등록 및 변경

  • 파이썬 버전 수정하기 전에 이미 등록된 것이 있는지 확인 필요
  • update-alternatives --config python 을 입력하였을 때 아무 값도 나오지 않으면 아무것도 등록된 것이 없다는 의미
    $ update-alternatives --config python

  • update-alternatives --install [symbolic link path] python [real path] number 명령어는 실행파일을 등록하는 명령어

  • 아래의 명령어 3개를 입력하면 2.7 버전과 3.6버전, 3.7버전이 update-alternatives에 등록

  • 파이썬 2.7과 3.6, 3.7이 설치되어 있어야만 update-alternatives에 등록이 가능

  • python의 설치 위치가 아래의 명령어와 다르다면 path를 변경해야만 실행이 가능

    $ update-alternatives --install /bin/python python /bin/python2.7 1
    $ update-alternatives --install /bin/python python /bin/python3.6 2
    $ update-alternatives --install /bin/python python /bin/python3.7 3

  • update-alternatives에 등록한 후 update-alternatives --config python 명령어를 입력하면 등록한 파이썬 버전을 선택하는 메뉴가 출력

    $ update-alternatives --config python
    
    There are 3 programs which provide 'python'.
    
      Selection    Command
    -----------------------------------------------
         1           /bin/python2.7
         2           /bin/python3.6
    *+ 3           /bin/python3.7
    
    Enter to keep the current selection[+], or type selection number:

  • 원하는 메뉴의 번호를 입력한 후 파이썬 버전을 확인하면 파이썬 버전을 확인 가능 → 3을 입력

  • 파이썬의 link를 따라가보면 alternatives 명령어로 설정 → alternatives가 /bin/python3.7을 가리킴

    # 선정된 python의 소프트링크 확인
    $ ls -al /bin/python
    lrwxrwxrwx 1 root root 24 Jan 18 23:50 /bin/python -> /etc/alternatives/python
    
    $ python -V
    Python 3.7.3



python3 업데이트 후 YUM 실행 시 SyntaxError: invalid syntax 에러

  • CentOS 7을 사용하던 도중에 파이썬(Python)을 2.7에서 3.7 버전으로 업그레이드를 할 필요有

  • 파이썬 3.7.3 버전을 설치한 후 /usr/bin에 있던 python 링크 또한 2.7에서 3.7 버전으로 변경

  • 아래와 같이 명령어를 실행하면 현재 파이썬 버전이 3.7이라는 것을 확인 가능

    $ python -V
    Python 3.7.3

  • 문제 발생(거의 항상 발생) → 파이썬을 3.7로 교체한 후 yum이 실행 X

    $ yum update
    File "/usr/bin/yum", line 30
      except KeyboardInterrupt, e:
                              ^
    SyntaxError: invalid syntax

  • yum이 파이썬 2.7 버전을 사용하고 있었는데 파이썬을 3.7으로 업데이트하면서 문법적 문제가 발생

  • 해결방법은 간단 → yum을 파이썬 2.7 버전에서 실행되도록 수정만 하면 됨

  • /usr/bin/yum 스크립트 내용 확인

    $ cat /usr/bin/yum
    #!/usr/bin/python
    import sys
    try:
       import yum
    except ImportError:
       print >> sys.stderr, """\
    There was a problem importing one of the Python modules
    required to run yum. The error leading to this problem was:
    
      %s
    
    Please install a package which provides this module, or
    verify that the module is installed correctly.
    
    It's possible that the above module doesn't match the
    current version of Python, which is:
    %s
    
    If you cannot solve this problem yourself, please go to
    the yum faq at:
     http://yum.baseurl.org/wiki/Faq
    
    """ % (sys.exc_value, sys.version)
       sys.exit(1)
    
    sys.path.insert(0, '/usr/share/yum-cli')
    try:
       import yummain
       yummain.user_main(sys.argv[1:], exit_code=True)
    except KeyboardInterrupt, e:
       print >> sys.stderr, "\n\nExiting on user cancel."
       sys.exit(1)

  • 가장 첫 줄 #!/usr/bin/python을 통해 python 명령어가 실행됨을 확인 가능

  • python 명령어에 대한 링크를 python3.6으로 변경해 놓았기 때문에 yum이 파이썬 3.6 환경으로 실행된 것

  • /usr/bin/yum 파일은 python2로 설정되어있기에 가장 첫 줄을 "#!/usr/bin/python2"로 변경 필요

  • vi 에디터를 이용해서 첫 번째 줄을 아래와 같이 변경

    $ vi /usr/bin/yum
    #!/usr/bin/python2
    [...아래 내용 생략]

  • /usr/bin/yum 파일 변경 이후 추가로 /usr/libexec/urlgrabber-ext-down 파일 변경 필요

  • 파이썬 2.7 버전에서 실행될 수 있도록 가장 첫 줄 #!/usr/bin/python2 변경

    $ vi /usr/libexec/urlgrabber-ext-down
    #!/usr/bin/python2
    [...아래 내용 생략]

  • yum 명령을 실행 테스트 → 정상적으로 동작

    $ yum update -y
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * base: mirror.kakao.com
    * epel: ftp.iij.ad.jp
    * extras: mirror.kakao.com
    * updates: mirror.navercorp.com
    Resolving Dependencies
    --> Running transaction check
    ---> Package bind-export-libs.x86_64 32:9.11.4-26.P2.el7_9.2 will be updated
    ---> Package bind-export-libs.x86_64 32:9.11.4-26.P2.el7_9.3 will be an update



변경한 python을 사용할 때 오류 발생

  • 파이썬에서 다음과 같은 오류(ERROR: Python headers are missing in /usr/include/python3.6m)가 발생하면 python3-dev를 통해 문제를 해결
    $ yum install python3-dev