Learn Python
>>> modules_packages.pip_venv

pip and Virtual Environments in Python

#concept

pip is Python's package installer, used to add third-party libraries — like requests or pandas — that aren't part of the standard library. A virtual environment is an isolated, self-contained installation of Python and its packages for one specific project, so different projects can depend on different (even conflicting) package versions without interfering with each other.

#how it works
1

python -m venv env_name

Creates a new virtual environment in a folder called env_name.

2

Activate it

A command that differs by operating system, making that isolated environment the one currently in use.

3

pip install package_name

Installs a package into whichever environment is currently active.

4

pip freeze > requirements.txt

Saves a list of exactly what's installed, so it can be recreated elsewhere.

5

pip install -r requirements.txt

Installs every package listed in that file, recreating the same environment on another machine.

#example
python -m venv myenv
source myenv/bin/activate
pip install requests
pip freeze > requirements.txt
Output
(myenv) $ pip freeze > requirements.txt
(myenv) $ cat requirements.txt
requests==2.31.0
1python -m venv myenv creates a new, isolated environment in a folder called myenv.
2Activating it changes the prompt to show (myenv), confirming that environment is now the active one.
3pip install requests installs the requests library only into this isolated environment, leaving the system-wide Python untouched.
4pip freeze > requirements.txt writes out exactly what’s installed and at which version, letting the same setup be recreated later with pip install -r requirements.txt.
#use cases
Installing third-party libraries a project depends on, like requests, pandas, or Django
Keeping each project's dependencies isolated, so one project's package versions never conflict with another's
Sharing a requirements.txt file with collaborators, so everyone can recreate the exact same environment
Avoiding the need for administrator privileges to install packages, since a virtual environment lives entirely within a project folder

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.