模块和包

import yitian as yt # pycharm报错, 导入模块的时候. 我们看到的: 把模块中的代码执行了一遍
# 模块的搜索路径. sys.path
# import sys
# print(sys.path)
# import yitian # 如果已经导入过该模块. 此时不会再执行模块中的代码了

# 导入模块的时候:
# 1. 去判断当前正在导入的模块是否已经倒入过
# 2. 如果已经导入过,不会重新导入该模块
# 3. 如果没有导入过. 首先开辟一个内存空间
# 4. 把该模块中的代码放在新开辟的空间中. 运行该模块中的代码
# 5. 把该文件的名字作为当前名称空间的名字(前提是没有as)

# print(yt.main_person_man)
# print(main_person_man) # 当前名称空间中没有main_person_man

# yt.fight_on_light_top()
# yt.fight_on_shaolin()
# yt.end()


# yt.main_person_man = "宝宝" # 从另一个模块中想改变模块中变量的值
# yt.fight_on_light_top()


# def gai():
#     global main_person_man # global 当前模块的全局
#     main_person_man = "龙霸天"
#     yt.fight_on_light_top()
#
# gai()
# yt.fight_on_light_top()

# yt.gai()
# yt.end()
#
# print(yt.main_person_man)

# import yitian as yt, shediao as sd, time, os, sys,json, re
#
# sd.huashanlunjian()
# yt.fight_on_shaolin()

# 顺序:
# 1. 先导入内置模块
# 2. 第三方模块
# 3. 你自己定义的模块


# 此时导入的只有一个功能
# from yitian import fight_on_shaolin
# from shediao import fight_on_shaolin

# main_person_man = "火工头陀"
# fight_on_shaolin()

# def fight_on_shaolin():
#     print("我要干少林")

# fight_on_shaolin()

# from shediao import *  # 也可以导入一大堆名字. 不推荐

import shediao

  

原文地址:https://www.cnblogs.com/work14/p/10196302.html