wtxnpw 发表于 2017-5-1 13:52:54

Python模块导入的多种方式


[*]在python中, 模块即一个后缀名为”.py”文件,python用文件组织一个模块.
[*]模块导入遵循作用域原则, 在什么作用域导入就只能在当前作用域生效.
[*]一个模块只被加载一次,无论导入它多少次.
[*]from module import name 是把名字导入到当前的名称空间

单行导入单个模块


import json

单行导入多个模块


import os, sys, time

导入指定的模块属性


from os import path, fork, mkdir

多行导入


from Tkinter import Tk, Frame, Button, Entry, Canvas, \
Text, LEFT, DISABLED, NORMAL, RIDGE, END

通配符导入(不提倡)


from os import *

用”as”将导入模块的属性重命名


import os.path as Path
from os import path as Path

相对导入
  一个”.”符号表示当前模块目录, 两个”.”符号表示当前模块的上层目录,以此类推.


from . import case, suite
from .util import strclass
from .. import name


<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>         
版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: Python模块导入的多种方式