Python 가상환경 활성화 (Windows/Linux)
A practical comparison of how to activate and deactivate Python virtual environments on Windows and Linux, including PowerShell-specific issues.
Python 가상환경 활성화 (Windows/Linux)
Introduction
Activating a Python virtual environment sounds trivial, but the differences between Windows and Linux can cause real confusion — especially when you switch between operating systems regularly. As a freelancer working on both Windows and Linux machines, I have been bitten by this more times than I care to admit.
The core issue is that each shell (bash, zsh, PowerShell, cmd) handles environment variable modification differently. A virtual environment activation script written for bash will not work in PowerShell, and vice versa.
Environment
Windows 11 PowerShell 7.4
Ubuntu 24.04 bash 5.2
Python 3.12.3Problem
Linux/macOS activation:
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ which python
/home/joel/projects/myapp/.venv/bin/python
(.venv) $ python --version
Python 3.12.3Windows CMD activation:
C:\Users\joel> python -m venv .venv
C:\Users\joel> .venv\Scripts\activate.bat
(.venv) C:\Users\joel> where python
C:\Users\joel\.venv\Scripts\python.exeWindows PowerShell activation:
PS C:\Users\joel> python -m venv .venv
PS C:\Users\joel> .venv\Scripts\Activate.ps1
(.venv) PS C:\Users\joel> Get-Command python
C:\Users\joel\.venv\Scripts\python.exeNow here is where it breaks. On PowerShell with default execution policy:
PS C:\Users\joel> .venv\Scripts\Activate.ps1
File C:\Users\joel\.venv\Scripts\Activate.ps1 cannot be loaded because running scripts
is disabled on this system. For more information, see
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .venv\Scripts\Activate.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedError : (:) [], FullyQualifiedError : [Import-PSModule] Cannot
load the script because running scripts is disabled on this system.And on Git Bash (Windows):
$ source .venv/Scripts/activate
bash: .venv/Scripts/activate: No such file or directoryAnalysis
The differences between platforms stem from how each shell modifies environment variables.
Linux/macOS: The activation script is at .venv/bin/activate and it modifies $PATH to put .venv/bin first. It also sets the $VIRTUAL_ENV variable and modifies the shell prompt.
Windows CMD: The activation script is at .venv\Scripts\activate.bat and it modifies %PATH% and sets %VIRTUAL_ENV%.
Windows PowerShell: The activation script is at .venv\Scripts\Activate.ps1 and it modifies $env:Path and $env:VIRTUAL_ENV. PowerShell's execution policy blocks script execution by default.
Git Bash on Windows: Git Bash uses Unix-style paths, but the venv is created with Windows-style paths. The activation script is not at .venv/bin/activate — it is at .venv/Scripts/activate.
Solution
Fix 1: Change PowerShell execution policy (recommended)
# Check current policy
Get-ExecutionPolicy
# Set policy for current user (requires admin for machine-wide)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Now activation works
.venv\Scripts\Activate.ps1Fix 2: Use py launcher on Windows
# py launcher handles Python detection automatically
py -m venv .venv
.venv\Scripts\Activate.ps1Fix 3: Git Bash activation on Windows
# Git Bash can use the activate script directly
source .venv/Scripts/activate
# Or use this one-liner that works across shells
source .venv/Scripts/activate 2>/dev/null || source .venv/bin/activateFix 4: Use python -m venv with explicit paths
# PowerShell
$ python -m venv "$PWD\.venv"
$ & "$PWD\.venv\Scripts\Activate.ps1"
# Bash
$ python3 -m venv "$PWD/.venv"
$ source "$PWD/.venv/bin/activate"Fix 5: Create a cross-platform activation helper
#!/bin/bash
# activate-env.sh
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
# Windows (Git Bash/MSYS2)
source .venv/Scripts/activate
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux
source .venv/bin/activate
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
source .venv/bin/activate
fi$ chmod +x activate-env.sh
$ source activate-env.shFix 6: Deactivation (works the same everywhere)
# Works on all platforms
deactivate
# Verify you're back to system Python
python --versionLessons Learned
- Use
sourcefor bash/zsh and&for PowerShell when invoking activation scripts. - Set PowerShell execution policy to RemoteSigned as the first thing on a new Windows machine.
- Git Bash on Windows uses
.venv/Scripts/activate, not.venv/bin/activate— this trips up many developers. - Always check
which pythonorGet-Command pythonafter activation to confirm the venv is active. - Create a shell wrapper if you frequently switch between Windows and Linux in the same project.
This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.