本文主要是介绍理解python -c||python -m interpreter run scripts,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
-
缘起
refer2中使用到:
python -c 'import site; print(site.getsitepackages())' python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
-c
-m
are both command-line[refer1] options.When invoking Python ,you may specify any of these options:
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
-
Using the Python Interpreter
The Python interpreter is usually installed as
/usr/local/bin/python3.9
on those machines where it is avaiable; putting/usr/local/bin
in your Unix shell’s search path makes it possible to start it by typing the command:python3.9
to the shell.
-
Redirecting the output
python hello.py > output.txt # rather than to the stdout,known as stram redirection. It will create or replace output.txt python hello.py >> output.txt # add the output to the file
-
Using python module
-m module
python -m <module-name> python -m hello # not python -m hello.py
The
-m
option searchessys.path
for the module name and runs its content as_main_
对于功能单一的代码,就可以用这样的方式,而不用写复杂的
argparses
-
Execute the Python statements
-c command
It executes the Python statements given as command.
Here command may contain multiple statements separated by newlines.
And leading whitespace is significant.
If the option is given, the first element of
sys.argv
will be “-c
” and the current directory will be added to the start ofsys.path
(allowing modules in that directory to be imported as top level modules). -
import
runs the code as its final stopIt just do the “run” work once, after the first
import
, successiveimport
executions do nothing. -
importlib
module -
exec(open('hello.py').read())
-
Stop running
Control-D
Unix;Control-Z
Windows; -
总结
python -m
orpython -c
both run python scripts using the interpreter.关于
python -m
和python -c
的用法,在网上搜索很难发现什么资料,后来才突然意识到,想要学习它们,最好的资料是它们自己的帮助文档:python -h
-
References
- Command line and environment
- How do I find the location of my Python site-packages directory?
- How to Run Your Python Scripts
- Using the Python Interpreter
这篇关于理解python -c||python -m interpreter run scripts的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!