Python Virtual Environment Management
Python Virtual Environment Management
When starting Python programming, setting up a virtual environment allows you to create an isolated environment for each project. This ensures that libraries used in different projects don’t interfere with each other, making your work safer and more efficient.
Python으로 프로그래밍을 시작할 때, 가상환경을 설정하면 각 프로젝트마다 독립적인 환경을 만들 수 있습니다. 이렇게 하면 다른 프로젝트에서 사용하는 라이브러리들이 서로 영향을 미치지 않아서 더욱 안전하고 효율적으로 작업할 수 있답니다.
Managing multiple projects with different library requirements under a single environment can lead to larger final builds, slower build times, and unexpected dependency conflicts.
여러 프로젝트를 운영할 때 각기 다른 라이브러리를 사용하는데 하나의 환경으로 운영하면 최종 결과물의 용량이 커질 뿐만 아니라 빌드 시간도 길어지고, 예상치 못한 종속성 문제로 오류가 발생하기도 합니다.
Virtual environments are especially important for collaboration, as they ensure all team members work in identical environments. Think of it as having an isolated Docker container for each project.
특히, 협업이 중요할때 이런걸 고려한다면 작업자간에 같은 환경으로 작업이 가능합니다. 하나의 독립적인 도커환경이라고 생각하고 사용해주시길 바랍니다.
This page will cover several approaches to managing Python virtual environments:
- Using PyCharm’s built-in virtual environment management
- Using the standard
venv
module from the command line - Using
uv
for faster dependency management - Using
pyenv
for Python version management
이 페이지에서는 Python 가상환경을 관리하는 여러 가지 방법을 다룹니다:
- PyCharm의 내장 가상환경 관리 기능 사용
- 커맨드라인에서 표준
venv
모듈 사용uv
를 사용한 더 빠른 의존성 관리pyenv
를 사용한 Python 버전 관리
1. Virtual Environment Management in PyCharm
1. 가상환경 및 인터프리터 관리
PyCharm makes it easy to set up and manage virtual environments for your Python projects. This is one of PyCharm’s biggest advantages - you don’t need to manually activate environments using terminal commands like venv\Scripts\activate
(Windows) or source venv/bin/activate
(macOS/Linux).
PyCharm에서는 가상환경을 설정하고 인터프리터를 관리하는 방법이 매우 간편합니다. VScode나 터미널환경에서는
venv\Scripts\activate
(윈도우) 또는source venv/bin/activate
(맥OS/리눅스)와 같은 명령어를 이용해야 하지만, PyCharm에서는 프로젝트마다 가상환경을 쉽게 만들고 관리할 수 있습니다.
1) Creating a Project Folder
1) 작업할 폴더 만들기
First, create a folder for your project. You can do this using your operating system’s file explorer or through the command line:
처음으로 작업할 폴더를 만듭니다. 윈도우 파일 탐색기나 맥OS의 Finder를 이용하여 직접 만드셔도 되고, 명령 프롬프트(CMD)나 터미널을 이용할 수도 있습니다.
Windows (Command Prompt):
cd desired_path
mkdir project_name
macOS/Linux (Terminal):
cd desired_path
mkdir project_name
Once created, open the folder in PyCharm:
만들어진 작업 폴더를 PyCharm의 ‘파일 - 열기’ 메뉴를 통해 열어서 시작하시면 편합니다.
PyCharm
File - Open
Note that command syntax differs between Windows (CMD), macOS Terminal, and Linux. If you’re new to command line interfaces, it’s perfectly fine to start with graphical tools like GitHub Desktop instead of git bash, and gradually build your skills.
CMD 명령어와 리눅스 그리고 맥은 명령어가 다릅니다. 처음부터 전문적으로 배우셔도 좋으나 git bash보다 GitHub Desktop를 사용하는 방법으로 차근차근 이해하고 원하시는 스킬로 한계단 올라가시길 바랍니다.
On Windows, PowerShell offers more features than CMD and is worth exploring.
윈도우에서는 PowerShell을 사용하실수 있다면 CMD보다 더 많은 기능들이 있으니 PowerShell도 써보시면 좋습니다.
2) Creating a Virtual Environment and Interpreter
2) 가상환경과 인터프리터 만들기
A virtual environment helps each project work in an isolated space. In simple terms, different projects may need different libraries and settings. Virtual environments keep these separate so they don’t interfere with each other.
가상환경은 각 프로젝트가 독립적인 공간에서 작업할 수 있도록 도와주는 기능입니다. 쉽게 말해, 프로젝트마다 필요한 라이브러리와 설정이 다를 수 있는데, 가상환경을 사용하면 이들을 격리시켜 서로 영향을 주지 않도록 관리할 수 있습니다.
An interpreter is the Python program that PyCharm uses to run your code. When you create a virtual environment, it includes a separate Python executable that’s specific to your project.
인터프리터는 파이참에서 파이썬 코드를 실행할 때 사용하는 파이썬 프로그램입니다. 가상환경을 만들면, 그 안에는 해당 프로젝트에서만 사용할 수 있는 별도의 python.exe 파일이 생기는데, 이 파일을 인터프리터로 설정해야 프로젝트가 제대로 동작합니다.
Accessing Settings
설정창 진입
Access the settings through one of these methods:
PyCharm의 ‘파일 - 설정’ 메뉴 또는 우측하단의 ‘Python Interpreter Selector - Click - 설정’을 통해 설정창에 진입이 가능합니다.
1. PyCharm
File - Settings (Windows/Linux) or Preferences (macOS)
2. PyCharm - Bottom right corner
Python Interpreter Selector - Click - Settings
Adding an Interpreter
인터프리터 추가창 진입
From the settings window or Python Interpreter Selector, you can access the interpreter addition screen. Select Local Interpreter
.
설정창 또는 Python Interpreter Selector에서 설정창에 진입하기전에 인터프리터 추가창으로 진입이 가능합니다.
로컬 인터프리터
를 선택해줍니다.
You can configure various environment settings:
- Virtualenv environment
- Create new
- Location: venv folder in your project directory
- Base interpreter: python.exe of your installed Python version
다양한 환경 설정을 구성할 수 있습니다:
- Virtualenv 환경 선택
- 새 가상환경 생성
- 위치: 프로젝트 디렉토리 내
venv
폴더- 기본 인터프리터: 설치된 Python 버전의
python.exe
파일 지정
The default settings are usually sufficient - just click Add to complete the process.
기본적으로 별도 설정하지않았다면 기본셋팅이 되어있어서 추가만하면 되게 되어있습니다.
You can customize the virtual environment folder name and location if needed.
venv라고 만들어지는 가상환경폴더명도 자유롭게 만드셔도되고 위치도 조절은 가능합니다.
The most important thing is to try it out, so complete the interpreter addition.
우선 해보는게 중요함으로 인터프리터 추가를 완료합니다.
3) Verifying the Setup
3) 적용 확인
Opening the Terminal
Click on the Terminal menu in the bottom left vertical menu.
터미널 창 열기
좌측 하단의 세로메뉴에 마우스를 올려놓으면 터미널 이라고 표시되는 메뉴를 눌러봅니다.
If you see (venv) in the terminal and Python 3.x (project_name) in the bottom right Python Interpreter Selector, the setup is working correctly.
(venv)라는 글이 있고 우측하단 Python Interpreter Selector에 Python 3.11 (작업폴더명)이 나온다면 정상적용되었습니다.
If the Python Interpreter Selector shows the correct environment but the Terminal doesn’t reflect it, try closing and reopening the Terminal. If that doesn’t work, restart PyCharm.
Python Interpreter Selector는 정상적인데 Terminal만 반영이 안된다면 Terminer을 끄고 다시 열어보시면 정상적용될텐데 어려우시면 PyCharm을 종료하고 다시 실행해서 확인해봅니다.
4) Deleting Virtual Environments and Interpreters
4) 가상환경 및 인터프리터 삭제
Knowing how to delete environments is important for resetting your workspace when needed.
개발중 작업환경을 초기화 하고 싶을 때 삭제하는 방법을 알아야 잘못된 환경을 삭제하고 새로운 가상환경으로 빠른 셋팅이 가능합니다.
Deleting a Virtual Environment
Simply click on the Project menu in the left vertical menu to open the project explorer. Find your virtual environment folder and delete it.
가상환경 삭제
단순하게 좌측의 세로메뉴중 프로젝트 메뉴를 누르면 프로젝트 탐색기가 열립니다. 여기서 생성한 가상환경의 폴더명을 선택해서 삭제하면 간단히 삭제가 됩니다.
However, deleting just the virtual environment folder leaves the interpreter reference. It’s good to know how to delete the interpreter as well.
앞서 가상환경과 인터피리터에 대해 설명해드렸는데 이 두개가 연계되어있어서 단순히 가상환경만 삭제하면 인터프리터가 남아있습니다. 그래서 인터프리터 삭제방법도 같이 알고있으면 좋습니다.
Deleting an Interpreter
Navigate to:
Interpreter settings
Python Interpreter list on the left of the interpreter addition feature
Show All at the bottom
인터프리터 삭제
인터프리터 설정창에서 인터프리터 추가창 진입기능 좌측의 Python 인터프리터 목록의 최하단에 있는 ‘모두 표시…’를 클릭합니다.
In the interpreter list, you can delete an interpreter by:
- Selecting it and clicking the ”-” button at the top
- Selecting it and pressing the Delete key
- Right-clicking it and selecting Delete
열린 인터프리터창 좌측 목록에서 삭제가 가능합니다. 목록 선택하여 상단의 ”-” 단추를 클릭하거나, 목록 선택후 Delete Key를 누르거나, 목록 선택하여 우클릭해서 삭제를 선택합니다.
Make sure to click Apply to confirm the changes.
꼭 적용을 해주셔야합니다.
Interpreters without a corresponding virtual environment are highlighted in red for easy identification.
가상환경이 없는 인터프리터는 붉은색으로 표기되니 확인하기 쉽습니다.
2. Command Line Virtual Environment Management
2. 명령줄 가상환경 관리
While PyCharm provides a convenient GUI for managing virtual environments, it’s also important to understand how to create and manage them from the command line.
PyCharm이 편리한 GUI로 가상환경을 관리할 수 있게 해주지만, 명령줄에서 가상환경을 생성하고 관리하는 방법을 이해하는 것도 중요합니다.
1) Using venv (Standard Library)
1) venv 사용하기 (표준 라이브러리)
Python’s built-in venv
module is the standard way to create virtual environments.
Python에 내장된
venv
모듈은 가상환경을 만드는 표준적인 방법입니다.
Windows:
python -m venv venv
venvScriptsactivate
macOS/Linux:
python3 -m venv venv
source venv/bin/activate
To deactivate the virtual environment:
가상환경을 비활성화하려면:
deactivate
2) Installing Packages
2) 패키지 설치하기
Once your virtual environment is activated, you can install packages using pip:
가상환경이 활성화되면 pip를 사용하여 패키지를 설치할 수 있습니다:
pip install package_name
To create a requirements.txt file for your project:
프로젝트의 requirements.txt 파일을 생성하려면:
pip freeze > requirements.txt
To install packages from a requirements.txt file:
requirements.txt 파일에서 패키지를 설치하려면:
pip install -r requirements.txt
3. Using uv for Faster Dependency Management
3. uv를 사용한 더 빠른 의존성 관리
uv
is a new Python packaging tool written in Rust that offers significantly faster package installation compared to pip. It’s compatible with existing Python projects and can be used as a drop-in replacement for pip in many cases.
uv
는 Rust로 작성된 새로운 Python 패키징 도구로, pip보다 훨씬 빠른 패키지 설치를 제공합니다. 기존 Python 프로젝트와 호환되며 많은 경우에 pip를 대체할 수 있습니다.
1) Installing uv
1) uv 설치하기
Windows/macOS/Linux:
pip install uv
Or using curl (macOS/Linux):
또는 curl 사용 (macOS/Linux):
curl -LsSf https://astral.sh/uv/install.sh | sh
2) Creating a Virtual Environment with uv
2) uv로 가상환경 만들기
uv venv
This creates a virtual environment in the .venv
directory by default.
이 명령은 기본적으로
.venv
디렉토리에 가상환경을 생성합니다.
3) Activating the Virtual Environment
3) 가상환경 활성화하기
Windows:
.venv\Scripts\activate
macOS/Linux:
source .venv/bin/activate
4) Installing Packages with uv
4) uv로 패키지 설치하기
uv pip install package_name
Or to install from a requirements.txt file:
또는 requirements.txt 파일에서 설치하려면:
uv pip install -r requirements.txt
5) Benefits of uv
5) uv의 장점
- Much faster package installation (5-100x faster than pip)
- Improved dependency resolution
- Built-in caching for faster repeated installations
- Compatible with existing Python projects
- Can generate lockfiles for reproducible environments
- 훨씬 빠른 패키지 설치 (pip보다 5-100배 빠름)
- 향상된 의존성 해결
- 반복 설치를 위한 내장 캐싱
- 기존 Python 프로젝트와 호환
- 재현 가능한 환경을 위한 락파일 생성 가능
4. Using pyenv for Python Version Management
4. pyenv를 사용한 Python 버전 관리
pyenv
allows you to easily switch between multiple versions of Python. It’s particularly useful when you need to work with projects that require different Python versions.
pyenv
를 사용하면 여러 Python 버전 간에 쉽게 전환할 수 있습니다. 서로 다른 Python 버전이 필요한 프로젝트를 작업할 때 특히 유용합니다.
1) Installing pyenv
1) pyenv 설치하기
macOS (using Homebrew):
brew update
brew install pyenv
Add to your shell configuration file (~/.zshrc, ~/.bash_profile, or ~/.bashrc):
쉘 설정 파일에 추가 (~/.zshrc, ~/.bash_profile, 또는 ~/.bashrc):
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Windows: For Windows, use pyenv-win
:
Windows의 경우,
pyenv-win
사용:
pip install pyenv-win --target $HOME.pyenv
Add to your environment variables:
환경 변수에 추가:
[System.Environment]::SetEnvironmentVariable('PYENV', $env:USERPROFILE + '.pyenvpyenv-win', 'User')
[System.Environment]::SetEnvironmentVariable('PYENV_HOME', $env:USERPROFILE + '.pyenvpyenv-win', 'User')
[System.Environment]::SetEnvironmentVariable('PYENV_ROOT', $env:USERPROFILE + '.pyenvpyenv-win', 'User')
Add to your PATH:
PATH에 추가:
[System.Environment]::SetEnvironmentVariable('path', $env:USERPROFILE + '.pyenvpyenv-winin;' + $env:USERPROFILE + '.pyenvpyenv-winshims;' + [System.Environment]::GetEnvironmentVariable('path', 'User'), 'User')
2) Installing Python Versions
2) Python 버전 설치하기
List available Python versions:
사용 가능한 Python 버전 목록 보기:
pyenv install --list
Install a specific version:
특정 버전 설치하기:
pyenv install 3.11.0
3) Setting Python Versions
3) Python 버전 설정하기
Set global Python version:
전역 Python 버전 설정:
pyenv global 3.11.0
Set local Python version (for current directory):
로컬 Python 버전 설정 (현재 디렉토리용):
pyenv local 3.11.0
4) Creating Virtual Environments with pyenv
4) pyenv로 가상환경 만들기
Install pyenv-virtualenv plugin (macOS/Linux):
pyenv-virtualenv 플러그인 설치 (macOS/Linux):
brew install pyenv-virtualenv
Add to your shell configuration file:
쉘 설정 파일에 추가:
eval "$(pyenv virtualenv-init -)"
Create a virtual environment with a specific Python version:
특정 Python 버전으로 가상환경 만들기:
pyenv virtualenv 3.11.0 my-project-env
Activate the virtual environment:
가상환경 활성화하기:
pyenv activate my-project-env
Deactivate:
비활성화하기:
pyenv deactivate
5. Next
- Poetry
Related Pages
- 1. Setting
- 2. Virtualenv
- 3. Poetry
- 4. Flet