from hello import hi
hi()
Пример #2
0
import hello
hello.hi()

from hello import msg
print(msg)
Пример #3
0
x = Circle(3)
print(x.area())
""" 模块导入训练 """
import hello as h
h.display()
""" 利用搜索路径来导入模块 """
import sys
print(sys.path)  # python会从这些路径中去找文件
# import test1 # 因为test1不在这个当前路径下,所以找不到
sys.path.append("D:\learnCode\learnPython\model"
                )  # 将此路径加入sys的搜索范围中,可以直接写“model”,表示当前路径添加model
# print(sys.path)
import test1
test1.display()
""" 导入包的模块 """
import package.hello as h
h.hi()
""" 学习模块测试 """
import package.moduleTest as t
print(t.cal(1, 2))
print(t.__name__)  # 在主程序中调用 模块.name是模块的名字
print(__name__)  # 而主程序的 name是main
""" 注意变量命名 """
a = (1, 2, 4)
a = list(a)  # list函数可以将元组转成列表
print(a)
list = [1, 2, 3]
# b = list(a) # 如果已经拿list做变量名,则不能用list将元组转为列表

print(dir(list))  # dir可以列出所有属性和方法
print(help(list.sort))  # help可以给出具体的属性或方法的定义
Пример #4
0
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import hello
>>> hi()
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    hi()
NameError: name 'hi' is not defined
>>> import hello as h
>>> h.hi()
I love you
>>> hello.hi()
I love you
>>> 
def say():
    print('I love China!')


# 在当前目录下 保存 hello.py 文件。
# import hell0
# 执行 hi(),  提示 不存在 hi。
#  需要命名空间, hello.hi()才能正常执行。
#
#  类似 import time  所有相关函数需要通过 time.函数名 调用
#

## 第一种: import 模块名
import hello
hello.hi()
print()

## 第二种:from 模块名 import 函数名
#
# 还有一种通配符 from 模块名 import *  加载所有函数,不需要命名空间
#  但是容易造成不同模块同名函数混淆。不建议用这种方式
from hello import say, hi
say()
hi()
print()

## 第三种:import 模块名 as 新名字
#
# 名字简写,也避免了多个模块同名函数混淆
import hello as hl
Пример #6
0
import hello

hello.hi()