Making a package executable via Python -m
In the previous chapter, we ran command-line tools, such as doctest
and venv
, by typing in the python3 -m
command followed by the name of the tool we wanted it to run:

What were we actually asking Python to do when we did that?
The -m
command-line switch for Python tells it to run a module. It uses the same mechanism to find the module that it would if we'd used an import
statement with the module's name and then it executes it.
However, venv
isn't a module, it's a package. So, what's happening when we use python -m venv
? We gave Python a package name, but we didn't give it a module name inside the package that it should run. When that happens, Python looks for a module named __main__
in the package and runs that:

So, python -m venv
means the same thing as python -m venv.__main__
.
Any module that's meant to contain a program's entry point has a problem because simply importing the module will run the code too. This can be annoying or troublesome at the...