──────────────────────────────────────

35. Python Project Workspace · Read Help and Set Up an Environment

Document an option with argparse and identify the Python interpreter used for a project.

Lesson files · lessons/modules-and-workspaces/command-help-and-environments

Before you start
- Pass a value by name and use a default when a caller leaves it out.
- Create and run a project’s main.py file.

By the end
- Add a help description and check both default and explicit group counts.
- Identify the command that runs pip with the project’s Python interpreter.

Where you can use it
Put a plain-language description in --help so a colleague can find the group option and run the planner with their own count.

A terminal runs a command such as python3 main.py --spares 4. Python starts main.py, and argparse reads the --spares option and its value. Use python3 main.py --help to see the available options. On Windows, use py in place of python3. The parser builds the help from its options and the descriptions you give them.

In Studio, the starter translates the Program input line into a parser call. With help, it checks the text from format_help(). With default, it supplies an empty argument list. With a number, it passes that number using the named option.

This project uses the standard library. To practise keeping a project’s Python packages separate, run python3 -m venv .venv or py -m venv .venv on Windows. Check which pip belongs to that environment with .venv/bin/python -m pip --version on macOS or Linux, or .\.venv\Scripts\python.exe -m pip --version in Windows PowerShell.

Predict
Where does the Extra items to bring text come from?
- The argument’s help setting.
- The default value.

Worked example (example.py)

import argparse

parser = argparse.ArgumentParser(prog="pack-planner")
parser.add_argument("--spares", type=int, default=0, help="Extra items to bring")
print("Extra items to bring" in parser.format_help())
print(parser.parse_args(["--spares", "4"]).spares)


Example result
True
4

Give --groups a default of 1 and add the help text Number of groups to supply. Keep type=int. The supplied program tests help mode, default mode and a group count passed as an option.

Starter program (starter.py)

import argparse

parser = argparse.ArgumentParser(prog="pack-planner")
parser.add_argument("--groups", type=int, default=0)

mode = input()
if mode == "help":
    help_text = parser.format_help()
    if "--groups" in help_text and "Number of groups to supply" in help_text:
        print("Help explains --groups.")
    else:
        print("Help needs a description.")
else:
    arguments = [] if mode == "default" else ["--groups", mode]
    options = parser.parse_args(arguments)
    print(f"Groups: {options.groups}")


Starter input
help

Hint
Add help="Number of groups to supply" in add_argument and change default=0 to default=1.

Case 1. Try the sample
Input
help
Expected output
Help explains --groups.

Case 2. Use the default
Input
default
Expected output
Groups: 1

Case 3. Set an explicit count
Input
4
Expected output
Groups: 4

Stretch
Print parser.format_help() to read the whole message. In a terminal, try python3 -m venv --help to find the environment folder argument. For an offline project with requirements.txt and compatible packages in a wheelhouse folder, use the environment’s Python with -m pip install --no-index --find-links wheelhouse -r requirements.txt.

Reflect
Why should you use the same interpreter to run pip and start the project?

Check your prediction
The argument’s help setting. Help text describes the option. The default supplies its value when the option is omitted.
