The difference between python -m and python
python file.py
Normal execution of Python scripts seems to be like this, even some friends don't even know what -m
is.
The explanation of -m
in help is: Treat the file as a script to execute.
What does it mean to execute it as a script? Let's take a look
Write a simple test script bar.py
import sys
print('sys.path len is %s.'% sys.path.__len__())for path in sys.path:print(path)
Then execute separately to see the results
python -m bar # bar! ! ! Not a bar.py pit! ! !
Therefore, the first line and the last added environment variable is empty, and the meaning of empty
is the current folder. Which is the directory where your command is executed
python bar.py
The first line is the absolute path, the directory where the file is located. Not the directory where the script is executed.
note
1 - The m parameter cannot be followed by .py
, which is equivalent to import bar
2 - m supports .
syntax python -m http.server
to start a web server
PS: Let's take a look at the meaning of python -m
Simply put: Execute files under a certain module
The form of the parameter after python -m is the module name. File name
The module can be run as a script. Look at the difference between the running mode and python file.py.
Using this command will search and execute py files in the library path, not only in the current library.
run library module as a script.
to sum up
So far, this article about why Python -m is introduced. For more related content about the difference between python -m and python, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. Hope you will support ZaLou more in the future. .Cn!
Recommended Posts