Skip to content
← DevOps Foundations

Learning bite

A Python project you can reproduce

Choose an interpreter, isolate packages, and run a small project from a known directory.

  • Python
Documentation reviewed2026-10-01 · 3 min read
  1. Why
  2. Predict
  3. Try
  4. Inspect
  5. Explain
  6. Save evidence

Predict before you try

Will opening a new terminal automatically select this project’s Python environment?

Try it: Run a file, then import it →
On this page

Give each tool a home

A Python script needs an interpreter to execute its instructions. Python also includes a standard library: modules such as json, pathlib, and unittest that come with the interpreter. Third-party packages are separate downloads. Keeping those packages per project prevents an upgrade for one tool from unexpectedly changing another.

Use Python 3.11 or newer for this module. Install a supported Python through your operating system's documented package manager or the Python download instructions↗. Do not replace the Python installation used by the operating system. On macOS or Linux, open a terminal and check python3 --version before continuing.

Create a fresh directory under your normal practice folder. Run these commands there:

bash
mkdir capacity-tool
cd capacity-tool
python3 -m venv .venv
source .venv/bin/activate
python -c 'import sys; print(sys.executable); print(sys.version.split()[0])'

The first printed path should point inside capacity-tool/.venv. The version should be 3.11 or newer. -m venv asks Python to run its environment-creation module. Activation changes this shell's executable search path; it does not start a server or alter all your terminals. A new terminal needs activation again, or you can call .venv/bin/python explicitly.

Run a file, then import it

Save hello.py in capacity-tool:

python
def greeting(name: str) -> str:
    return f"Hello, {name}"

if __name__ == "__main__":
    print(greeting("learner"))

Run python hello.py. Expected output: Hello, learner. Then run python -c 'import hello; print(hello.greeting("operator"))'. Expected output: Hello, operator, without the first greeting. Executing a file gives its __name__ the value "__main__"; importing it gives the module its name. This guard lets tests reuse functions without accidentally running a command-line program.

A module is an importable Python file. A package groups modules. The current working directory affects which relative files your tool opens; it is not automatically the directory containing the script. Keep the terminal at the project root during these exercises.

Keep the project, recreate the environment

Add .venv/, __pycache__/, and *.pyc to this project's .gitignore. Commit your source, tests, and dependency declarations, not the interpreter environment. The next exercises use only the standard library, so there is nothing else to install.

When you later need packages, use python -m pip from the active environment so you know which interpreter receives them. A declared dependency range and a resolved lock file solve different problems: one describes supported versions, while the other records an exact selection. Tools such as uv↗ can manage project metadata, interpreter selection, and a lock file together. That is useful next-step tooling, not a prerequisite for understanding this small program.

Check your understanding

If a new terminal cannot import an installed package, first compare its interpreter path with the one used for installation. Reinstalling globally may hide the actual mismatch. A virtual environment isolates Python packages; it is not a security sandbox and does not isolate the filesystem or network.

Keep this directory for the next lesson. You will replace a greeting with calculations, decisions, and functions. When finished for the day, deactivate leaves the environment without deleting it.

References: Python virtual environments↗, modules↗, and packaging guide↗.

Prove what you learned

Inspect the result

Record input, returned value/output and expected versus actual failure behaviour.

Explain it in your own words

Explain why your observation supports the stated outcome, and name one thing it does not prove.

Try a small variation

Open a second terminal and compare sys.executable before and after activation.

Your evidence should show

  • Record the context/revision or label the work as a paper or prepared fixture.
  • Compare the prediction with the actual result from “Run a file, then import it”.
  • Record input, returned value/output and expected versus actual failure behaviour.
  • Explain the transfer challenge and retain the same verification question.
Hint

Compare the interpreter path in both terminals.

Answer guide: compare after predicting

No. Activate the project environment again or use its interpreter by explicit path.

Compare this with your actual observation. A paper answer is useful reasoning practice; record execution separately.

Recall how a shell selects an executable.

Save the prediction, result and explanation ↓

Your notes and evidence

Record observations, questions, or links to your work. Keep credentials out of your notes.

Loading saved progress…

Back up or restore this path

Progress and notes stay in this browser. A backup contains only this learning path.