[no audio]
Friends here we are going to discuss about Sys Module. See
basically Sys Module is used to work with Python runtime
environment. I mean while running your Python script, if you want to
interact with your Python interpreter then Sys Module is
helpful. Guys, generally people will get confused whenever if they
are discussing about Sys Module.
See generally 'Sys' means we will feel it has a system.
Yes, of course
it is system, but it is not your operating system. Whenever
if you are using Sys Module in Python it is helpful to get
Python runtime environment on your system,
not about your system.
It is about Python runtime environment on your system.
Right? Fine. Now,
this Sys Module provides some functions and variables,
you know, any module provide always some functions and
variables or simply some operations, and these functions on
the variables are used to manipulate different parts of the
Python runtime environment.
Let's say you are using Python 3.7.0.
[no audio]
To get that information you can use this module.
[no audio]
Right? You have different things.
I will show you one by one, right? Simply assume that 'Sys' is
used to get Python runtime environment details. And we know
that, if you want to use any module, not only 'Sys', what is
the first step you have to do? Always we have to import that,
and to list out all functions and variables, and to get some
documentation, these two are the helpful things. 'dir' of your
module will give the list of operations or functions and
variables, and 'help(sys)' will give the complete documentation,
right? So we are at initial stage.
Let me open once again my Python terminal, right by typing
simply 'python'.
Let me 'import sys',
[no audio]
then 'dir(sys)', will you the list of
operations or the functions which are there with your 'sys',
and the variables.
So by seeing all of these, you can't understand which is a
function and which is a variable. Then how you can understand
that? That's why you have a document here.
So initially you are at initial stage, right?
That's why, before going to use all these operations
just have a glance once on your 'help', your document.
Just go through this top to down. Just have a glance.
Don't try to learn. Then somewhere
you have see functions, right? Now,
these are the functions.
Let me go down, right. It will take some time.
Just wait.
Let me go down.
Now, you have 'data', that means whatever you have here,
they're are like variables.
That's it. Now, you know, if you want to use variable from any module,
you have to do simply, suppose
let me take one of the variable called
'platform'. So guys this is not your operating system details,
'win32'. It is your Python software, which is installed on
your host as a 32-bit.
So on Windows I install my Python software as a 32-bit Python
software. That's it.
Let's say I have one more variable called 'version'.
If it is a function you have to take parentheses.
Right. Fine.
Now, let me open a simple script.
So let me open my 'Sublime Text'.
So I am going to write a simple script using your Sys Module,
right? So command line are helpful to know your documentation,
right? But if you want to write a script just select any editor.
I am selecting, suppose 'Sublime Text'.
I am going to work with the Sys Module,
so basics of 'sys'.
[no audio]
'.py'. So guys, I want to use this module, then
what is the first step
I need to do? Import Sys Module.
So you have seen what are the list of operations which are
there with your Sys Module by running simply 'dir' of your
'sys'. Right.
So these are all the operations.
Now simple things. See I want to display the version of my
Python. If you remember you can find out that using
your, from command line you can run simply 'python' space
'-- version', right, or previously
we had a discussion on Platform Module, in Platform Module
also you have one of the operation to get your Python version,
fine forget about all those things, simply using 'sys'
also you can get your Python environment details,
that is simply 'sys.version'. I am saving and running, see the output.
Yes, you are getting. I want to get in terms of tuple. See that,
you're getting, right.
So either 'version' or 'version_info', both will give your Python
information details.
[no audio]
Then let me comment these two things, these two are variables,
right? I am going to take some more variables, suppose
I want to know
the modules which you imported to run your Python script,
so for that in 'sys' you have 'modules',
'sys.modules' will list out, simply you're not going to use
them but simply we are seeing how many modules you are importing
to run your Python script. As of now, you're importing only
'sys', right. But see the magic what you are getting.
See, these all are the modules, different modules
you are importing to run your Python script.
Actually, you are not importing. You are importing only 'sys'
that is there in the very first, in output but remaining all modules
by default your Python is importing to run your Python scripts. By default
Python is also importing some modules.
So we are able to see that information with the help of 'sys'.
That's why we are saying that 'sys' is a module to work with
your Python runtime environment, right?
Not only that.
Let me take one more thing, 'print(
[no audio]
sys.path)'. So what is the 'sys.path'? First observe the
output. You're getting some list of paths. See while running
your Python script, your Python needs some libraries and modules,
right? All those libraries and modules
your Python is importing from all these paths.
See, not only here, if you are good at your any Windows or Unix-like
systems, right. See system is also depending on some paths.
So these are the paths your system is using while running
some scripts or programs on your Windows operating system,
the same way while running Python script, Python needs some
paths from which it's importing some modules. See by
default, Python is importing some modules
no, then from which location it has to import? That information
is there in 'sys.path'. Now simply you can say that 'sys.path'
is an environment variable for your Python,
is an environment variable for Python.
That's it.
Fine.
Okay, so these all, whatever we discussed just now, right, you
may or may not use regularly in your real time,
but the next thing whatever I'm going to write that you are
going to use it.
Right. What is that?
Just observe here.
See I'm writing simply
'print("hello")', run the script, and see the output.
Yes, you are getting your output.
"Hello". Right.
Now, what I am doing is, before your "hello",
I am writing 'sys.exit' to stop my Python runtime environment,
that means stop the script.
See the output. You're not getting anything because actually
whenever if you press 'Control + B', or whenever if you run your
script from command line, something like 'python', your script
name, you are trying to run your Python script, but what I
am saying internally, inside of your script somewhere I am
saying, "Please stop my Python",
that is the meaning of this 'sys.exit'.
So you are working with your Python saying that "Stop Python".
So you are stopping your running Python script, right?
So because of this 'sys.exit',
your Python is going to stop your run, stop
your running your Python script.
That's why you are not able to get this output. By this line
itself you are stopping, then you are not executing this
line. Be clear. It will be very useful. While going forward
I will use in different scenarios this 'sys.exit'. The simple
meaning for 'sys.exit' is, whenever, if your Python identifies
'sys.exit' simply you are abrupting or stopping Python
script, that's it.
Okay, and this is a function.
So by default 'sys.exit'
will give return code 0. If you want to provide any
other than 0 return codes, you can pass your codes here.
So how to use that code? We will see. For time being just
assume that 'sys.exit' by default with zero as return code,
and if you want to provide any return code, yes you can
provide inside of your parentheses.
That's why 'sys.exit' is a function.
Okay.
Okay, guys. Thank you for watching this video.
[no audio]