Python SYS Module
What is Python SYS Module
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 os.py 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.
1 2 3 4 5 6 7 8 9 10 11 12 | import sys print dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode','exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions'] |
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 argv.py create a program called <url> and type the following code into it.
1 2 3 4 | #!/usr/bin/env python # -*- coding: utf-8 -*- import sys print sys.argv[1] |
Now run this program from the command line as follows.
1 2 | python argv.py test test |
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.
1 2 | import sys print sys.argv[2] |
Now let’s send a second argument.
1 2 | python argv.py test try try |
to fully understand the argv attribute, let’s make our program look like this.
1 2 3 | import sys print sys.argv |
In the same way, let’s work on the command line and look at the output we receive.
1 2 | python argv.py test try ['argv.py', 'test', 'try'] |
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. ac.py create a file named <url>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os def ac(): if os.name == "nt": os.startfile(sys.argv[1]) #if os is windows elif os.name == "posix": os.system("xdg-open %s" %sys.argv[1]) #if os is linux if(len(sys.argv)>2) print "too much file tried." else: ac() <!--2--> |
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
1 2 3 4 5 6 7 8 9 10 11 12 | import sys isim = raw_input("study:").lower() if isim == "universty": print "welcome." else: print "this program cant open for you." sys.exit() print "if program cant open users cannot see this." Study:universty this program cant open for you. |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import sys for i in sys.path: print i /home/mazlumagar/workspace/Python/sys /home/mazlumagar/workspace/Python /usr/lib/python2.7 /usr/lib/python2.7/plat-linux2 /usr/lib/python2.7/lib-tk /usr/lib/python2.7/lib-old /usr/lib/python2.7/lib-dynload /usr/local/lib/python2.7/dist-packages /usr/lib/python2.7/dist-packages /usr/lib/python2.7/dist-packages/PIL /usr/lib/python2.7/dist-packages/gst-0.10 /usr/lib/python2.7/dist-packages/gtk-2.0 /usr/lib/python2.7/dist-packages/ubuntu-sso-client /usr/lib/pymodules/python2.7 |
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.
1 | sys.path.append('/dizin/altdizin') |
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.
1 2 3 | import sys print sys.version_info sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0) |