zoukankan      html  css  js  c++  java
  • python 基础 7.0 import 导入

    一. python 常用内置模块的使用(datetime,logging,os,command)
          在日常的开发工作中,我们要写很多的python 代码,如果都写在一个文件中,会导致代码特别难维护,为了拓展代码的可维护性,我们把函数写在不同的文件里,这样每个文件包含的文件就比较少,逻辑更加清楚。在python中,我们创建的文件基本都是以py结尾,那一个py的文件就称之为模块。
         为了方便管理模块,python中又引入了包(package)这个概念,每个包下面都有一个__init__.py 文件,这个文件是必须存在的,否则,python 就把这个目录当成普通目录,而不是一个包。__init__.py 可以是空文件,也可以有python 代码,因为__init__.py 本身就是一个模块,举个例子:test 目录下面有__init__.py aaa.py  , bbb.py 三个文件,所以python 会把test 当成一个模块。如果test 目录下没有__init__.py 则pyhon 会把test当成一个普通目录。
         
     
    二 . import 导入
       1.  有时候一个文件或者一个包中已经出现了一个函数,我们在另一个python代码中需要引入该文件或者该文件某个函数,那怎么解决?python给我提供一个关键字,import
    1》如果是本地导入文件,直接使用: import filename
    2》 如果导入的是一个包,该包下面必须是有__init__.py 文件才可以导入,否则报错,只有有了__init__.py 文件,python 解析器才会把这个目录当成包。
     
    2. 目录结构说明
     
    新建modules 目录---》 新建lzc包(python package),会自动生成__init__.py---》在lzc包下创建test.py 脚本----》与lzc包同级目录下创建模块.py 脚本。
     
    cat test.py
        def hello():
        print 'hello world'
     
     
     
      二. 常用导入模块的常用格式:
       1.  示例:在模块.py 里调用  test中hell函数
     
    #/usr/bin/python
    #coding=utf-8
    #@Time   :2017/11/8 10:34
    #@Auther :liuzhenchuan
    #@File   :模块.py
     
    print '##'*5+'方法一'+'##'*5
    #方法一 :导入hello模块并打印
    import lzc.test
    lzc.test.hello()
     
    print '##'*5+'方法二'+'##'*5
    #方法二. 使用from ....import 导入,从什么模块中导入什么
    from lzc import test
    test.hello()
     
    print '##'*5+'方法三'+'##'*5
    # #方法二. 使用from ....import 导入,从什么模块中导入什么,最终可以导入的可以示一个函数,一个类,也可以是一个模块
       #总之一层一层的调用就可以了
       #注意,import 后面导入的是什么,在调用的时候后面就的从什么开始写。
    from lzc.test import hello
    hello()
     
     
    print '##'*5+'方法四 import 特殊用法'+'##'*5
    from lzc import test as aa
    aa.hello()
     
     
    >>>
    ##########方法一##########
    hello world
    ##########方法二##########
    hello world
    ##########方法三##########
    hello world
    ##########方法四 import 特殊用法##########
    hello world
     
     
    2.调用test模块中的类
     
    cat test.py
    class ren(object):
        name = 'yangyang'
        sex = 'F'
        jineng = 'cisha'
        def __init__(self):
            age = '10'
            coller = 'yellow'
        def get_jineng(self):
            return self.jineng
    class child(ren):
        sex = 'M'
        def __init__(self):
            print 'my sex is {0}'.format(self.sex)
     
    girl = child()
    print girl.name
    print girl.get_jineng()
     
    >>>
    my sex is M
    yangyang
    cisha
     
    在模块.py 中调用
    from lzc import test
    test.girl()
    >>>
    my sex is M
    yangyang
    cisha
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    php中防盗链使用.htaccess
    使用Apache的.htaccess就可以防盗链
    左连接和右链接的区别?
    大量查询SQL语句 实例
    java项目打包
    java小项目
    哈夫曼树
    广义表
    树和森林的遍历
    根据前序中序,中序后序建立二叉树
  • 原文地址:https://www.cnblogs.com/lzcys8868/p/7808101.html
Copyright © 2011-2022 走看看