Chapter IX, import and from ... import

Chapter IX, import and from ... import

A, import and from ... import ...

The python seen as mobile phones, pip seen as a housekeeper applications, third-party application module is a library inside the application butler. import and import from ... import python is the process of using the module

Two, import the module name

Our import timemodule as an example

import this time, when there were three things

  • Generating a namespace called time in memory

  • Run time files, is to put the name of the file space time.py time namespace

  • time.py execute it in the current file, the time to get the module name

    Use Import timetime, only with time. Method name (), can not be directly method name ()

    Direct method name you want to use, then you from time importmethods name

    import time    #导入time模块
    
    time.time()
    time.sleep()
    -------------------------------------------------------------
    import time,os,requests  #导入多个模块
    #推荐使用下面的方式
    import time
    import os
    import requests

## three, from the module name import specific functions

from time import time     #从time模块导入time方法,也可以多个导入#from time import sleep,time(其实还可以from 模块名 import里面的全局变量)
time(1) #直接调用方法
sleep()

from ... import ... first import module happened three things:

  1. Create a module to module, whichever namespace
  2. Execution module corresponding file, will perform the namespace name generated in the process are thrown into the module
  3. Get a name in the current namespace executable file, the name of the module directly to a particular name, which means you can not add any prefix directly
  • Pros: do not add a prefix code more streamlined
  • Disadvantages: easily conflict with the currently executing file namespace name

Guess you like

Origin www.cnblogs.com/demiao/p/11366228.html