Skip to content
$ lynuxpro
← Python for DevOps

LESSON 1 OF 19 · BEGINNER · 13 MIN

Python setup and virtual environments

Free previewLesson 1 of the 2 free lessons in this Pro course.Unlock all 19

HOW IT WORKSA toolbox per project

Python 3installed once.venvprivate toolboxpip installrequests, pyyamlrequirements.txtthe shopping listSystem Pythonkeep it clean
1/5

Install Python 3 once on your machine.

$ python3 --version

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

SystemHow
macOSbrew install python
Ubuntu / Debiansudo apt install python3 python3-venv python3-pip
Windowsinstall from python.org and tick "Add to PATH"
python3 --version
SHELLbashutf-8 · 1L
Python 3.12.4
OUTPUTterminalutf-8 · 1L

Use Python 3.10 or newer. Python 2 is dead; never use it.

Your first script

echo 'print("Hello, DevOps")' > hello.pypython3 hello.py
SHELLbashutf-8 · 2L
Hello, DevOps
OUTPUTterminalutf-8 · 1L

Virtual 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
SHELLbashutf-8 · 2L
(.venv) $
OUTPUTterminalutf-8 · 1L

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 list
SHELLbashutf-8 · 2L

Save what you installed so others can repeat it:

pip freeze > requirements.txtpip install -r requirements.txt
SHELLbashutf-8 · 2L
CommandWhat it does
pip install NAMEinstall a package
pip install NAME==2.31.0install an exact version
pip uninstall NAMEremove it
pip freezelist exact versions
pip install -r requirements.txtinstall everything in the file

Good habits

  • One venv per project, kept in .venv.
  • Add .venv/ to .gitignore. Commit requirements.txt instead.
  • 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)"
SHELLbashutf-8 · 2L
/home/ana/tools/.venv/bin/python3/home/ana/tools/.venv
OUTPUTterminalutf-8 · 2L

If the path points into .venv, the venv is active. If it points to /usr/bin, it is not.

Other tools you will hear about

ToolWhat it does
pyenvinstalls and switches between Python versions
pipxinstalls command-line tools, each in its own venv
uva very fast pip and venv replacement
poetrymanages 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")
NORMALpythonutf-8 · 2L
chmod +x backup.py./backup.py
SHELLbashutf-8 · 2L
backup done
OUTPUTterminalutf-8 · 1L

The 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 pip from the wrong Python. Run python3 -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. 1. What is a virtual environment for?

  2. 2. How do you save installed packages so others can repeat them?

  3. 3. How can you tell a venv is active?