python -v 命令是做什么用的?

27

我最近重新安装了macOS,并想检查Python的版本。我在终端中输入python -v,但不确定这做了什么?我对终端和Python编程都很新。我是否意外安装了新软件?

Last login: Sun May 14 15:02:37 on ttys000
Matthews-MacBook-Pro:~ matthewkol$ python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py
import site # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py
import os # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc
import errno # builtin
import posix # builtin
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py
import posixpath # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.py
import stat # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py
import genericpath # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.py
import warnings # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/linecache.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/linecache.py
import linecache # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/linecache.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.py
import types # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py
import UserDict # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.py
import _abcoll # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.py
import abc # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.py
import _weakrefset # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.pyc
import _weakref # builtin
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py
import copy_reg # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.py
import traceback # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.py
import sysconfig # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py
import re # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_compile.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_compile.py
import sre_compile # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_compile.pyc
import _sre # builtin
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.py
import sre_parse # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_constants.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_constants.py
import sre_constants # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_constants.pyc
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_locale.so", 2);
import _locale # dynamically loaded from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_locale.so
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.py
import _sysconfigdata # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_osx_support.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_osx_support.py
import _osx_support # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_osx_support.pyc
import encodings # directory /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/__init__.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/__init__.py
import encodings # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/__init__.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.py
import codecs # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.pyc
import _codecs # builtin
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/aliases.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/aliases.py
import encodings.aliases # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/aliases.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py
import encodings.utf_8 # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc
Python 2.7.10 (default, Feb  6 2017, 23:53:20) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so", 2);
import readline # dynamically loaded from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so
>>> help
Type help() for interactive help, or help(object) for help about object.
>>> help(object)
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.py
import pydoc # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.pyc
import imp # builtin
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py
import inspect # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/string.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/string.py
import string # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/string.pyc
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/strop.so", 2);
import strop # dynamically loaded from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/strop.so
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/dis.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/dis.py
import dis # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/dis.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/opcode.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/opcode.py
import opcode # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/opcode.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tokenize.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tokenize.py
import tokenize # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tokenize.pyc
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/itertools.so", 2);
import itertools # dynamically loaded from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/itertools.so
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/token.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/token.py
import token # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/token.pyc
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/operator.so", 2);
import operator # dynamically loaded from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/operator.so
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py
import collections # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.pyc
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_collections.so", 2);
import _collections # dynamically loaded from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_collections.so
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/keyword.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/keyword.py
import keyword # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/keyword.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/heapq.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/heapq.py
import heapq # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/heapq.pyc
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_heapq.so", 2);
import _heapq # dynamically loaded from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_heapq.so
import thread # builtin
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py
import pkgutil # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/repr.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/repr.py
import repr # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/repr.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.py
import locale # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py
import functools # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.pyc
dlopen("/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_functools.so", 2);
import _functools # dynamically loaded from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_functools.so

>>> python -V
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> ^D
# clear __builtin__._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.exitfunc
# clear sys.exc_type
# clear sys.exc_value
# clear sys.exc_traceback
# clear sys.last_type
# clear sys.last_value
# clear sys.last_traceback
# clear sys.path_hooks
# clear sys.path_importer_cache
# clear sys.meta_path
# clear sys.flags
# clear sys.float_info
# restore sys.stdin
# restore sys.stdout
# restore sys.stderr
# cleanup __main__
# cleanup[1] _collections
# cleanup[1] site
# cleanup[1] sysconfig
# cleanup[1] _heapq
# cleanup[1] abc
# cleanup[1] _weakrefset
# cleanup[1] sre_constants
# cleanup[1] collections
# cleanup[1] _codecs
# cleanup[1] opcode
# cleanup[1] _warnings
# cleanup[1] _sysconfigdata
# cleanup[1] repr
# cleanup[1] _osx_support
# cleanup[1] readline
# cleanup[1] strop
# cleanup[1] _functools
# cleanup[1] thread
# cleanup[1] keyword
# cleanup[1] signal
# cleanup[1] traceback
# cleanup[1] pydoc
# cleanup[1] itertools
# cleanup[1] posix
# cleanup[1] exceptions
# cleanup[1] _weakref
# cleanup[1] token
# cleanup[1] heapq
# cleanup[1] locale
# cleanup[1] functools
# cleanup[1] encodings
# cleanup[1] operator
# cleanup[1] pkgutil
# cleanup[1] zipimport
# cleanup[1] inspect
# cleanup[1] encodings.utf_8
# cleanup[1] imp
# cleanup[1] codecs
# cleanup[1] encodings.aliases
# cleanup[1] dis
# cleanup[1] tokenize
# cleanup[1] string
# cleanup[1] re
# cleanup[1] _locale
# cleanup[1] sre_compile
# cleanup[1] _sre
# cleanup[1] sre_parse
# cleanup[2] copy_reg
# cleanup[2] posixpath
# cleanup[2] errno
# cleanup[2] _abcoll
# cleanup[2] types
# cleanup[2] genericpath
# cleanup[2] stat
# cleanup[2] warnings
# cleanup[2] UserDict
# cleanup[2] os.path
# cleanup[2] linecache
# cleanup[2] os
# cleanup sys
# cleanup __builtin__
# cleanup ints: 21 unfreed ints
# cleanup floats
Matthews-MacBook-Pro:~ matthewkol$ 

1
根据官方文档 - https://docs.python.org/3/using/cmdline.html#cmdoption-v。虽然您正在使用Python 2,但对于这种特殊情况并不重要,但是这里是py2文档 - idjaw
1
你想使用大写字母V来表示版本。在向Python命令传递参数时,小写字母v和大写字母V之间存在差异。 - idjaw
谢谢。所以我的理解是,使用“-v”选项,我可以将系统预安装的Python模块导入终端,这样我就可以在终端中编写Python脚本?然后当我退出时,它会清除所有已导入的内容并清理一切? - Matthew
3个回答

13

你没有做错任何事情。从Python的手册页面中获取(通过man python访问):

   -v     Print  a  message each time a module is initialized, showing the
          place (filename or built-in module) from  which  it  is  loaded.
          When  given twice, print a message for each file that is checked
          for when searching for a module.  Also provides  information  on
          module cleanup at exit.

   -V ,  --version
          Prints  the  Python  version number of the executable and exits.
          When given twice, print more information about the build.

把小写的-v看作"详细模式"——它对于调试模块非常有用。你列出的这些模块都来自标准库。


1
这应该是被接受的答案,因为它实际上解释了小写的-v选项所做的事情。当所有这些东西滚过时,对于Python新手来说非常可怕... ;) - leanne

6

4
为了确保我理解正确,我已经导入了Python,这样我就可以在终端内编写Python脚本?然后当我退出时,它会清除一切并清理干净?另外,当它说正在清除和清理时,到底发生了什么? - Matthew
1
是的,这就是正在发生的事情。 - Menachem Hornbacher

0

python -v 命令用于以 详细模式 运行 Python 解释器。当执行此命令时,它会显示有关 Python 程序执行的详细信息,包括所有导入的模块Python 版本以及解释器执行程序所采取的步骤。它对于调试和理解 Python 程序的内部工作原理非常有用。


1
这个答案在6年后添加了什么? - FlyingTeller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接