.. code-block:: shell-session
- $ python3 prog.py
- $ python3 prog.py --help
+ $ python prog.py
+ $ python prog.py --help
usage: prog.py [-h]
options:
-h, --help show this help message and exit
- $ python3 prog.py --verbose
+ $ python prog.py --verbose
usage: prog.py [-h]
prog.py: error: unrecognized arguments: --verbose
- $ python3 prog.py foo
+ $ python prog.py foo
usage: prog.py [-h]
prog.py: error: unrecognized arguments: foo
.. code-block:: shell-session
- $ python3 prog.py
+ $ python prog.py
usage: prog.py [-h] echo
prog.py: error: the following arguments are required: echo
- $ python3 prog.py --help
+ $ python prog.py --help
usage: prog.py [-h] echo
positional arguments:
options:
-h, --help show this help message and exit
- $ python3 prog.py foo
+ $ python prog.py foo
foo
Here is what's happening:
.. code-block:: shell-session
- $ python3 prog.py -h
+ $ python prog.py -h
usage: prog.py [-h] echo
positional arguments:
.. code-block:: shell-session
- $ python3 prog.py 4
+ $ python prog.py 4
Traceback (most recent call last):
File "prog.py", line 5, in <module>
print(args.square**2)
.. code-block:: shell-session
- $ python3 prog.py 4
+ $ python prog.py 4
16
- $ python3 prog.py four
+ $ python prog.py four
usage: prog.py [-h] square
prog.py: error: argument square: invalid int value: 'four'
.. code-block:: shell-session
- $ python3 prog.py --verbosity 1
+ $ python prog.py --verbosity 1
verbosity turned on
- $ python3 prog.py
- $ python3 prog.py --help
+ $ python prog.py
+ $ python prog.py --help
usage: prog.py [-h] [--verbosity VERBOSITY]
options:
-h, --help show this help message and exit
--verbosity VERBOSITY
increase output verbosity
- $ python3 prog.py --verbosity
+ $ python prog.py --verbosity
usage: prog.py [-h] [--verbosity VERBOSITY]
prog.py: error: argument --verbosity: expected one argument
.. code-block:: shell-session
- $ python3 prog.py --verbose
+ $ python prog.py --verbose
verbosity turned on
- $ python3 prog.py --verbose 1
+ $ python prog.py --verbose 1
usage: prog.py [-h] [--verbose]
prog.py: error: unrecognized arguments: 1
- $ python3 prog.py --help
+ $ python prog.py --help
usage: prog.py [-h] [--verbose]
options:
.. code-block:: shell-session
- $ python3 prog.py -v
+ $ python prog.py -v
verbosity turned on
- $ python3 prog.py --help
+ $ python prog.py --help
usage: prog.py [-h] [-v]
options:
.. code-block:: shell-session
- $ python3 prog.py
+ $ python prog.py
usage: prog.py [-h] [-v] square
prog.py: error: the following arguments are required: square
- $ python3 prog.py 4
+ $ python prog.py 4
16
- $ python3 prog.py 4 --verbose
+ $ python prog.py 4 --verbose
the square of 4 equals 16
- $ python3 prog.py --verbose 4
+ $ python prog.py --verbose 4
the square of 4 equals 16
* We've brought back a positional argument, hence the complaint.
.. code-block:: shell-session
- $ python3 prog.py 4
+ $ python prog.py 4
16
- $ python3 prog.py 4 -v
+ $ python prog.py 4 -v
usage: prog.py [-h] [-v VERBOSITY] square
prog.py: error: argument -v/--verbosity: expected one argument
- $ python3 prog.py 4 -v 1
+ $ python prog.py 4 -v 1
4^2 == 16
- $ python3 prog.py 4 -v 2
+ $ python prog.py 4 -v 2
the square of 4 equals 16
- $ python3 prog.py 4 -v 3
+ $ python prog.py 4 -v 3
16
These all look good except the last one, which exposes a bug in our program.
.. code-block:: shell-session
- $ python3 prog.py 4 -v 3
+ $ python prog.py 4 -v 3
usage: prog.py [-h] [-v {0,1,2}] square
prog.py: error: argument -v/--verbosity: invalid choice: 3 (choose from 0, 1, 2)
- $ python3 prog.py 4 -h
+ $ python prog.py 4 -h
usage: prog.py [-h] [-v {0,1,2}] square
positional arguments:
.. code-block:: shell-session
- $ python3 prog.py 4
+ $ python prog.py 4
16
- $ python3 prog.py 4 -v
+ $ python prog.py 4 -v
4^2 == 16
- $ python3 prog.py 4 -vv
+ $ python prog.py 4 -vv
the square of 4 equals 16
- $ python3 prog.py 4 --verbosity --verbosity
+ $ python prog.py 4 --verbosity --verbosity
the square of 4 equals 16
- $ python3 prog.py 4 -v 1
+ $ python prog.py 4 -v 1
usage: prog.py [-h] [-v] square
prog.py: error: unrecognized arguments: 1
- $ python3 prog.py 4 -h
+ $ python prog.py 4 -h
usage: prog.py [-h] [-v] square
positional arguments:
options:
-h, --help show this help message and exit
-v, --verbosity increase output verbosity
- $ python3 prog.py 4 -vvv
+ $ python prog.py 4 -vvv
16
* Yes, it's now more of a flag (similar to ``action="store_true"``) in the
.. code-block:: shell-session
- $ python3 prog.py 4 -vvv
+ $ python prog.py 4 -vvv
the square of 4 equals 16
- $ python3 prog.py 4 -vvvv
+ $ python prog.py 4 -vvvv
the square of 4 equals 16
- $ python3 prog.py 4
+ $ python prog.py 4
Traceback (most recent call last):
File "prog.py", line 11, in <module>
if args.verbosity >= 2:
.. code-block:: shell-session
- $ python3 prog.py 4
+ $ python prog.py 4
16
You can go quite far just with what we've learned so far,
.. code-block:: shell-session
- $ python3 prog.py
+ $ python prog.py
usage: prog.py [-h] [-v] x y
prog.py: error: the following arguments are required: x, y
- $ python3 prog.py -h
+ $ python prog.py -h
usage: prog.py [-h] [-v] x y
positional arguments:
options:
-h, --help show this help message and exit
-v, --verbosity
- $ python3 prog.py 4 2 -v
+ $ python prog.py 4 2 -v
4^2 == 16
.. code-block:: shell-session
- $ python3 prog.py 4 2
+ $ python prog.py 4 2
16
- $ python3 prog.py 4 2 -v
+ $ python prog.py 4 2 -v
4^2 == 16
- $ python3 prog.py 4 2 -vv
+ $ python prog.py 4 2 -vv
Running 'prog.py'
4^2 == 16
.. code-block:: shell-session
- $ python3 prog.py 4 2
+ $ python prog.py 4 2
4^2 == 16
- $ python3 prog.py 4 2 -q
+ $ python prog.py 4 2 -q
16
- $ python3 prog.py 4 2 -v
+ $ python prog.py 4 2 -v
4 to the power 2 equals 16
- $ python3 prog.py 4 2 -vq
+ $ python prog.py 4 2 -vq
usage: prog.py [-h] [-v | -q] x y
prog.py: error: argument -q/--quiet: not allowed with argument -v/--verbose
- $ python3 prog.py 4 2 -v --quiet
+ $ python prog.py 4 2 -v --quiet
usage: prog.py [-h] [-v | -q] x y
prog.py: error: argument -q/--quiet: not allowed with argument -v/--verbose
.. code-block:: shell-session
- $ python3 prog.py --help
+ $ python prog.py --help
usage: prog.py [-h] [-v | -q] x y
calculate X to the power of Y
.. code-block:: shell-session
- $ python3 helloworld.py
+ $ python helloworld.py
Hello, world!
* the Python module or package passed to the Python interpreter with the
.. code-block:: shell-session
- $ python3 -m tarfile
+ $ python -m tarfile
usage: tarfile.py [-h] [-v] (...)
* Python code read by the Python interpreter from standard input:
.. code-block:: shell-session
- $ echo "import this" | python3
+ $ echo "import this" | python
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
.. code-block:: shell-session
- $ python3 -c "import this"
+ $ python -c "import this"
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
returned if your function does not have a return statement).
By proactively following this convention ourselves, our module will have the
-same behavior when run directly (i.e. ``python3 echo.py``) as it will have if
+same behavior when run directly (i.e. ``python echo.py``) as it will have if
we later package it as a console script entry-point in a pip-installable
package.
.. code-block:: shell-session
- $ python3 -m bandclass
+ $ python -m bandclass
This command will cause ``__main__.py`` to run. How you utilize this mechanism
will depend on the nature of the package you are writing, but in this
.. code-block:: shell-session
- $ python3 start.py
+ $ python start.py
Define the variable `my_name`!
The exit code of the program would be 1, indicating an error. Uncommenting the
.. code-block:: shell-session
- $ python3 start.py
+ $ python start.py
Dinsdale found in file /path/to/start.py
Note that importing ``__main__`` doesn't cause any issues with unintentionally