What are pip and virtual environments in Python, and why do you use them?
pip is the package installer for Python. It installs packages from PyPI and from other indexes, and it manages the dependencies that those packages declare, so asking for one package brings in the other packages that it needs. The commands you need most are pip install, pip uninstall, pip list, pip show, and pip freeze.
A virtual environment is an isolated folder that holds its own copy of the Python interpreter and its own place for installed packages. When it is active, pip installs into that folder instead of into the system one. You create one with python -m venv .venv, and you turn it on with the activate script, which is source .venv/bin/activate on Linux and macOS and .venv\Scripts\activate in Windows. From that moment on, pip and python refer to the environment and not to the system.
The reason this matters is that it keeps different projects separate. Two projects can need different versions of the same library, and without environments they would fight over the single system copy of it. Environments also make a project repeatable, because the list of installed packages is written down in a file, and everybody who installs from that file ends up with the same versions.
That file is usually called requirements.txt, and you create it with pip freeze and use it with pip install -r requirements.txt. Many new projects use pyproject.toml instead, because that file describes the project itself along with its dependencies. Tools such as pip-tools, Poetry, and uv also manage the lock file that pins the exact versions.
The practical rule for an interview is simple. Use one virtual environment per project, always turn it on before you install anything, and commit the dependency file so that the next person can build the same setup.