Python basic 6 module knowledge

Source: Internet
Author: User

The main contents of this section:

    1. referencing other modules
    2. Module names
    3. 2 Examples of simple standard libraries
    4. Practice writing your own module
    5. Reference pages

The power of Python is that he has a rich and powerful standard library and a third library.

referencing other modules

To use a module, we must first import the module. Python imports a module using the import statement. For example, import the module that comes with the system, math:

import math

Just want to import a few functions of the math module used, not all functions, you can use the following statement:

import pow, sin, log

Call the Math module's function:

print math.pow(2,10)

A package is a folder. The module is the xxx.py file.

Module names

When different people write more than one module, it is likely that there is a problem with the name of the module, the name of the module as long as it is placed under a different directory. For example: under the P1 directory there is a UTIL.PY,P2 directory under the util.py. A reference to the response can be used: P1.util P2.util For example: The Math module has a log function, and the logging module also has a log function, how to use it at the same time.

Method One

Import imports the module name, which refers to the function name through the module name, and there is no conflict:

import math, loggingprint math.log(10)   # 调用的是math的log函数logging.log(10, ‘something‘) # 调用的是logging的log函数
Method Two

From...import Importing the log function is bound to cause a conflict. At this point, you can give the function an "alias" to avoid the conflict:

from math import logfrom logging import log as logger # logging的log现在变成了loggerprint log(10) # 调用的是math的loglogger(10, ‘import from logging‘) # 调用的是logging的log
2 Examples of simple standard libraries Sys
import sys #导入sys模块print(sys.path) #输出python的环境变量print(sys.argv[2]) #带参数的输出
Os
    • Example One
import os #倒入os模块cmd_res = os.system("pwd") #传入并执行系统命令pwdprint("-->",cmd_res)

The execution result of the pirnt is 0,os.system ("pwd") output to the screen, no further input into the variable, 0 represents the result of the execution.

    • Example Two
import os #倒入os模块cmd_res = os.popen("pwd") #传入并执行系统命令pwdprint("-->",cmd_res)

The execution results of Os.popen ("pwd") are stored in a place in memory where print prints the memory object address. It needs to be read out to read the execution results from memory with read.

import os cmd_res = os.popen("pwd").read()print("---->",cmd_res)
    • Example Three
os.mkdir("new_dir") #创建新目录
Os,system Comprehensive Example
os,sysos.system(‘‘.join(sys.argv[1:])) #把用户的输入的参数当作一条命令交给os.system来执行
Practice writing your own module

For example, write a login.py content as follows, as a login module:

_name =  "Cathy" _password = " 123456 "username = input  ( "username:") password = input ( "password") if username = _name and password = _password: print (" Welcome user {Name} login ... ". Format (name= username)) else: print ( "Invalid username or password!")       

Apply the login module elsewhere.

import login

The above-mentioned login.py module can only be imported in the current directory, if you want to use the system where and how to do it? Now you're going to put this login.py in the Python Global environment variable directory and print (Sys.path) to see a list of Python environment variables.

Reference pages

Http://www.cnblogs.com/alex3714/articles/5465198.html

http://www.imooc.com/code/6075

Python basic 6 module knowledge

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.