online posts 16
python 101 for network engineers

task 1.1 · chapter 1 of 8: running python · 20 min

Hello, Python

objective

Use the REPL as a calculator, write and run your first script, and find out which Python each terminal runs: the lab’s venv, and the one outside it.

you edit, in the labscripts/hello.pyanswers/1.1.yml

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
The REPLpython in the terminal; exit() or Ctrl+D to leave
Your scriptscripts/hello.py: a new file
Your answersanswers/1.1.yml
new to this? step by stepbeginner
  1. New to the lab app, the editor or make? Meet the lab covers them; this task starts where its section 4 ends.
  2. In the terminal: python --version. Then python, type len("GigabitEthernet1"), and exit().
  3. Make scripts/hello.py with one line, print("Hello, network!"), and run python scripts/hello.py.
  4. which python. Challenge: deactivate, then which python again.
  5. Fill in answers/1.1.yml, then make check T=1.1.

more in meet the lab

1 explain

One interpreter, two ways in

The interpreter

Python is a program, the interpreter, that reads Python code and runs it a statement at a time. You’ll use it two ways in this lab:

  • the REPL, its interactive prompt >>>: type a line, get the answer back straight away;
  • a script: the same lines saved in a .py file, run with python file.py.

The REPL is for trying things. A script is for keeping them. Every function you write in this lab, you’ll try in the REPL first.

in one sentence python alone opens the REPL; python file.py runs a script.

A script prints only what you tell it to

In the REPL, 2 + 2 shows 4, because the REPL prints every result. In a script, nothing appears unless the code calls print().

Which Python?

A computer can have several Pythons, each with its own installed packages. The terminal runs the first one it finds on PATH, and which python says which that is. In this lab, every new terminal starts inside the lab’s venv, a Python environment with the lab’s packages in it, and the prompt shows (lab) to say so. deactivate leaves it.

in one sentence When code works in one terminal and not another, compare which python in both.

2 show

The prompt, a script, two Pythons

Starting the REPL prints the version and where it was built:

$ python
Python 3.11.18 (main, Oct 28 2026, 04:42:12) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 ** 10
1024
>>> len("GigabitEthernet1")
16
>>> exit()

A one-line script, and running it:

$ cat scripts/hello.py
print("Hello, network!")
$ python scripts/hello.py
Hello, network!

And the two Pythons:

$ which python
/home/vscode/.venvs/lab/bin/python
$ deactivate
$ which python
/usr/local/bin/python
3 follow along

Your first line of Python

  1. Open a terminal (Terminal › New Terminal). Its prompt starts with (lab). Ask Python what it is:

    run in the terminal
    python --version
    
  2. Start the REPL with python, and use it as a calculator for a minute: 2 + 2, 10 / 4, 10 // 4, 2 ** 10. Then count the letters in an interface name:

    >>> len("GigabitEthernet1")
    

    Leave with exit().

  3. Make the script. In the Explorer, right-click the scripts folder › New File…, call it hello.py, and give it one line:

    new filescripts/hello.pyCtrl+P → hello.py
    print("Hello, network!")
    

    Save it, and run it:

    run in the terminal
    python scripts/hello.py
    

    The quotes and the exclamation mark matter: the check compares the line exactly.

  4. Find out which Python the terminal runs:

    run in the terminal
    which python
    
  5. Fill in the follow-along answers, with what your terminal printed:

    editanswers/1.1.ymlCtrl+P → 1.1.yml
    version:          3.11.18
    venv_python:      /home/vscode/.venvs/lab/bin/python
    repl_answer:      16
    

in one sentence The lab’s Python is the venv’s, at ~/.venvs/lab/bin/python, and every new terminal starts in it.

4 challenge

The Python outside the venv

Leave the venv in this terminal and find out which Python it runs then. That’s system_python.

Afterwards, open a new terminal: it’s back in the venv. (make commands always use the venv, whichever terminal you run them in.)

hint

deactivate is a command the venv added to your shell when it was activated. Run it, and the (lab) in the prompt goes away.

hint

Then the same which python as before. It’s no longer under .venvs.

show the solutionanswers/1.1.yml

or pull it into your lab: make solve T=1.1, then git diff --staged shows what changed

system_python:    /usr/local/bin/python
$ deactivate
$ which python
/usr/local/bin/python

That’s the codespace’s own Python 3.11, the one the venv was made from. It doesn’t have Netmiko or any of the lab’s other packages: a venv’s packages stay inside the venv. Run python -c "import netmiko" here and you get ModuleNotFoundError; in a new terminal, inside the venv, the same line prints nothing, because it worked.

5 check: run it, then prove it

$ make check T=1.1

when it's right, you'll see

task 1.1  checks/test_1_1.py
checks/test_1_1.py::test_version PASSED                                  [ 20%]
checks/test_1_1.py::test_venv_python PASSED                              [ 40%]
checks/test_1_1.py::test_repl_answer PASSED                              [ 60%]
checks/test_1_1.py::test_system_python PASSED                            [ 80%]
checks/test_1_1.py::test_hello PASSED                                    [100%]
============================== 5 passed in 0.02s ===============================
✓ task 1.1 done