본문 바로가기

프로그래밍/Python

[요약]파이썬(Python) 가상환경 설정 및 패키지 설치

[요약]파이썬(Python) 가상환경 설정 및 패키지 설치


1. 가상환경의 필요성

  용도별 / 프로젝트별 사용된 파이선 버전 + 프레임워크가 각각 다른 환경을 독립적으로 구축(패키지 설치/관리) 하여 프로젝트 구축 및 배포 관리를 쉽게 할 수 있도록 한다.

예)

  A프로젝트 =  Python 3.5 + Django + Web

  B프로젝트 = Python 2.x + Numpy, Tensorflow, + Data Analysis

  C프로젝트 = Python 3.6 + PyQT5 + GUI App


2. 가상환경 설정 및 패키지 설치


   가. Command Prompt 창 열기(cmd.exe, Windows)

C:\python -m venv D:\Python_basic


# 해당 폴더에 include, Lib, Scripts 폴더가 생김

# Python_basic 폴더가 하나의 가상 환경으로 설정됨


  나. 가상환경 Activation / Deactivation

D:\Python_basic\cd Scripts

D:\Python_basic\Scripts\activate

(python_basic) D:\Python_basic\Scripts>   # 이렇게 나오면 가상환경에 활성화 됨

가상환경 비활성화 

(python_basic) D:\Python_basic\Scripts> deactivate

D:\Python_basic\cd Scripts    # 다시 Command 모드로 빠져 나온다.



  다. 패키지 설치 테스트

       - Simplejson 설치 ( https://github.com/simplejson/simplejson )

# 패키지중 Simplejson 검색

(python_basic) D:\Python_basic\Scripts> pip search simplejson

# 설치

(python_basic) D:\Python_basic\Scripts> pip install simplejson

Collecting simplejson

  Downloading simplejson-3.17.2-cp37-cp37m-win_amd64.whl (73 kB)

     |████████████████████████████████| 73 kB 278 kB/s

Installing collected packages: simplejson

Successfully installed simplejson-3.17.2

WARNING: You are using pip version 20.1.1; however, version 20.2.2 is available.

You should consider upgrading via the 'd:\python_basic\scripts\python.exe -m pip install --upgrade pip' command.


# 가상환경에 설치된 패키지 목록 보기

(python_basic) D:\Python_basic\Scripts> pip list

Package    Version

---------- -------

pip        20.1.1

setuptools 47.1.0

simplejson 3.17.2

WARNING: You are using pip version 20.1.1; however, version 20.2.2 is available.

You should consider upgrading via the 'd:\python_basic\scripts\python.exe -m pip install --upgrade pip' command.


# 가상환경에 설치된 패키지 삭제하기

(python_basic) D:\Python_basic\Scripts> pip uninstall simplejson

# 가상환경에 설치된 패키지 업그레이드

(python_basic) D:\Python_basic\Scripts> pip install --upgrade simplejson

# 가상환경에 설치된 패키지 정보보기

(python_basic) D:\Python_basic\Scripts> pip show simplejson



  라. 가상환경 내에서 Visual Studio Code 실행

(python_basic) D:\Python_basic\Scripts> code