python sys模块_Python sys模块

Python sys模块提供与解释器交互的简单功能,适用于Windows、Macintosh和Linux平台。包括导入系统、sys.modules、sys.argv、sys.path、sys.stdin、sys.copyright、sys.exit和sys.getrefcount等功能。sys.argv收集传给脚本的字符串参数,sys.path显示系统路径,sys.stdin用于接收标准输入,sys.exit让解释器立即退出,sys.getrefcount返回对象的引用计数。
摘要由CSDN通过智能技术生成

python sys模块

Python sys module provides easy functions that allow us to interact with the interpreter directly.

Python sys模块提供了简单的功能,使我们可以直接与解释器进行交互。

Python sys模块 (Python sys module)

The functions python sys module provides allows us to operate on underlying interpreter, irrespective of it being a Windows Platform, Macintosh or Linux. In this lesson, we will review these functions and what we can do with these.

python sys模块提供的功能使我们可以在基础解释器上进行操作,无论它是Windows平台,Macintosh还是Linux。 在本课程中,我们将回顾这些功能以及我们可以做什么。

Python导入系统 (Python import sys)

Let us start our journey with these functions and what information they offer. Please note that before running any functions, we need to import it using below command.

让我们从这些功能及其提供的信息开始我们的旅程。 请注意,在运行任何功能之前,我们需要使用以下命令将其导入。

import sys

Python sys.modules (Python sys.modules)

This function gives the names of the existing python modules current shell has imported. Let’s execute this on the system:

此函数提供当前shell已导入的现有python模块的名称。 让我们在系统上执行此操作:

>>> sys.modules.keys()
dict_keys(['builtins', 'sys', '_frozen_importlib', '_imp'])

I have removed many modules as initially Python imports many modules by default. So the output may differ when you will execute this command in your python setup.

我删除了许多模块,因为最初Python默认会导入许多模块。 因此,当您在python设置中执行此命令时,输出可能会有所不同。

Python sys.argv (Python sys.argv)

This function collects the String arguments passed to the python script. Let’s execute this on the system by making a script:

此函数收集传递给python脚本的String参数。 让我们通过编写脚本在系统上执行此操作:

import sys
print('The command line arguments are:')
for i in sys.argv:
    print(i)

Run this script on terminal now:

python sys.argv, python sys module

立即在终端上运行此脚本:

Python系统路径 (Python sys.path)

This function just displays the PYTHONPATH set in current system. Let’s execute this on the system by making a script:

此功能仅显示当前系统中设置的PYTHONPATH 。 让我们通过编写脚本在系统上执行此操作:

import sys
print('\n\nThe PYTHONPATH is', sys.path, '.\n')

Run this script on terminal now:

python sys.path command, python sys functions

立即在终端上运行此脚本:

Python sys.stdin (Python sys.stdin)

This function is used to take . Let’s execute this on the system by making a script:

此功能用于。 让我们通过编写脚本在系统上执行此操作:

import sys
user_input = sys.stdin.readline()
print("Input : " + user_input)

Run this script on terminal now:

Python sys.stdin

立即在终端上运行此脚本:

This is probably the most commonly used function in sys module as it is the standard way of taking input from user.

这可能是sys模块中最常用的功能,因为它是从用户那里获取输入的标准方法。

Python sys.copyright (Python sys.copyright)

This String just displays the copyright information on currently installed Python version. Let’s execute this on the system by making a script:

该字符串仅显示当前安装的Python版本的版权信息。 让我们通过编写脚本在系统上执行此操作:

import sys
print(sys.copyright)

Run this script on terminal now:

python sys copyright

立即在终端上运行此脚本:

Python sys.exit (Python sys.exit)

This method makes the Python interpretor exits the current flow of execution abruptly. Let’s execute this on the system by making a script:

此方法使Python解释器突然退出当前执行流程。 让我们通过编写脚本在系统上执行此操作:

import sys
print("JournalDev")
sys.exit(1)
print("Hello")

Run this script on terminal now:

python sys exit

立即在终端上运行此脚本:

Python sys.getrefcount (Python sys.getrefcount)

This python sys module method returns the count for references to an object where it is used. Python keeps track of this value, as, when this value reaches 0 in a program, the memory for this variable is cleaned up. Let’s execute this on the system by making a script:

此python sys模块方法返回计数,用于引用使用该对象的对象。 Python会跟踪该值,因为在程序中该值达到0时,将清理该变量的内存。 让我们通过编写脚本在系统上执行此操作:

import sys

variable = "JournalDev"

print(sys.getrefcount(0))
print(sys.getrefcount(variable))
print(sys.getrefcount(None))

Run this script on terminal now:

python sys getrefcount

立即在终端上运行此脚本:

In this lesson, we learned about various functions provided by sys module in Python and saw how they work. See more lessons on Python here.

在本课程中,我们学习了python中sys模块提供的各种功能,并了解了它们如何工作。 在此处查看有关Python的更多课程。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/17243/python-sys-module

python sys模块

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值