5

When creating a virtual environment, I run:

python3 -m venv env

I understand that -m executes a module (venv in this case). I also know that it only works if you have a __main__.py file in your module. So, what does the shortcut -m actually stand for?

Is it:

  1. -m for module or
  2. -m for __main__
  3. Something else?

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

martin-martin
  • 3,274
  • 1
  • 33
  • 60
  • I always liked to think of it as `-m` for **make**, but I know that's incorrect ; ) – martin-martin Sep 21 '18 at 10:04
  • 3
    -m is for module – Vineeth Sai Sep 21 '18 at 10:04
  • Do you have any resources that state this clearly @VineethSai? If so, please post it and I'll accept it as an answer. – martin-martin Sep 21 '18 at 10:05
  • 3
    in section 1.1.1 It clearly says -m is module name https://docs.python.org/2/using/cmdline.html – Vineeth Sai Sep 21 '18 at 10:07
  • It doesn't stand for anything. – Stop harming Monica Sep 21 '18 at 13:04
  • Please add some notes on why this question was downvoted so that I can improve. I felt it was a valid question to ask on SO and I did my research. Thanks! – martin-martin Oct 01 '18 at 13:07
  • @martin-martin, I'm on board with the downvote (but would be even more on-board with an off-topic close vote) because knowing the answer doesn't change how one goes about the _practice_ of programming, and Stack Overflow's scope is explicitly limited to _practical_ questions. The recent edit makes the curiosity-based, impractical nature even more clear. – Charles Duffy May 05 '23 at 18:13
  • @CharlesDuffy okay thank you for clarifying. Would you suggest to delete the question, or keep it in its current state where it's marked as a duplicate? Or should we change the status to "Off topic"? – martin-martin May 08 '23 at 23:07

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,389
  • 7
  • 23
  • 34
  • 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
  • 27,712
  • 9
  • 72
  • 90