How do you install dependencies and run a Python application?
Short answer
Create a virtual environment (python -m venv venv), activate it, install dependencies with pip install -r requirements.txt, and run the app — for example python app.py in development, or with a production server like gunicorn (Flask/Django) or uvicorn (FastAPI).
Explained in simple words
A virtual environment is a private box of libraries for one project. Without it, every project on the machine shares the same libraries, and one project upgrading a library can break another.
Step by step
python3 --version
python3 -m venv venv # create the virtual environment
source venv/bin/activate # activate it (Windows: venv\Scripts\activate)
pip install -r requirements.txt # install the project's libraries
python app.py # run in development
requirements.txt — the list of libraries, ideally with pinned versions:
flask==3.0.3
gunicorn==22.0.0
psycopg2-binary==2.9.9
Running in production
The built-in development servers are not meant for real traffic. Use a production server:
gunicorn -w 4 -b 0.0.0.0:8000 app:app # Flask: 4 workers, module app, object app
uvicorn main:app --host 0.0.0.0 --port 8000 # FastAPI
Example — Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "app:app"]
Inside a container you usually skip venv, because the container itself is already isolated.
What to say in the interview
"For Python apps we use a virtual environment so dependencies are isolated per project, and install them with pip install -r requirements.txt with pinned versions. In production we don't use the built-in dev server — Flask and Django run under gunicorn and FastAPI under uvicorn. In Docker we copy requirements.txt first and install it in its own layer so builds are fast."
Follow-up questions you may get
- How do you create requirements.txt?
pip freeze > requirements.txt, then keep only what you need. - What is pyproject.toml? The modern project file used by tools like Poetry and uv; it can replace requirements.txt.