파이썬 가상환경 만들기
🔹 방법 1: 가상환경(virtual environment) 사용하기
1. venv 설치 (한번만 필요)
sudo apt install python3-venv
2. 가상환경 만들기
python3 -m venv venv
그러면 venv/ 폴더가 생겨.
여기에 파이썬과 pip가 분리된 상태로 들어감.
3. 가상환경 활성화
source venv/bin/activate
활성화되면 프롬프트 앞에 (venv) 표시가 생김
4. 이제 pip 설치 가능!
pip install -r requirements.txt
이제 에러 없이 설치돼야 해!
✅ 5. 작업 끝났으면 비활성화
deactivate
가상환경 빠져나오게 돼.
필요하면 .gitignore에 venv/ 추가해두면 좋음
======================================================
아래 에러는 최근 우분투 버전 (예: Ubuntu 22.04 이상)에서
pip3 install을 시스템 전체에 쓰는 걸 막아두는 보안 기능 때문이다
핵심 메시지는:
❌ “This environment is externally managed”
즉, 시스템 Python 환경은 apt로 관리하니까 pip으로 막 설치하지 말라는 경고임
==> 가상환경을 사용하라는 얘기
pip install시 에러메세지
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.
See /usr/share/doc/python3.12/README.venv for more information.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing –break-system-packages.
hint: See PEP 668 for the detailed specification.