Python SYS Module


What is Python SYS Module

python sys
python sys

the sys module contains functions and attributes related to the Python system. So thanks to this module, we can manage the version of Python we use.
When we examined modules, we said that modules in Python were actually a Python program. So the OS module is actually a program called. But there are some exceptions. sys module is one of them.the sys module is written in C programming language, not Python programming language.
As with everything else, we can use the dir() function to find out the contents of the sys module.

As you can see, the sys module contains a lot of functions and attributes. We will examine some of these.

Argv Attribute

The arguments we send when executing Python on the command line are sent as elements of the argv attribute. Now create a program called <url> and type the following code into it.

Now run this program from the command line as follows.

As you can see, he wrote the test statement on the screen. Here is the argv attribute when working on the command line, as well as an additional argument. We can send more than one argument.

Now let’s send a second argument.

to fully understand the argv attribute, let’s make our program look like this.

In the same way, let’s work on the command line and look at the output we receive.

As you can see, we got a list of output. You may have noticed that the first element on this list is the name of our program. We can also do all the operations on the lists. Now one. create a file named <url>

As you can see, when running the program from the command line, we want the name of the file it wants to open as an argument. We open the program according to the operating system with the attributes that we process in the OS module. Notice that we use 2 when counting the arguments sent here. If you remember, the first element of the list that the argv attribute holds was the name of the program itself. Therefore, the number of elements of this list is already at least one.

exit() Function

As you can see, if the user does not have university education, the program ends. there is no extra operation performed by the Exit() Function.

Path Attribute

When Python imports a module, Python searches for that module in directories that show the path attribute.

As you can see, we’ve listed where Python is looking for modules. returns a list of backward path attributes. You can also add,remove, etc.on the list.

Python will now look into the directory we added when importing modules into it. We should know that after the program is finished, the list goes back to its original state. The directories we add are valid only for the program we are working with.
When Python searches for a module, it ends the search process after it performs the Find operation. This means that if a module is in two directories, the module will be imported in the directory that is in the first directory of the list.

Version_info Attribute

the version_info attribute returns information about the version of Python used.