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系统路径 (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.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:

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

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.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.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 sysvariable = "JournalDev"print(sys.getrefcount(0))
print(sys.getrefcount(variable))
print(sys.getrefcount(None))

Run this script on terminal now:

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

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模块

python sys模块_Python sys模块相关推荐

  1. python莫比乌斯环_python基础|模块

    1 模块简介 在python中常见的模块有三种,在python解释器中的内置模块,第三方模块和自定义模块.模块的有使用python编写的文件,有已被编译为共享库或DLL的C或C++扩展,也有使用C编写 ...

  2. python如何自定义模块_python自定义模块和开源模块使用方法

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  3. python import变量_Python import模块调用

    开发过程中代码越写越多,在一个文件里代码会越来越长,不容易维护,为了容易维护代码,我们把很多函数分组,分别放在不同的文件里,在Python中,一个.py文件就是模块(Module) 工具/原料 Pyt ...

  4. python常用运维模块_python常用模块之一

    sys模块: sys模块是提供关于python本身的详细内在的信息的模块. sys.executable变量,它包含python解释器的路径 sys.platform变量,告诉我们现在处于什么操作系统 ...

  5. python 多层包多模块_python Modules模块操作

    今天学习python的Modules模块操作,并记录学习过程欢迎大家一起交流分享. 首先新建一个python文件命名为my_module.py的自定义moudle文件,在这个文件中进行模块代码编写: ...

  6. python pp模块_python常用模块

    1.re模块 re模块用于对python的正则表达式的操作 1.1 什么是正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物 ...

  7. python counter模块_python collections模块 计数器(counter)

    一.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 把我写入的元素出现的多少次都计算出来 import collectio ...

  8. 如何显示python的内置模块_python之模块(内置模块)

    内置模块是Python自带的功能,在使用内置模块相应的功能时,需要[先导入]再[使用] 一.sys 用于提供对Python解释器相关的操作: 1 sys.argv #命令行参数List,第一个元素是程 ...

  9. python中的模块_python的模块和包的详细说明

    Python模块和包的详细说明 模块的导入 模块的加载与修改 模块和脚本的说明 模块搜索路径 包的导入 一.模块的导入 之前我们简单的使用了一下模块,并没有详细的介绍,现在我们来详细的说说 1.什么是 ...

最新文章

  1. NS_ASSUME_NONNULL_BEGIN 延伸
  2. 官司一打 20 年,Linux 抄袭 Unix 终有定论,原告被 “ 熬死 ” 、IBM赔了上亿
  3. 记一次LVM修复过程
  4. JMM主内存和工作内存运行流程图
  5. js string转number_Node.js 和 C++ 之间的类型转换
  6. 接口的基本演练 java
  7. ReactNative设置字体不随系统字体大小变化
  8. Jmeter系列之接口断言
  9. 为什么感觉iPhone 11还有很多人去买?
  10. TCMalloc : Thread-Caching Malloc
  11. C#只能靠参数而不能靠返回值类型的不同来区分方法重载
  12. 进阶10 补充知识点
  13. 高通三频802.11ac平台:家庭WiFi新体验?
  14. OBS Studio录制腾讯会议
  15. openvn 安装和配置
  16. html 页眉选项卡,连续两个奇数页页眉 再选择插入菜单——分页,执行2次
  17. Redis常用基本命令(1)
  18. 三十六计之借刀杀人(第三计)
  19. qq 表情gif免费下载
  20. Ubuntu图形界面下命令行窗口打开快捷键创建

热门文章

  1. [DB]MariaDB 与 MySql 数据库
  2. mysql explain和profiling
  3. 关于MATLAB实现的数字信号处理(二)
  4. [转载] python异常和错误有什么区别_python的错误和异常
  5. JAVA-初步认识-第六章-类与对象的关系(细节)
  6. 保存画面为图片 当前MFC保存该程序为图片 c++ vc
  7. MySQL 5.6 dump/load buffer pool实验
  8. Cracking The Coding Interview5.3 暂存
  9. ASP.NET MVC Url中参数过长引发的问题
  10. *******clob问题***********