본문 바로가기
Python

[Python] venv : Python Virtual Envrionment

by ds31x 2023. 6. 30.

Python 3의 경우, standard library로 venv를 virtual environment를 위해 제공하고 있음.

이를 통해 프로젝트마다 적절한 package들을 사용할 수 있게 됨.


Create a virtual environment

python -m venv 환경이름
  • 위의 명령어를 통해 환경이름이라는 subdirectory가 만들어짐.
  • 해당 directory 밑에 virtual environment를 위한 file들이 위치하고 있음.

Activate the virtual environment

# linux 계열
source 환경이름/bin/activate
  • 생성된 director의 subdirectory bin 밑에 있는 `activate script를 수행하면 만들어진 가상환경이 활성화됨.
  • source 대신 .를 사용하기도 함.
# windows : cmd prompt
.\환경이름\scripts\activate.bat
  • Windows의 command prompt의 경우, scripts subdirectory 밑에 있는 activate.bat를 수행하여 활성화.
# windows : power shell
.\환경이름\scripts\Activate.ps1
  • Windows의 power shell의 경우임.
  • 위의 .ps 스크립트가 동작하지 않을 경우, 관리자 모드로 Set-ExecutionPolicy RemoteSigned를 입력하고 Y를 선택하고 다시 수행.

Install packages in the virtual environment

활성화된 상태에서 pip를 이용하여 패키지 설치 및 제거를 수행.

  • 환경이름/lib/python3.x/site-packages 라는 subdirectory에 설치되는 패키지들이 위치하게 됨.
  • host system과 다른 virtual environment에 영향을 주지 않음.

Deactivate the virtual environment

Virtual environment를 빠져나오는 경우, 다음의 명령어를 이용함.

deactivate

Remove the virtual environment

생성된 환경이름 디렉토리를 째로 삭제하면 됨.

728x90

'Python' 카테고리의 다른 글

[Python] binary file : write and read  (0) 2023.07.04
[Python] Text File : read and write  (0) 2023.07.04
[Python] file : open and close  (0) 2023.07.04
[Python] Regular Expression : 표현식 기초 및요약  (0) 2023.07.03
[Python] pip 사용법  (0) 2023.06.30