Virtual environment (venv)
Also: 虛擬環境 · virtual environment · virtualenv · PEP 668 · externally-managed-environment
A project-private folder of Python packages, kept apart from the system. Once activated, whatever pip installs lands only inside it.
When you will meet it
Your first pip install on macOS or Linux is quite likely to be refused outright with error: externally-managed-environment. Not recognising that line leads you to assume you mistyped, or to find a 'one-line fix' online that damages your system Python. The real answer is a venv, and it costs two commands.
An analogy
Like renting a separate workshop instead of doing the work in your living room. Pile up whatever tools you like and make any mess — the living room is unaffected, and if you dislike it you simply hand the workshop back.
Minimal example
python3 -m venv .venv # 在目前資料夾建一個叫 .venv 的環境
source .venv/bin/activate # macOS / Linux:啟動它
.venv\Scripts\Activate.ps1 # Windows PowerShell:啟動它
python3 -m pip install requests # 之後裝的東西都只進到 .venv 裡
deactivate # 用完離開After activation your prompt gains a (.venv) prefix. Seeing that prefix is how you know the python and pip you are about to type mean this environment, not the system one.
What people get wrong
- Creating a venv and then running pip install without activating it. The package still goes to the system Python and you get the identical error again.
- Reading externally-managed-environment as 'not enough permission' and re-running with sudo. It has nothing to do with permissions — your distribution blocked it deliberately, and sudo only makes the aftermath harder to clean up.
Related terms
Next
- Triple Strike: OMP Error #15, PEP 668, and Python Dependency Hell23 min
- 五分鐘設置你的 LLM 開發環境15 minChinese only