6

When creating a virtual environment, I run:

python3 -m venv env

I understand that -m executes a module (venv in this case). However, what does the -m flag actually stand for?

Is it -m for module, or -m for __main__?

I couldn't find an unambiguous explanation. Here are some resources I investigated:

martin-martin
  • 2,834
  • 1
  • 30
  • 51

2 Answers2

4

in section 1.1.1 It clearly says -m is the module name, here.

Quoting from the docs :

"since the argument is a module name, you must not give a file extension (.py). The module-name should be a valid Python module name"

Although -m is arbitrary as in the backend It is an argparser doing all the work.

When called with -m module-name, the given module is located on the Python module path and executed as a script

Package names are also permitted. When a package name is supplied instead of a normal module, the interpreter will execute <pkg>.__main__ And I guess the main also starting with 'm' is a coincidence.

Vineeth Sai
  • 3,262
  • 7
  • 20
  • 31
  • 2
    Hei, I don't think that the docs you linked clearly explain that this is what `-m` stands for. I am still okay accepting your answer, but could you improve it by linking this sentence instead: `When called with -m module-name, the given module is located on the Python module path and executed as a script.` and using a more up-to-date Python documentation: https://docs.python.org/3.7/using/cmdline.html – martin-martin Sep 24 '18 at 10:01
  • Updated it. Thanks – Vineeth Sai Sep 24 '18 at 10:04
  • When python -m pkg runs, and if the pkg has got a __main__ file then it will also run. But what about the __init__ file? – variable Oct 15 '19 at 18:30
1

It runs the module following -m. See the official documentation

The documentation says -m <module-name>, as well as "Since the argument is a module name...", so it makes sense to assume that "m" stands for module.

blue_note
  • 25,410
  • 6
  • 56
  • 79