首页 > 编程知识 正文

如何查看当前 Python 版本?

时间:2024-04-28 10:06:29 阅读:335786 作者:MGVQ

一、如何查看Python

Python 是现在非常常用的一种编程语言,但是在电脑上安装好 Python 之后,如何查看电脑上是否已经安装呢?下面介绍一下。

import platform
print(platform.python_version())

运行结果如下:

3.8.5

二、Python如何查看代码

在 Python 中,可以查看当前运行的代码所在的文件名,以及代码在文件中的行数。

import os
import inspect

def show_current_file_line():
    callerframerecord = inspect.stack()[1]
    frame = callerframerecord[0]
    info = inspect.getframeinfo(frame)
    print(f"file name: {info.filename}")
    print(f"line number: {info.lineno}")
  
show_current_file_line()

运行结果如下:

file name: /path/to/current/file.py
line number: 8

三、如何查看Python库

在 Python 中,有很多第三方库可以调用,但是怎样查看已经安装了哪些库呢?可以使用 pip 命令(line)来查看已经安装的库。

pip list

运行结果如下:

Package        Version
-------------- --------
argh           0.26.2
astroid        2.4.2
decorator      4.4.2

四、Python查看当前环境

当在使用 Python 写代码时,有时会需要知道当前代码运行的环境,可以通过以下代码来实现。

import os
print(os.environ['PATH'])

运行结果如下:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

五、如何查看Python路径

在编写 Python 代码时,有时会需要查看某个文件或目录的路径,通过以下代码可以实现它。

import os
print(os.path.abspath('/'))

运行结果如下:

/home/user

六、如何查看Python的版本

可以使用以下代码来查看 Python 的版本。

import sys
print(sys.version_info)

运行结果如下:

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

七、如何查看Python环境

有时候需要在 Python 代码中执行系统命令,比如执行一些 shell 命令,这时就需要知道当前 Python 环境的一些信息,下面的代码可以帮助你查看当前 Python 环境的版本、路径等信息。

import platform
import sys
import os

print(f"OS: {os.name}")
print(f"Platform: {platform.system()}")
print(f"Python Version: {sys.version}")
print(f"Python Executable Path: {sys.executable}")

运行结果如下:

OS: posix
Platform: Linux
Python Version: 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0]
Python Executable Path: /usr/bin/python3

八、如何查看电脑有没有Python

如果想要知道电脑上是否已经安装 Python,可以在终端中输入以下命令。

python --version

如果已经安装,则会显示 Python 的版本号。

Python 3.8.5

文章到这里就结束了,希望对大家有所帮助。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。