In simple words
- Python: an easy-to-read programming language widely used for automation, scripts and tools in DevOps.
- Virtual environment (venv): a private folder of packages for one project, so projects do not clash with each other or the system.
- pip: Python's tool for installing packages.
- requirements.txt: a list of a project's packages and versions, so anyone can install the same set.
Big picture
Python is a language for writing small programs that do jobs for you: call an API, rename 500 files, check a server. Before you write any code, you need two things: Python itself, and a clean box to keep each project's tools in. That clean box is called a virtual environment.
Think of it like a toolbox per project. Project A can have version 1 of a tool, project B can have version 2, and they never fight.
Install and check Python
| System | How |
|---|---|
| macOS | brew install python |
| Ubuntu / Debian | sudo apt install python3 python3-venv python3-pip |
| Windows | install from python.org and tick "Add to PATH" |
python3 --versionPython 3.12.4Use Python 3.10 or newer. Python 2 is dead; never use it.
Your first script
echo 'print("Hello, DevOps")' > hello.pypython3 hello.pyHello, DevOpsVirtual environments
A virtual environment (venv) is a folder with its own Python and its own installed packages. Nothing you install there touches the rest of your computer.
python3 -m venv .venvsource .venv/bin/activate(.venv) $The (.venv) in the prompt tells you it is active. To leave it, run deactivate. On Windows, activate with .venv\Scripts\activate.
Install packages with pip
pip is the tool that downloads packages from PyPI, the public package store.
pip install requests pyyamlpip listSave what you installed so others can repeat it:
pip freeze > requirements.txtpip install -r requirements.txt| Command | What it does |
|---|---|
pip install NAME | install a package |
pip install NAME==2.31.0 | install an exact version |
pip uninstall NAME | remove it |
pip freeze | list exact versions |
pip install -r requirements.txt | install everything in the file |
Good habits
- One venv per project, kept in
.venv. - Add
.venv/to.gitignore. Commitrequirements.txtinstead. - Pin versions in files that CI uses, so builds are the same every time.
Which Python am I using?
When something odd happens, check which Python and which pip are running:
which python3python3 -c "import sys; print(sys.prefix)"/home/ana/tools/.venv/bin/python3/home/ana/tools/.venvIf the path points into .venv, the venv is active. If it points to /usr/bin, it is not.
Other tools you will hear about
| Tool | What it does |
|---|---|
pyenv | installs and switches between Python versions |
pipx | installs command-line tools, each in its own venv |
uv | a very fast pip and venv replacement |
poetry | manages dependencies and packaging in one tool |
You do not need them on day one. venv and pip are enough, and every other tool builds on the same ideas.
Run a script like a program
On Linux and macOS you can start a script without typing python3:
#!/usr/bin/env python3print("backup done")chmod +x backup.py./backup.pybackup doneThe first line (the shebang) tells the system which interpreter to use.
Common mistakes
- Forgetting to activate. Packages install into your system Python and you get "module not found" later. Check the
(.venv)prefix. - Using
pipfrom the wrong Python. Runpython3 -m pip install ...to be sure. - Committing
.venv. It is large and machine specific.
Summary
Install Python 3, make a venv with python3 -m venv .venv, activate it, install packages with pip, and save them in requirements.txt.
CHECK YOUR UNDERSTANDING
3 quick questions
1. What is a virtual environment for?
2. How do you save installed packages so others can repeat them?
3. How can you tell a venv is active?