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.
scripts/hello.pyanswers/1.1.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| The REPL | python in the terminal; exit() or Ctrl+D to leave |
|---|---|
| Your script | scripts/hello.py: a new file |
| Your answers | answers/1.1.yml |
new to this? step by stepbeginner
- New to the lab app, the editor or
make? Meet the lab covers them; this task starts where its section 4 ends. - In the terminal:
python --version. Thenpython, typelen("GigabitEthernet1"), andexit(). - Make
scripts/hello.pywith one line,print("Hello, network!"), and runpython scripts/hello.py. which python. Challenge:deactivate, thenwhich pythonagain.- Fill in answers/1.1.yml, then
make check T=1.1.
more in meet the lab
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
.pyfile, run withpython 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.
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
Your first line of Python
Open a terminal (Terminal › New Terminal). Its prompt starts with
(lab). Ask Python what it is:run in the terminalpython --versionStart 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().Make the script. In the Explorer, right-click the
scriptsfolder › New File…, call ithello.py, and give it one line:new filescripts/hello.pyCtrl+P → hello.pyprint("Hello, network!")Save it, and run it:
run in the terminalpython scripts/hello.pyThe quotes and the exclamation mark matter: the check compares the line exactly.
Find out which Python the terminal runs:
run in the terminalwhich pythonFill in the follow-along answers, with what your terminal printed:
editanswers/1.1.ymlCtrl+P → 1.1.ymlversion: 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.
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.1when 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