Python3学习笔记:python中的模块导入操作

一、常见类型

  • 完全导入:import 模块1, 模块2,模块...
  • 完全导入别名:import ... as ...
  • 部分导入:from ... import ...
  • 部分导入别名:from ... import ... as ...
  • from ... import *

二、解释

import
找到模块加载初始化,在所在作用域局部的命名空间中,增加一个名称与模块建立关联。

from
加载初始化后面的模块,但是不会把模块名称,加入的当前名称空间

dir()
内建函数,没有参数时,返回当前局部作用域的名称列表。

三、类型

1、完全导入:import 模块1, 模块2,模块...

例一
import os, re

print(dir())    # 查看当前名称空间


# 执行结果
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'os', 're']

os, re 模块,被加载到当前的名称空间。

例二
import os.path

print(dir())    # 查看当前名称空间

# 执行结果
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'os']

当前名称空间中,只加载了os,没有加载 path 。

例三
import pathlib.Path

print(dir())    # 查看当前名称空间

# 执行结果
ModuleNotFoundError: No module named 'pathlib.Path'; 'pathlib' is not a package

Path 不是模块,只是pathlib模块里面的类

2、完全导入别名 import ... as ...

import threading as td

print(dir())    # 查看当前名称空间


# 执行结果
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'td']

当前名称空间内只有td,没有threading

完全导入总结

  • 顶级模块会加载到当前名称空间,与模块建立关联。
  • 非顶级模块,只会将其顶级部分加载到名称空间,因此访问只能通过完全的限定名来使用。例二中,只能通过 os.path 使用path
  • import 后面只能导入模块,不能是其他对象
  • 使用别名时,别名会加入名称空间,它与原模块建立绑定关系。

3、部分导入:from ... import ...

例一
from random import randint, randrange

print(dir())    # 查看当前名称空间

# 执行结果
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'randint', 'randrange']

randint, randrange 加载到名称空间

例二
from os.path import exists

print(dir())    # 查看当前名称空间

# 执行结果
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'exists']

exists 加载到当前名称空间

4、部分导入别名:from ... import ... as ...

from os.path import exists as e

print(dir())    # 查看当前名称空间

# 执行结果
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'e']

e 加载到当前名称空间

部分导入总结

  • 部分导入,只会把import后面的模块,加载到名称空间,有别名的,将别名加入名称空间。
  • 此时的import后面可以是模块内的类、函数
  • 关于加载顺序,例如: os.path,会先加载os,让后加载path,最后加载exists,但是,只有exists或者它的别名会加入当地名称空间。

5、from ... import *

例一
# test1.py 文件
A = 1
D = 4
_B = 2
__C = 3


# test2.py 文件
from test1 import *

print(dir())

# test2执行结果
['A', 'D', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

在test2中,只把test1 中 变量 A, D 导入了,对私有变量、保护变量都没有导入。

例二
# test1.py 文件
__all__ = ['A', '_B']

A = 1
D = 4
_B = 2
__C = 3

# test2.py 文件
from test1 import *

print(dir())

# test2执行结果
['A', '_B', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

test1 中 变量 A, _B 导入了test2。

例三
# test1.py 文件
__all__ = []

A = 1
D = 4
_B = 2
__C = 3

# test2.py 文件
from test1 import *

print(dir())

# test2执行结果
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

没有从test1中导入

from … import *总结

  • from ... import * 在没有__all__的限制下,只会把所有普通变量导入。
  • 在存在__all__ 的情况下,只会把__all__中的内容导入。__all__为空时,则导入的为空。
  • from ... import * ,导入简单,但是会导入大量不使用的变量,并且还会造成名称的冲突,尤其在自定义模块,最好使用__all__。

你可能感兴趣的:(Python3学习笔记)