python_如何建立包

时间:2023-03-08 17:07:04

步骤:

  (1)包的名称为drawing

  (2)drawing中建立模块color和shape

视图:

python_如何建立包

备注:

  (1) E:/python_script/已经加入到系统变量path中

  (2) 建立包时,包下面必须包含__init__.py的文件(此文件中可以没有任何内容)

调用包下的模块:

  (1)color.py 代码

def red():
print "the color is red"

  (2)shape.py 代码

    

def circle():
print "the shape is circle"

  (3)调用color和shape中函数的代码

#coding:utf-8

#import drawing
import drawing.color
from drawing import shape #引用的时候,需要加入包名和模块名
drawing.color.red()
#引用的时候,直接用模块名即可
shape.circle()

      运行结果:

      python_如何建立包