라즈베리파이 파이썬 3.9 설치 - lajeubelipai paisseon 3.9 seolchi

설치 환경

이전에는 기본 파이썬 버전을 원하는 버전의 파이썬으로 변경해서 사용했지만, 이제부터는 가상환경을 만들어서 사용하려고 합니다.

라즈베리파이에 파이썬 3.7이 아닌 다른 버전의 파이썬을 설치했습니다.

이전에 라즈베리파이4에 파이썬을 설치하다가 이것저것 꼬여서 개판이 되었다고 글을 적었었는데.. 중간에 잘못된 것을 어떻게 해결하려고 하다가 더 꼬여버리는 바람에 sd카드를 포맷하고 새

redfox.tistory.com

가상환경 생성에 사용하는 파이썬 버전은 다음과 같습니다.

$ python3.9 -V Python 3.9.2

가상환경 생성

가상환경 생성은 별 거 없습니다. 터미널에 다음 명령어를 입력해줍니다.

"REDFOX.TISTORY"란 디렉토리에 가상환경을 만들라는 명령어입니다.

$ python3.9 -m venv REDFOX.TISTORY

만약 해당 명령어 실행 후 에러가 나타난다면 venv 패키지를 설치한 다음 다시 생성을 시도합니다.

$ sudo apt install python3.9-venv $ python3.9 -m venv REDFOX.TISTORY

생성 완료

가상환경 생성은 이제 전부입니다.

명령 실행 후 에러가 나타나지 않았다면 "ls" 명령을 통해 "REDFOX.TISTORY" 디렉토리가 생성된 것을 확인할 수 있습니다.

$ ls Bookshelf Desktop Documents Downloads Music Pictures Public Templates REDFOX.TISTORY Videos

가상환경 사용하기

가상환경을 사용하기 위해서는 만들어진 디렉토리에서 activate를 싱행해주면 됩니다.

성공적으로 작동한 경우 다음과 같이 터미널에 괄호로 디렉토리 이름이 표시되는 것을 확인할 수 있습니다.

$ cd REDFOX.TISTORY $ ls bin include lib lib64 pyvenv.cfg share $ cd bin $ ls activate activate.csh activate.fish Activate.ps1 easy_install easy_install-3.9 pip pip3 pip3.9 python python3 python3.9 $ . activate (REDFOX.TISTORY) pi@raspberrypi:~/REDFOX.TISTORY/bin $

가상환경 종료하기

가상환경을 종료하고 싶다면 deactivate 명령어를 입력해주면 종료됩니다.

(REDFOX.TISTORY) pi@raspberrypi:~/VENV3.9/bin $ deactivate pi@raspberrypi:~/VENV3.9/bin $

단축 프로그램 만들기

경로 진입이 귀찮다면 다음과 같이 명령어를 입력한 파일을 하나 생성하면 그 과정을 줄일 수 있습니다.

# 파일을 만들 경로로 이동 $ cd {파일이 위치할 경로} # REDFOX라는 이름으로 파일 생성 $ nano REDFOX # 다음 내용을 입력한 다음 "Ctrl + X", "Y", 엔터 키를 순서대로 눌러 파일 저장 . ~/REDFOX>TISTORY/bin/activate # 파일 실행 $ . REDFOX # 가상환경이 실행된 것을 확인할 수 있다. (REDFOX.TISTORY) pi@raspberrypi:~ $

Python and Raspberry Pi OS are made for each other, as without Python, you will face issues while accessing your Raspberry Pi terminal or running other applications that require Python. There are other advantages of having Python on Raspberry Pi as well, such as ease of use, versatility, and freedom to access several Python libraries that are included in the Raspberry Pi libraries list.

Since replacing the older Python version with the new one is a complex task because you won’t be able to remove the previous Python version as it will result in system failure. So, to help you out in this situation, we present this article to provide your guidelines on how you can update Python on Raspberry Pi.

To update the Python version on Raspberry Pi, you must need to follow the step-by-step instruction given below:

Step 1: Install Python on Raspberry Pi

First, head toward the official Python website to check the latest version of Python. At the time of writing, the latest version of Python is 3.9.9; thus, we have used the following command to install this version on Raspberry Pi.

$ wget //www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz

Step 2: Extract File Contents

Next, you must extract the contents of the Python-3.9.9.tgz file using the following command:

$ tar -zxvf Python-3.9.9.tgz

 
The above command will store the file in a directory with the name “Python-3.9.9”.

Step 3: Configure Python Latest Version on Raspberry Pi

After successfully extracting the contents, it’s now time to configure Python on Raspberry Pi and for this purpose, head towards the Python directory using the following command:

 


Next, apply the following command to configure Python on Raspberry Pi:

$ ./configure --enable-optimizations

 


After the configuration, run the following command to build the installation packages for the latest version of Python on Raspberry Pi:

Step 4: Update the Python Version on Raspberry Pi

To make the latest version of Python the default one, you must need to remove the previous Python version and replace it with the new one.

Since the Python files are stored inside the directory “/usr/bin”, you must need to visit the directory using the following command:

 


At the current location, remove the previous Python directory using the following command:

 


Next, link the latest version of Python placed inside the directory “usr/local/bin” using the following command:

$ sudo ln -s /usr/local/bin/python3.9 python

 


This will update the Python version on your Raspberry Pi device and you can confirm it by executing the following command:

 

Conclusion

Updating the Python will help you speed up your system and application’s performance because the newer version comes with new features compared to the previous version. Now that you have learned how to update the Python version on Raspberry Pi through the above guidelines, it’s time to take out your Raspberry Pi device, open the terminal and perform the step-by-step instructions mentioned above to update the Python version successfully.

About the author

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.

Toplist

최신 우편물

태그