python3:高级-包管理code

模块部分:

01.py

# 包含一个学生类,
# 一个sayhello函数,
# 一个打印语句

class Student():
    def __init__(self, name="NoName", age=18):
        self.name = name
        self.age = age


    def say(self):
        print("My name is {0}".format(self.name))


def sayHello():
    print("Hi, 欢迎来到图灵学院!")

print("我是模块p01呀,你特么的叫我干毛")

02.py

# 借助于importlib包可以实现导入以数字开头的模块名称
import importlib

# 相当于导入了一个叫01的模块并把导入模块赋值给了tuling
tuling = importlib.import_module("01")


stu = tuling.Student()
stu.say()

p01.py


# 包含一个学生类,
# 一个sayhello函数,
# 一个打印语句

class Student():
    def __init__(self, name="NoName", age=18):
        self.name = name
        self.age = age


    def say(self):
        print("My name is {0}".format(self.name))


def sayHello():
    print("Hi, 欢迎来到图灵学院!")



# 此判断语句建议一直作为程序的入口
if __name__ == '__main__':

    print("我是模块p01呀,你特么的叫我干毛")

p02.py

import p01

stu = p01.Student("xiaojign", 19)

stu.say()

p01.sayHello()

p03.py

import p01 as p

stu = p.Student("yueyue", 18)
stu.say()

p04.py

from p01 import Student, sayHello


stu = Student()
stu.say()
sayHello()

p05.py

from p01 import *


sayHello()

stu = Student("yaona", 28)
stu.say()

p06.py

import sys

print( type(sys.path ))
print( sys.path )

for p in sys.path:
    print(p)

p07.py

import pkg01

pkg01.inInit()

p08.py

import pkg01.p01

stu = pkg01.p01.Student()
stu.say()

p09.py

from pkg01 import *

inInit()

stu = Student()

p10.py

from pkg02 import *

stu = p01.Student()
stu.say()

inInit()

包部分

pkg01

__init__.py


def inInit():
    print("I am in init of package")

pkg02

__init__.py


__all__=['p01']

def inInit():
    print("I am in inti of pacakge")

本博文所有内容均来自免费学python全系列教程全栈工程师网课

猜你喜欢

转载自blog.csdn.net/sunshine_lyn/article/details/81152285