1 00:00:00,000 --> 00:00:02,400 [no audio] 2 00:00:02,400 --> 00:00:06,200 Friends here we are going to discuss about Sys Module. See 3 00:00:06,200 --> 00:00:09,400 basically Sys Module is used to work with Python runtime 4 00:00:09,400 --> 00:00:14,500 environment. I mean while running your Python script, if you want to 5 00:00:14,500 --> 00:00:18,600 interact with your Python interpreter then Sys Module is 6 00:00:18,600 --> 00:00:22,900 helpful. Guys, generally people will get confused whenever if they 7 00:00:22,900 --> 00:00:24,500 are discussing about Sys Module. 8 00:00:24,800 --> 00:00:28,100 See generally 'Sys' means we will feel it has a system. 9 00:00:28,900 --> 00:00:29,600 Yes, of course 10 00:00:29,600 --> 00:00:34,700 it is system, but it is not your operating system. Whenever 11 00:00:34,700 --> 00:00:39,700 if you are using Sys Module in Python it is helpful to get 12 00:00:39,700 --> 00:00:42,400 Python runtime environment on your system, 13 00:00:43,400 --> 00:00:44,700 not about your system. 14 00:00:44,800 --> 00:00:47,900 It is about Python runtime environment on your system. 15 00:00:48,900 --> 00:00:51,300 Right? Fine. Now, 16 00:00:52,500 --> 00:00:56,700 this Sys Module provides some functions and variables, 17 00:00:56,900 --> 00:00:59,000 you know, any module provide always some functions and 18 00:00:59,000 --> 00:01:02,600 variables or simply some operations, and these functions on 19 00:01:02,600 --> 00:01:06,300 the variables are used to manipulate different parts of the 20 00:01:06,400 --> 00:01:08,100 Python runtime environment. 21 00:01:08,400 --> 00:01:11,500 Let's say you are using Python 3.7.0. 22 00:01:11,500 --> 00:01:13,500 [no audio] 23 00:01:13,500 --> 00:01:17,400 To get that information you can use this module. 24 00:01:17,400 --> 00:01:19,800 [no audio] 25 00:01:19,800 --> 00:01:21,200 Right? You have different things. 26 00:01:21,200 --> 00:01:24,300 I will show you one by one, right? Simply assume that 'Sys' is 27 00:01:24,300 --> 00:01:28,700 used to get Python runtime environment details. And we know 28 00:01:28,700 --> 00:01:32,200 that, if you want to use any module, not only 'Sys', what is 29 00:01:32,200 --> 00:01:35,000 the first step you have to do? Always we have to import that, 30 00:01:35,600 --> 00:01:40,300 and to list out all functions and variables, and to get some 31 00:01:40,300 --> 00:01:44,200 documentation, these two are the helpful things. 'dir' of your 32 00:01:44,200 --> 00:01:47,100 module will give the list of operations or functions and 33 00:01:47,100 --> 00:01:51,000 variables, and 'help(sys)' will give the complete documentation, 34 00:01:51,600 --> 00:01:53,400 right? So we are at initial stage. 35 00:01:53,400 --> 00:01:59,000 Let me open once again my Python terminal, right by typing 36 00:01:59,000 --> 00:01:59,900 simply 'python'. 37 00:01:59,900 --> 00:02:01,100 Let me 'import sys', 38 00:02:01,100 --> 00:02:03,800 [no audio] 39 00:02:03,800 --> 00:02:06,400 then 'dir(sys)', will you the list of 40 00:02:06,400 --> 00:02:10,699 operations or the functions which are there with your 'sys', 41 00:02:10,900 --> 00:02:12,000 and the variables. 42 00:02:12,800 --> 00:02:15,200 So by seeing all of these, you can't understand which is a 43 00:02:15,200 --> 00:02:17,700 function and which is a variable. Then how you can understand 44 00:02:17,700 --> 00:02:20,400 that? That's why you have a document here. 45 00:02:21,200 --> 00:02:23,400 So initially you are at initial stage, right? 46 00:02:23,400 --> 00:02:26,300 That's why, before going to use all these operations 47 00:02:26,700 --> 00:02:30,400 just have a glance once on your 'help', your document. 48 00:02:31,700 --> 00:02:34,900 Just go through this top to down. Just have a glance. 49 00:02:34,900 --> 00:02:37,700 Don't try to learn. Then somewhere 50 00:02:37,700 --> 00:02:39,100 you have see functions, right? Now, 51 00:02:39,100 --> 00:02:40,200 these are the functions. 52 00:02:41,600 --> 00:02:44,700 Let me go down, right. It will take some time. 53 00:02:44,700 --> 00:02:45,400 Just wait. 54 00:02:46,100 --> 00:02:46,800 Let me go down. 55 00:02:46,800 --> 00:02:49,700 Now, you have 'data', that means whatever you have here, 56 00:02:49,700 --> 00:02:51,200 they're are like variables. 57 00:02:51,500 --> 00:02:55,400 That's it. Now, you know, if you want to use variable from any module, 58 00:02:55,400 --> 00:02:56,700 you have to do simply, suppose 59 00:02:56,700 --> 00:02:58,500 let me take one of the variable called 60 00:02:59,700 --> 00:03:05,300 'platform'. So guys this is not your operating system details, 61 00:03:05,400 --> 00:03:10,800 'win32'. It is your Python software, which is installed on 62 00:03:10,800 --> 00:03:12,700 your host as a 32-bit. 63 00:03:13,000 --> 00:03:16,300 So on Windows I install my Python software as a 32-bit Python 64 00:03:16,300 --> 00:03:18,100 software. That's it. 65 00:03:18,500 --> 00:03:21,300 Let's say I have one more variable called 'version'. 66 00:03:23,200 --> 00:03:25,900 If it is a function you have to take parentheses. 67 00:03:27,100 --> 00:03:28,900 Right. Fine. 68 00:03:29,700 --> 00:03:33,300 Now, let me open a simple script. 69 00:03:34,100 --> 00:03:36,300 So let me open my 'Sublime Text'. 70 00:03:36,700 --> 00:03:39,600 So I am going to write a simple script using your Sys Module, 71 00:03:40,000 --> 00:03:43,800 right? So command line are helpful to know your documentation, 72 00:03:44,100 --> 00:03:48,600 right? But if you want to write a script just select any editor. 73 00:03:49,400 --> 00:03:51,200 I am selecting, suppose 'Sublime Text'. 74 00:03:51,400 --> 00:03:53,300 I am going to work with the Sys Module, 75 00:03:53,500 --> 00:03:55,100 so basics of 'sys'. 76 00:03:55,100 --> 00:03:57,200 [no audio] 77 00:03:57,200 --> 00:04:00,300 '.py'. So guys, I want to use this module, then 78 00:04:00,300 --> 00:04:01,100 what is the first step 79 00:04:01,100 --> 00:04:03,600 I need to do? Import Sys Module. 80 00:04:04,300 --> 00:04:06,300 So you have seen what are the list of operations which are 81 00:04:06,300 --> 00:04:09,600 there with your Sys Module by running simply 'dir' of your 82 00:04:09,600 --> 00:04:11,600 'sys'. Right. 83 00:04:11,800 --> 00:04:13,400 So these are all the operations. 84 00:04:14,400 --> 00:04:19,899 Now simple things. See I want to display the version of my 85 00:04:19,899 --> 00:04:25,300 Python. If you remember you can find out that using 86 00:04:25,300 --> 00:04:30,500 your, from command line you can run simply 'python' space 87 00:04:30,500 --> 00:04:34,300 '-- version', right, or previously 88 00:04:34,300 --> 00:04:37,400 we had a discussion on Platform Module, in Platform Module 89 00:04:37,400 --> 00:04:40,800 also you have one of the operation to get your Python version, 90 00:04:42,400 --> 00:04:46,100 fine forget about all those things, simply using 'sys' 91 00:04:46,100 --> 00:04:49,200 also you can get your Python environment details, 92 00:04:49,300 --> 00:04:54,000 that is simply 'sys.version'. I am saving and running, see the output. 93 00:04:54,200 --> 00:05:00,800 Yes, you are getting. I want to get in terms of tuple. See that, 94 00:05:02,300 --> 00:05:04,700 you're getting, right. 95 00:05:05,000 --> 00:05:08,400 So either 'version' or 'version_info', both will give your Python 96 00:05:08,400 --> 00:05:09,800 information details. 97 00:05:09,800 --> 00:05:14,100 [no audio] 98 00:05:14,100 --> 00:05:17,900 Then let me comment these two things, these two are variables, 99 00:05:17,900 --> 00:05:21,900 right? I am going to take some more variables, suppose 100 00:05:22,100 --> 00:05:23,300 I want to know 101 00:05:24,700 --> 00:05:28,700 the modules which you imported to run your Python script, 102 00:05:29,300 --> 00:05:32,500 so for that in 'sys' you have 'modules', 103 00:05:33,900 --> 00:05:37,000 'sys.modules' will list out, simply you're not going to use 104 00:05:37,000 --> 00:05:41,100 them but simply we are seeing how many modules you are importing 105 00:05:41,200 --> 00:05:44,100 to run your Python script. As of now, you're importing only 106 00:05:44,100 --> 00:05:46,800 'sys', right. But see the magic what you are getting. 107 00:05:48,400 --> 00:05:51,400 See, these all are the modules, different modules 108 00:05:51,400 --> 00:05:53,500 you are importing to run your Python script. 109 00:05:53,700 --> 00:05:56,300 Actually, you are not importing. You are importing only 'sys' 110 00:05:56,600 --> 00:06:00,400 that is there in the very first, in output but remaining all modules 111 00:06:00,400 --> 00:06:06,100 by default your Python is importing to run your Python scripts. By default 112 00:06:06,100 --> 00:06:08,400 Python is also importing some modules. 113 00:06:09,300 --> 00:06:12,400 So we are able to see that information with the help of 'sys'. 114 00:06:12,400 --> 00:06:16,500 That's why we are saying that 'sys' is a module to work with 115 00:06:16,500 --> 00:06:19,600 your Python runtime environment, right? 116 00:06:20,000 --> 00:06:20,800 Not only that. 117 00:06:22,300 --> 00:06:24,500 Let me take one more thing, 'print( 118 00:06:24,500 --> 00:06:26,600 [no audio] 119 00:06:26,600 --> 00:06:31,000 sys.path)'. So what is the 'sys.path'? First observe the 120 00:06:31,000 --> 00:06:35,900 output. You're getting some list of paths. See while running 121 00:06:35,900 --> 00:06:38,900 your Python script, your Python needs some libraries and modules, 122 00:06:38,900 --> 00:06:40,900 right? All those libraries and modules 123 00:06:40,900 --> 00:06:43,500 your Python is importing from all these paths. 124 00:06:45,100 --> 00:06:51,500 See, not only here, if you are good at your any Windows or Unix-like 125 00:06:51,500 --> 00:06:56,100 systems, right. See system is also depending on some paths. 126 00:06:57,500 --> 00:07:01,000 So these are the paths your system is using while running 127 00:07:01,000 --> 00:07:03,800 some scripts or programs on your Windows operating system, 128 00:07:04,400 --> 00:07:08,400 the same way while running Python script, Python needs some 129 00:07:08,400 --> 00:07:12,900 paths from which it's importing some modules. See by 130 00:07:12,900 --> 00:07:14,500 default, Python is importing some modules 131 00:07:14,500 --> 00:07:17,700 no, then from which location it has to import? That information 132 00:07:17,700 --> 00:07:21,800 is there in 'sys.path'. Now simply you can say that 'sys.path' 133 00:07:21,800 --> 00:07:24,800 is an environment variable for your Python, 134 00:07:26,400 --> 00:07:34,500 is an environment variable for Python. 135 00:07:34,600 --> 00:07:35,400 That's it. 136 00:07:36,500 --> 00:07:37,500 Fine. 137 00:07:37,800 --> 00:07:42,300 Okay, so these all, whatever we discussed just now, right, you 138 00:07:42,300 --> 00:07:44,900 may or may not use regularly in your real time, 139 00:07:45,100 --> 00:07:48,500 but the next thing whatever I'm going to write that you are 140 00:07:48,500 --> 00:07:49,400 going to use it. 141 00:07:50,400 --> 00:07:51,600 Right. What is that? 142 00:07:51,600 --> 00:07:52,400 Just observe here. 143 00:07:53,100 --> 00:07:54,200 See I'm writing simply 144 00:07:54,200 --> 00:07:58,200 'print("hello")', run the script, and see the output. 145 00:07:58,200 --> 00:07:59,300 Yes, you are getting your output. 146 00:07:59,300 --> 00:08:00,900 "Hello". Right. 147 00:08:01,100 --> 00:08:03,300 Now, what I am doing is, before your "hello", 148 00:08:03,600 --> 00:08:08,500 I am writing 'sys.exit' to stop my Python runtime environment, 149 00:08:08,500 --> 00:08:10,400 that means stop the script. 150 00:08:11,700 --> 00:08:15,800 See the output. You're not getting anything because actually 151 00:08:15,800 --> 00:08:19,200 whenever if you press 'Control + B', or whenever if you run your 152 00:08:19,200 --> 00:08:21,900 script from command line, something like 'python', your script 153 00:08:21,900 --> 00:08:25,200 name, you are trying to run your Python script, but what I 154 00:08:25,200 --> 00:08:27,900 am saying internally, inside of your script somewhere I am 155 00:08:27,900 --> 00:08:30,000 saying, "Please stop my Python", 156 00:08:30,700 --> 00:08:32,400 that is the meaning of this 'sys.exit'. 157 00:08:33,100 --> 00:08:36,799 So you are working with your Python saying that "Stop Python". 158 00:08:37,500 --> 00:08:42,000 So you are stopping your running Python script, right? 159 00:08:42,100 --> 00:08:44,500 So because of this 'sys.exit', 160 00:08:44,500 --> 00:08:47,700 your Python is going to stop your run, stop 161 00:08:47,700 --> 00:08:49,299 your running your Python script. 162 00:08:50,200 --> 00:08:53,900 That's why you are not able to get this output. By this line 163 00:08:53,900 --> 00:08:56,200 itself you are stopping, then you are not executing this 164 00:08:56,200 --> 00:08:59,500 line. Be clear. It will be very useful. While going forward 165 00:08:59,500 --> 00:09:03,000 I will use in different scenarios this 'sys.exit'. The simple 166 00:09:03,000 --> 00:09:06,900 meaning for 'sys.exit' is, whenever, if your Python identifies 167 00:09:06,900 --> 00:09:11,700 'sys.exit' simply you are abrupting or stopping Python 168 00:09:11,700 --> 00:09:12,600 script, that's it. 169 00:09:14,300 --> 00:09:15,900 Okay, and this is a function. 170 00:09:17,300 --> 00:09:19,000 So by default 'sys.exit' 171 00:09:19,000 --> 00:09:22,300 will give return code 0. If you want to provide any 172 00:09:22,300 --> 00:09:25,700 other than 0 return codes, you can pass your codes here. 173 00:09:26,500 --> 00:09:29,200 So how to use that code? We will see. For time being just 174 00:09:29,200 --> 00:09:33,200 assume that 'sys.exit' by default with zero as return code, 175 00:09:33,800 --> 00:09:36,300 and if you want to provide any return code, yes you can 176 00:09:36,300 --> 00:09:38,300 provide inside of your parentheses. 177 00:09:38,300 --> 00:09:40,700 That's why 'sys.exit' is a function. 178 00:09:41,900 --> 00:09:42,900 Okay. 179 00:09:43,500 --> 00:09:45,700 Okay, guys. Thank you for watching this video. 180 00:09:45,700 --> 00:09:52,400 [no audio]