우분투 파이썬 버전 관리

OpenJR·2025년 3월 5일

우분투 버전마다 디폴트 파이썬 버전이 정해져 있다. 예를 들어, 20.04는 3.8, 22.04는 3.10 이다.

물론 다른 파이썬 버전을 사용하려면 가장 좋은 법은 가상환경(아나콘다)를 사용하는 것이지만, 가끔....그러지 못할 때가 있다.

보통 파이썬 버전들이 설치된 경로는 아래와 같다.

nvidia@nvidia-JR:/usr/bin$ ls -al | grep python
lrwxrwxrwx 1 root root         23 Sep 11 16:02 pdb3.8 -> ../lib/python3.8/pdb.py
lrwxrwxrwx 1 root root         31 Mar 13  2020 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root root          9 Mar 13  2020 python3 -> python3.8
lrwxrwxrwx 1 root root         16 Mar 13  2020 python3-config -> python3.8-config
-rwxr-xr-x 1 root root    5478168 Sep 11 16:02 python3.8
lrwxrwxrwx 1 root root         33 Sep 11 16:02 python3.8-config -> x86_64-linux-gnu-python3.8-config
lrwxrwxrwx 1 root root         33 Mar 13  2020 x86_64-linux-gnu-python3-config -> x86_64-linux-gnu-python3.8-config
-rwxr-xr-x 1 root root       3241 Sep 11 16:02 x86_64-linux-gnu-python3.8-config

여기서 주목할 점은 python3가 python3.8을 심볼릭 링크로 물고 있다는 것이다.

버전 관리 설정법

sudo update-alternatives --config python으로 먼저 설정이 되어 있는지 확인하자. 아마 대부분 이 명령어가 모지 하는 사람들은 update-alternatives: error: no alternatives for python이런 에러가 뜰텐데 걱정 마라 정상이다.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.X 2

위 처럼 버전을 설정해주고 다시 sudo update-alternatives --config python로 보면 잘 보인다.

$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.6   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.6   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

위 처럼 변경만 해주면 버전이 바뀐다!

profile
Jacob

0개의 댓글