Usages
-
run python file
python test.py
-
run a module
-m: module
python -m pdb test.py python -m pydoc -p 9000
-
input from stdin
(base) PS C:\> python Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
-
-c: source from command line
python -c "import shutil;shutil.copytree('./tflexdb', './tflexdb.backup', ignore=lambda src,names:[name for name in names if name.endswith('.log')])"
Sometime it is useful to use it in bash script if it is hard to implement with pure bash scripts.
-
run a package directory with main
- package with __main__.py
[willchen@fdev060101 py]$ python run_package/ ('my_print:', 'Hello') [willchen@fdev060101 py]$ python xmlrpc/ /usr/local/anaconda2/bin/python: can't find '__main__' module in 'xmlrpc/'
-
run pyz
python3 -m zipapp run_package/ # generate run_package.pyz python3 run_package.pyz # same with run package folder
Input scale: stdin, cmd_line, file, module, pcakge, package.pyz
Go Over Interpreter
Interpreter
Byte Code: op_code + stack_index
>>> add = lambda a1 ,a2 : a1 + a2
>>> add(1,3)
4
>>> add.__code__.co_code # the bytecode as raw bytes
b'|\x00|\x01\x17\x00S\x00'
>>> list(add.__code__.co_code) # the bytecode as numbers
[124, 0, 124, 1, 23, 0, 83, 0]
>>> import dis
>>> dis.dis(add) # (line number, index of bytecode, op_name, stack index, varialbe name)
1 0 LOAD_FAST 0 (a1)
2 LOAD_FAST 1 (a2)
4 BINARY_ADD
6 RETURN_VALUE
>>>
In C Python, there is a very long swith case code for op handle
Virtual Machine is a Stack Manipulator