Why Python -m

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

  1. Simply put: Execute files under a certain module

  2. 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.

  1. The explanation in the python parameter description is:

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

Why Python -m
Why python is popular
Why call python a glue language
Python multithreading
Python CookBook
Python FAQ
Why doesn't Python support the switch statement?
Python3 dictionary
Python3 module
Python basics
Python descriptor
Python basics 2
Python exec
Python3 tuple
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Why doesn't Python support the switch statement?
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Why is python suitable for writing crawlers
Python string
python queue Queue
Why doesn't Python support the switch statement?
Python basics 4
Python basics 5