Module Use module

Source: Internet
Author: User
Tags first string

You've learned how to define a function once in your program and reuse the code. If you want to reuse many functions in other programs, how do you write programs? As you might have guessed, the answer is to use a module. A module is basically a file that contains all of the functions and variables you define. In order to reuse modules in other programs, the file name of the module must be an extension of. py.
Modules can be entered from other programs to take advantage of its functionality. This is also the way we use the Python standard library. First, we'll learn how to use the standard library module.
Using the SYS module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example 8.1 using the SYS module

#!/usr/bin/python#Filename:using_sys.pyImportSysnum=1Print 'The comman line arguments is:' forIinchSYS.ARGV:#if num==1:#Print I    PrintIPrint '\ n \ PYTHONPATH is', Sys.path,'\ n'


Output
$ python using_sys.py We are arguments
The command line arguments is:
using_sys.py
We
Is
Arguments

The PYTHONPATH is ['/home/swaroop/byte/code ', '/usr/lib/python23.zip ', '/usr/lib/python2.3 ', '/usr/lib/python2.3/ Plat-linux2 ', '/usr/lib/python2.3/lib-tk ', '/usr/lib/python2.3/lib-dynload ', '/usr/lib/python2.3/site-packages ', ' /usr/lib/python2.3/site-packages/gtk-2.0 ']
How it works
First, we use the import statement to enter the SYS module. Basically, this statement tells Python that we want to use this module. The SYS module contains functions related to the Python interpreter and its environment.
When Python executes the import SYS statement, it looks for the sys.py module in the directory listed in the Sys.path variable. If this file is found, the statement in the main block of the module will be run and the module will be able to be used by you. Note that the initialization process only takes place the first time we enter the module. In addition, "SYS" is the abbreviation for "system".
The argv variable in the SYS module is indicated by the use of a dot--sys.argv--One advantage of this approach is that the name does not conflict with any argv variables that are used in your program. In addition, it clearly shows that this name is part of the SYS module.
The SYS.ARGV variable is a list of strings (the list is explained in detail in a later section). In particular, SYS.ARGV contains a list of command-line arguments, which are parameters that are passed to your program using the command line.
If you use the IDE to write these programs, look for a way to specify the command-line arguments of the program in the menu.
Here, when we execute the Python using_sys.py we are arguments, we use the Python command to run the using_sys.py module, and the following content is passed as a parameter to the program. Python stores it in the SYS.ARGV variable for us.
Remember that the name of the script always sys.argv the first parameter of the list. So, here, ' using_sys.py ' is sys.argv[0], ' we ' are sys.argv[1], ' is ' is sys.argv[2] and ' arguments ' is sys.argv[3]. Note that Python starts counting from 0, not starting with 1.
The Sys.path contains the list of directory names for the input modules. We can observe that the first string of Sys.path is empty-this empty string indicates that the current directory is also part of the Sys.path, which is the same as the PYTHONPATH environment variable. This means that you can enter the module directly in the current directory. Otherwise, you have to put your module in one of the directories listed in Sys.path.

Module Use module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.