• Python学习之路(十九):基础知识之模块导入


    我们已经知道模块是已经写好的一些功能的集合或者别人写好的函数、变量 、方法 、放在一个文件中,这个文件可以被我们直接使用。但在使用时会有不同的方法,并且我们自己也能够创建自己的模块,实现自己想要的功能。

    1.如何创建自己的模块?

    创建一个Py文件,给它起一个符合变量名命名规则的名字,这个文件就可以是一个模块

    1 在当前目录下创建my_module.py文件,并且在文件中创建函数:
    2 def read1():
    3     print('in the module')
    4 
    5 def read2():
    6     print('dhj')

    我们便能在该目录下的其他程序中调用这个模块

    1 import my_module
    2 my_module.read1()
    3 my_module.read2()

    2.所导入模块的命名空间

    当导入一个模块时,模块中变量的名字有着独立的空间,与执行文件程序中的变量不会产生冲突。

    1 import my_module
    2 def read1():
    3     print('module')
    4 read1()
    5 my_module.read1()

    3.模块能被重复导入吗?

    模块被导入过后,再次导入时会应用上一次导入到内存中的内容,不会将模块重复加载到内存中。如果想看模块是否被导入,可以执行:

    1 import sys
    2 print(sys.modules)

    4.模块可以取别名

    1 import my_module as m
    2 m.read1()  # 在起别名之后再想要调用模块中的内容,必须使用别名

    给模块起别名,一定程度上能够简化程序:

    1 def func(dic, t = 'json'):
    2     if t == 'json':
    3         import json
    4         return json.dumps(dic)
    5     elif t == 'pickle':
    6         import pickle
    7         return pickle.dumps(dic)

    可以将模块起别名:

    1 def func(dic, t='json'):
    2     if t == 'json':
    3         import json as seq
    4     elif t == 'pickle':
    5         import pickle as aeq
    6     return seq.dumps(dic)

    5.导入多个模块

    可以一行导入多个

    1 import os, time

    建议一行一行导入多个模块
    导入顺序:内置模块、第三方模块、自定义模块

    6.从模块中取出所有变量进行调用

    1 from my_module import *
    2 name = 'egon'
    3 print(name)
    4 read1()
    5 read2()

    其中__all__变量能够限制导入的变量个数,在my_module文件中加入该变量:

    1 __all__ = ['read1']
    2 def read1():
    3     print('in the module')
    4 
    5 def read2():
    6     print('dhj')

    此时只能调用read1

    7.模块的加载与修改

    已经导入的模块已经保存到内存中,再返回修改模块中的内容,执行的时候不会体现,要想体现:

    1 import importlib
    2 importlib.reload(my_module)

    8.模块导入的路径

    1 import sys
    2 print(sys.path)
    3 path = r'D:untitled2路径'  # 导入其他路径下的模块时,将包含模块的文件夹路径添加到路径列表中
    4 sys.path.append(path)
    5 import a

    9.从包中导入

    包就是文件夹中有一个__init__.py文件,创建时为创建python package文件夹,自带一个__init__.py文件,可以通过程序创建这样的包:

     1 import os
     2 os.makedirs('glance/api')
     3 os.makedirs('glance/cmd')
     4 os.makedirs('glance/db')
     5 open('glance/api/异常处理.py', 'w').close()
     6 open('glance/api/policy.py', 'w').close()
     7 open('glance/api/version.py', 'w').close()
     8 open('glance/cmd/manage.py', 'w').close()
     9 open('glance/cmd/异常处理.py', 'w').close()
    10 open('glance/db/_init__.py', 'w').close()
    11 open('glance/db/models.py', 'w').close()

    (1)import 方式从包中导入模块

    1 import glance.api.policy
    2 glance.api.policy.get()

    (2)from import 方式导入

    1  from glance.api import policy  # 点的左边必须是包
    2 policy.get()

     

  • 相关阅读:
    周末之个人杂想(十三)
    PowerTip of the DaySorting Multiple Properties
    PowerTip of the DayCreate Remoting Solutions
    PowerTip of the DayAdd Help to Your Functions
    PowerTip of the DayAcessing Function Parameters by Type
    PowerTip of the DayReplace Text in Files
    PowerTip of the DayAdding Extra Information
    PowerTip of the DayPrinting Results
    Win7下IIS 7.5配置SSAS(2008)远程访问
    PowerTip of the DayOpening Current Folder in Explorer
  • 原文地址:https://www.cnblogs.com/Studying-Du/p/12488840.html
Copyright © 2020-2023  润新知