Printing Python version in output

前端 未结 5 1012
清歌不尽
清歌不尽 2020-11-30 17:07

How can I print the version number of the current Python installation from my script?

相关标签:
5条回答
  • 2020-11-30 17:14

    Try

    import sys
    print(sys.version)
    

    This prints the full version information string. If you only want the python version number, then Bastien Léonard's solution is the best. You might want to examine the full string and see if you need it or portions of it.

    0 讨论(0)
  • 2020-11-30 17:25

    Try

    python --version 
    

    or

    python -V
    

    This will return a current python version in terminal.

    0 讨论(0)
  • 2020-11-30 17:31
    import sys  
    

    expanded version

    sys.version_info  
    sys.version_info(major=3, minor=2, micro=2, releaselevel='final', serial=0)
    

    specific

    maj_ver = sys.version_info.major  
    repr(maj_ver) 
    '3'  
    

    or

    print(sys.version_info.major)
    '3'
    

    or

    version = ".".join(map(str, sys.version_info[:3]))
    print(version)
    '3.2.2'
    
    0 讨论(0)
  • 2020-11-30 17:34
    import platform
    print(platform.python_version())
    

    This prints something like

    3.7.2

    0 讨论(0)
  • 2020-11-30 17:37

    If you are using jupyter notebook Try:

    !python --version
    

    If you are using terminal Try:

     python --version
    
    0 讨论(0)
提交回复
热议问题