title | date | categories | tags | keywords | ||||
---|---|---|---|---|---|---|---|---|
Python 가상 환경 만들기 - `venv` |
2022-07-30 08:37:00 +0900 |
|
|
|
모든 코드를 스스로 작성한다면 가상 환경이 그다지 필요 없을지도 모릅니다. 하지만 요즘의 소프트웨어 개발은 그렇게 되지 않죠. 다른 사람들과 협업하기 위해서는 내 코드와 다른 사람의 코드, 그리고 그 코드들이 실행되는 환경을 분리해야 하는 상황이 생깁니다.
설령 혼자 작업하더라도 GitHub 같은 곳에 공개된 다른 사람들의 코드를 사용해야 하는 경우가 많죠.
구체적으로 Python 가상 환경이 필요한 경우는 다음과 같습니다.
- 시스템의 Python 설정과 다른 환경을 만들고 싶을때
- 여러 개의 Python 프로젝트를 진행하는데, 프로젝트마다 다른 환경을 구성하고 싶을 때
사실 Python 가상환경을 만들 수 있는 도구에는 다양한 것들이 있지만, 이 글에서는 Python 3.3 부터 표준으로 포함된 venv
를 사용하겠습니다.
각 가상 환경은 각 환경의 site directory와 Python 실행파일/바이너리와, Python package를 설치할 수 있는 디렉토리를 가집니다. 따라서 시스템에 설치되어 있는 것과는 다른 버전의 Python package들을 사용할 수 있게 됩니다.
Python을 오래 전 부터 사용해 오신 분들이라면 virtualenv
가 익숙할 것입니다. 하지만 venv
라는 모듈이 Python 3.3 에 포함되었고, 3.5 부터 venv
를 사용하는 것이 권장되고 있습니다.
python3 -m venv /path/to/new/virtual/environment
위 명령을 실행하면 대상 디렉토리가 만들어지고, 필요한 파일, 심볼릭 링크, 서브디렉토리 등이 대상 디렉토리 아래에 생성됩니다.
자세한 사용법을 알고 싶다면 명령에 -h
를 사용합니다.
python3 -m venv -h
아래와 같은 결과가 나올 것입니다.
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip]
[--prompt PROMPT] [--upgrade-deps]
ENV_DIR [ENV_DIR ...]
Creates virtual Python environments in one or more target directories.
positional arguments:
ENV_DIR A directory to create the environment in.
optional arguments:
-h, --help show this help message and exit
--system-site-packages
Give the virtual environment access to the system site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks are not the default for the platform.
--copies Try to use copies rather than symlinks, even when symlinks are the default for the platform.
--clear Delete the contents of the environment directory if it already exists, before environment
creation.
--upgrade Upgrade the environment directory to use this version of Python, assuming Python has been
upgraded in-place.
--without-pip Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by
default)
--prompt PROMPT Provides an alternative prompt prefix for this environment.
--upgrade-deps Upgrade core dependencies: pip setuptools to the latest version in PyPI
Once an environment has been created, you may wish to activate it, e.g. by sourcing an activate script in its bin
directory.
일단 가상 환경이 만들어지면, 다음과 같은 방법으로 가상 환경을 활성화 할 수 있습니다.
source <venv-directory>/bin/activate
만약 fish
라는 쉘을 사용한다면
. <venv-directory>/bin/activate.fish
csh
/tcsh
을 사용할 경우
. source <venv-directory>/bin/activate.csh
가상 환경이 활성화 된 경우 VIRTUAL_ENV
라는 환경 변수가 가상 환경의 path 로 설정됩니다. 만약 어떤 가상 환경에서 실행중인지 확인하고 싶다면, 이 환경 변수를 확인하면 됩니다.
% echo $VIRTUAL_ENV
/Users/<username>/Work/venv-demo
deactivate
명령으로 앞서 활성화 했던 가상 환경을 비활성화 (빠져나오기) 할 수 있습니다.
아쉽게도 venv
는 서로 다른 Python 버전을 사용하는 환경을 만들 수는 없습니다. 그런 것을 위해서는 다음을 참고하세요.
- conda
- miniconda
- conda-forge
- PEP 405 - Python Virtual Environments
- Lightning Bits: Engineering for Researchers, Ep 03: How to use virtual environments to keep your computer organized
- https://docs.python.org/ko/3.8/library/venv.html
- https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment
GitHub에 업로드 시 포함해야하는 파일 목록은 어떤 게 있을까요?