首页 » InfoTech »

python -m 参数

2018年9月28日 / 30次阅读
Python

打开支付宝首页,搜索“529018372”,即可领取红包!可重复领。

本文总结自己在学习使用python -m这个参数启动模块运行时,收集到的相关信息。

用python -help查看:

D:\python365>python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-b     : issue warnings about str(bytes_instance), str(bytearray_instance)
         and comparing bytes/bytearray with str. (-bb: issue errors)
-B     : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
-i     : inspect interactively after running script; forces a prompt even
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-I     : isolate Python from the user's environment (implies -E and -s)
-m mod : run library module as a script (terminates option list)
-O     : remove assert and __debug__-dependent statements; add .opt-1 before
         .pyc extension; also PYTHONOPTIMIZE=x
-OO    : do -O changes and also discard docstrings; add .opt-2 before
         .pyc extension
-q     : don't print version and copyright messages on interactive startup
-s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE
-S     : don't imply 'import site' on initialization
-u     : force the binary I/O layers of stdout and stderr to be unbuffered;
         stdin is always buffered; text I/O layer will be line-buffered;
         also PYTHONUNBUFFERED=x
-v     : verbose (trace import statements); also PYTHONVERBOSE=x
         can be supplied multiple times to increase verbosity
-V     : print the Python version number and exit (also --version)
         when given twice, print more information about the build
-W arg : warning control; arg is action:message:category:module:lineno
         also PYTHONWARNINGS=arg
-x     : skip first line of source, allowing use of non-Unix forms of #!cmd
-X opt : set implementation-specific option
file   : program read from script file
-      : program read from stdin (default; interactive mode if a tty)
arg ...: arguments passed to program in sys.argv[1:]

Other environment variables:
PYTHONSTARTUP: file executed on interactive startup (no default)
PYTHONPATH   : ';'-separated list of directories prefixed to the
               default module search path.  The result is sys.path.
PYTHONHOME   : alternate  directory (or ;).
               The default module search path uses \python{major}{minor}
.
PYTHONCASEOK : ignore case in 'import' statements (Windows).
PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.
PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.
PYTHONHASHSEED: if this variable is set to 'random', a random value is used
   to seed the hashes of str, bytes and datetime objects.  It can also be
   set to an integer in the range [0,4294967295] to get hash values with a
   predictable seed.
PYTHONMALLOC: set the Python memory allocators and/or install debug hooks
   on Python memory allocators. Use PYTHONMALLOC=debug to install debug
   hooks.

D:\python365>

-m mod : run library module as a script (terminates option list)

按照脚本的方式运行library里面的模块:

(1)运行的是标准库里面的模块;

(2)运行时,不要写出.py后缀。

比如,启动Python自带的Http服务器:

python -m http.server

http是一个库里面的package,这个package里面有一个server.py文件,以上命令,就是运行这个py文件。如果你去打开这个py文件,看看,在最后,就可以看到启动httpserver的代码。(__name__ = __main__)这行命令如果不带-m,就不能成功执行。

我们自己写的模块,也可以使用-m这个参数来运行,不过记住,不要有.py后缀,否则会有一个错误出现

print(__name__)
import sys
print(set(sys.path))
print('-'*10)
print(len(set(sys.path)))

D:\pytest>python -m testk
__main__
{'', 'D:\\', 'D:\\python365', 'D:\\python365\\python36.zip', 'D:\\python365\\lib
', 'D:\\python365\\lib\\site-packages', 'D:\\python365\\DLLs', 'D:\\pytest'}
----------
8

D:\pytest>python -m testk.py
testk
{'', 'D:\\python365\\python36.zip', 'D:\\', 'D:\\pytest', 'D:\\python365\\lib',
'D:\\python365\\lib\\site-packages', 'D:\\python365\\DLLs', 'D:\\python365'}
----------
8
D:\python365\python.exe: Error while finding module specification for 'testk.py'
 (AttributeError: module 'testk' has no attribute '__path__')

D:\pytest>python testk.py
__main__
{'D:\\python365\\python36.zip', 'D:\\python365\\lib', 'D:\\python365\\DLLs', 'D:
\\python365', 'D:\\pytest', 'D:\\', 'D:\\python365\\lib\\site-packages'}
----------
7

带上py后缀,不仅有个错误,__name__的值也不再是__main__,而是testk。

不带-m参数后,有一个大家都很关注的不同点,就是sys.path里面少了一个值''。其实,我真的不知道这个空字符串路径的作用是什么。

 

再回到python -m http.server这行命令,麦新杰做了如下测试:

有这个模块文件:D:\\pytest\\testk.py

D:\pytest>cd ..

D:\>python -m pytest.testk
__main__
{'', 'D:\\python365\\python36.zip', 'D:\\', 'D:\\python365', 'D:\\python365\\lib
', 'D:\\python365\\lib\\site-packages', 'D:\\python365\\DLLs'}
----------
7

D:\>python pytest.testk.py
python: can't open file 'pytest.testk.py': [Errno 2] No such file or directory

D:\>python pytest.testk
python: can't open file 'pytest.testk': [Errno 2] No such file or directory

D:\>python pytest/testk.py
__main__
{'D:\\pytest', 'D:\\', 'D:\\python365\\lib', 'D:\\python365\\python36.zip', 'D:\
\python365\\lib\\site-packages', 'D:\\python365\\DLLs', 'D:\\python365'}
----------
7

D:\>python -m pytest/testk
D:\python365\python.exe: No module named pytest/testk

-m参数穿透了pytest文件夹,将其作为一个package的文件夹,直接调用里面的testk来运行。

请参考 设置Python的PYTHONPATH环境变量 的后半部分,介绍了在python -m的情况下,import失败的一种场景和解决方案。

本文链接:http://www.maixj.net/ict/python-m-19009
云上小悟 麦新杰(QQ:1093023102)

相关文章

评论是美德

《python -m 参数》有2条评论

无力满足评论实名制,评论对非实名注册用户关闭,有事QQ:1093023102.

  • 麦新杰

    python -m 就是直接运行模块库里面的代码,把模块文件直接运行起来。常常看到的是python -m pip install XXXXX这样的说明。 [ ]

    • 麦新杰

      pip不是一个标准模块,是属于site-package里面的内容。 [ ]


前一篇:
后一篇:

栏目精选

云上小悟,麦新杰的独立博客

Ctrl+D 收藏本页

栏目

AD

ppdai

©Copyright 麦新杰 Since 2014 云上小悟独立博客版权所有 备案号:苏ICP备14045477号-1。云上小悟网站部分内容来源于网络,转载目的是为了整合信息,收藏学习,服务大家,有些转载内容也难以判断是否有侵权问题,如果侵犯了您的权益,请及时联系站长,我会立即删除。

网站二维码
go to top