{"id":80178,"date":"2021-01-16T09:00:56","date_gmt":"2021-01-16T03:30:56","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=80178"},"modified":"2021-01-16T09:00:56","modified_gmt":"2021-01-16T03:30:56","slug":"sys-module-in-python-with-examples","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/","title":{"rendered":"Sys Module in Python with Examples"},"content":{"rendered":"<p>Python is interesting when the coder has a logic for each code he does rather than a default function, but a defined logic makes a coder and the code even better.<\/p>\n<p>With this in mind, we are going to start what predefined logic has been given by the coders initially for the learners not only to grasp more but also to learn better.<\/p>\n<p>Sys module is one of them, reading the whole article you\u2019ll surely reach out one conclusion that \u2018 yeah, it couldn\u2019t be much better\u2019. So let\u2019s start.<\/p>\n<h2>What is Sys Module in Python?<\/h2>\n<p>Sys module in Python basically provides access to some variables used or maintained by the interpreter and some functions that interact strongly with the interpreter. It is always available in the code library.<\/p>\n<p>It provides information about constants, functions, and methods of the interpreter. But another possibility is the help() function.<\/p>\n<p>You can also change the output behavior of the Python shell interactively and even in calculation, by simply rebinding (sys.displayhook) into a callable object.<\/p>\n<p>Using help(sys) provides valuable detailed information about the interpreter and function.<\/p>\n<p><strong>Basic Code of sys() in Python<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Import sys \nprint(\u201cWelcome{}toTechVidvan{}\u201d.format(sys.argv[1], sys.argv[2]))\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Welcome{}toTechVidvan{}\u201d.format(sys.argv[1], sys.argv[2]<\/div>\n<p>Now you must be wondering what is sys.argv()? No problem indeed, the fairy basket has an answer to all of this, without wasting much time, let\u2019s get started.<\/p>\n<h3>Command-Line Arguments in Python sys Module<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import sys\nprint(sys.argv)\nfor K in range(len(sys.argv)):\n   if K==0:\n       print(\"The function is\",sys.argv[0])\n   else:   print(\"Argument:\",sys.argv[K])\n<\/pre>\n<p><strong>Sys.exit:<\/strong> This causes the program to exit in a runtime freeing the space occupied.<\/p>\n<p>sys.exit(return_file(name))<\/p>\n<p><strong>Sys.maxsize:<\/strong> Returns to the largest integer a variable can hold.<\/p>\n<p>Sys.maxsize: int(input(\u201center size\u201d))<\/p>\n<p><strong>Sys.path:<\/strong> This basically recollects and refunctions all the paths used in a module.<\/p>\n<p><strong>Sys.version:<\/strong> This is an attribute that displays a string containing the version number of the current Python interpreter.<\/p>\n<p><strong>Sys.argv:<\/strong> This is operating system dependent, and it is mainly used in algebraic codes.<\/p>\n<p>[os.fsencode(arg) for arg in sys.argv]<\/p>\n<p><strong>Sys.audit:<\/strong> This raises an auditing lookup in a code.<\/p>\n<p>sys.addaudithook<\/p>\n<p><strong>Sys.exit([arg]):<\/strong> This helps in exiting the algebraic code from python.<\/p>\n<p>sys.exit(&#8220;some error message&#8221;)<\/p>\n<p><strong>Sys.float_info:<\/strong> A tuple holding information about the float type. It has low level information about the precision and internal representation.<\/p>\n<p>Sys.float int(input(\u201center a number\u201d))<\/p>\n<p><strong>sys.getrefcount(object):<\/strong> Returns the reference count of the object.count and it is returned one higher than you might expect.<\/p>\n<p>getrefcount().<\/p>\n<p><strong>sys.getprofile():<\/strong> Get the profiler function by setprofile().<\/p>\n<p><strong>sys.gettrace():<\/strong> Get the trace function by settrace().<\/p>\n<p><strong>sys.intern(string):<\/strong> This function basically provides a platform to the user to enter the string and execute any function on it.<\/p>\n<p><strong>Sys.modules:<\/strong> This has a collection of all modules which are in built in python code. It can be manipulated to force reloading of modules and other tricks.<\/p>\n<h3>Copyright in sys()<\/h3>\n<p>sys.copyright in code displays the copyright information on the installed version of Python.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt;&gt;print(sys.copyright)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Copyright (c) 2001-2018 Python Software Foundation.<br \/>\nAll Rights are Reserved.<br \/>\nCopyright (c) 2000 BeOpen.com.<br \/>\nAll Rights are Reserved.<\/div>\n<h4>Python sys.getrefcount<\/h4>\n<p>This python sys module method returns count for references to the object where it is used. Python keeps track of it\u2019s value, as, when this value reaches 0 in a program, the memory for this variable is cleaned up.<\/p>\n<h4>Extracting Names of Modules<\/h4>\n<p>Sys has a collection of modules in its library, a coder can extract any module within the run time by:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt; import sys\n&gt;&gt;&gt; sys.modules\n<\/pre>\n<h4>Exiting Flow of Execution in Python sys Module<\/h4>\n<p>the sys.exit in Python lets the interpreter abruptly exit the current flow of execution of the code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt; sys.exit\n<\/pre>\n<h4>Redirecting the Output in Python sys()<\/h4>\n<p>Instead of delivering the output to the console, the coder can log into a text file.<br \/>\n<strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt; import sys\n&gt;&gt;&gt; print('Welcome to TechVidan')\n #Prints normally\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Welcome to TechVidan<\/div>\n<h4>Redirecting the Error Information in Python sys Module<\/h4>\n<p>Using sys.stderr and text file in Python, you can log error information to the text file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&gt;&gt;&gt; import sys\n&gt;&gt;&gt; fopen=open('error.log','w') #Opening the file\n&gt;&gt;&gt; sys.stderr=fopen #Redirecting standard error by assigning file object of file to stderr\n&gt;&gt;&gt; raise Exception('this is an error')\n&gt;&gt;&gt; fopen.close()\n<\/pre>\n<h2>Summary<\/h2>\n<p>Too much to learn, huh?<\/p>\n<p>Of course, coding requires a lot of memory, when and where to use which function, it is a skill every good coder has.<\/p>\n<p>Though learning so many functions of a sys module can be tiring but practicing them isn\u2019t. Because it&#8217;s not the end, there is so much more sys() has to offer and a lot more python.<\/p>\n<p>The above functions were some of the basic and necessary ones a coder should know, so get yourself started by practicing them!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is interesting when the coder has a logic for each code he does rather than a default function, but a defined logic makes a coder and the code even better. With this in&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":80179,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3346,3347,3348],"class_list":["post-80178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-module","tag-python-sys-module","tag-sys-module-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Sys Module in Python with Examples - TechVidvan<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sys Module in Python with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python is interesting when the coder has a logic for each code he does rather than a default function, but a defined logic makes a coder and the code even better. With this in&#046;&#046;&#046;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-16T03:30:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-SYS-Module.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sys Module in Python with Examples - TechVidvan","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/","og_locale":"en_US","og_type":"article","og_title":"Sys Module in Python with Examples - TechVidvan","og_description":"Python is interesting when the coder has a logic for each code he does rather than a default function, but a defined logic makes a coder and the code even better. With this in&#46;&#46;&#46;","og_url":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-01-16T03:30:56+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-SYS-Module.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Sys Module in Python with Examples","datePublished":"2021-01-16T03:30:56+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/"},"wordCount":749,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-SYS-Module.jpg","keywords":["python module","python sys module","Sys Module in Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/","url":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/","name":"Sys Module in Python with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-SYS-Module.jpg","datePublished":"2021-01-16T03:30:56+00:00","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-SYS-Module.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/01\/Python-SYS-Module.jpg","width":1200,"height":628,"caption":"Sys module in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/sys-module-in-python-with-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Sys Module in Python with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=80178"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80178\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/80179"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=80178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=80178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=80178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}