新建模块,导入模块的方法

新建模块:

注意类的定义中函数声明一定要包含 self 参数

# -*- coding: utf-8 -*-
class module1:
    def __init__(self):
        print(self)

    def function1(self):
        print(self)

    def function2(self):
        print(self)

要当模块调用的py文件一般是保存在系统目录:

…Libsite-packages

当然也可以保存在其他位置,例如:

C:WinPython-32bit-3.4.4.2my_WorkSpace1module1.py

导入模块:
# -*- coding: utf-8 -*-
import sys
sys.path.append('C:WinPython-32bit-3.4.4.2my_WorkSpace1module1.py')
import module1
module1.module1.function1('调用模块成功!')
效果:

360截图20160522212923400

原文地址:https://www.cnblogs.com/blog-3123958139/p/5517821.html