Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to "run library module as a script" with the "-m" option? [duplicate]

I'm new to Python (and to programming as well) and, although well documented, I cannot understand what exactly means the -m directive (precisely in the creation of a virtual environment: python3 -m venv my_env.

As far as I can read from the documentation, it stands for "run library module as a script": it is in fact this concept that I cannot figure out and what is the difference in running the command without the -m.

Moreover, it this a characteristic of Python 3?

like image 630
Pankus Avatar asked Sep 20 '17 10:09

Pankus


People also ask

What does '- M mean in Python?

The -m stands for module-name in Python. The module-name should be a valid module name in Python. The -m flag in Python searches the sys. path for the named module and executes its contents as the __main__ module.

What does M mean in command line?

Conclusion. The -m flag is, at its simplest, a means to execute python scripts from the command line by using modulenames rather than filenames.

What does M mean pip?

To begin with, python -m pip executes pip using the Python interpreter you specified as python . So /usr/bin/python3. 7 -m pip means you are executing pip for your interpreter located at /usr/bin/python3. 7 . You can read the docs on -m if you're unfamiliar with the flag and how it works (it's very handy).

What is the difference between a script and a module?

A script is a Python file that's intended to be run directly. When you run it, it should do something. This means that scripts will often contain code written outside the scope of any classes or functions. A module is a Python file that's intended to be imported into scripts or other modules.


1 Answers

Python modules are just script files that are located in a place where Python can find them. As with all scripts, you can just run them directly if you know where they are, e.g. python /path/to/module.py.

Properly designed modules usually do nothing except set up stuff (e.g. functions and types you could import), but they usually won’t have any visible side effect. That’s why you can do import sys and nothing happens.

However, some modules may offer useful stuff when they are run from the command line. Examples for that include venv but also http.server or idlelib: All of those are regular modules that can be imported from other modules without side effects.

But when executed directly, they all do things (e.g. venv sets up a virtual environment, http.server runs a simple HTTP server, and idlelib runs IDLE). This is usually done with the following check:

if __name__ == '__main__':     print('Module is being executed directly, so do stuff here') 

This is a special way of recognizing of a script/module is being executed directly, or whether it’s just being imported from some other module. You can learn more about the question “What does if __name__ == '__main__': do?”.

So, you can run a module directly using python /path/to/module.py as we established before. But this requires you to know the full path to the module. That’s where the -m option comes into play: For modules that can usually be imported just using import modulename, you can use python -m modulename to run that module directly. Just as if you typed the full path to it.

So for our above examples, we can just use python -m venv, python -m http.server. or python -m idlelib.

like image 149
poke Avatar answered Oct 19 '22 01:10

poke