Day 14 模块
一 modules definition 定义
二 modules advantage 为什么用模块
Some of the advantages of using modules in your Python code are: they enable you to organize your code into smaller pieces that are easier to manage. the code in the modules can be reloaded and rerun as many times as needed, which enables code reuse.
三 Using a module 使用模块
Create a Module 创建
To create a module just save the code you want in a file with the file extension .py:
#文件名:foo.py
x=1
def get():
print(x)
def change():
global x
x=0
class Foo:
def func(self):
print('from the func')
Use a Module 使用
Now we can use the module we just created, by using the import statement:
The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):
import foo #导入模块foo
a=foo.x #引用模块foo中变量x的值赋值给当前名称空间中的名字a
foo.get() #调用模块foo的get函数
foo.change() #调用模块foo中的change函数
obj=foo.Foo() #使用模块foo的类Foo来实例化,进一步可以执行obj.func()
Naming a Module 命名
You can name the module file whatever you like, but it must have the file extension .py
Re-naming a Module 重命名
You can create an alias when you import a module, by using the as keyword:
From ...import 导入具体功能
You can choose to import only parts from a module, by using the from keyword.
Note: When importing using the from keyword, do not use the module name when referring to elements in the module. Example: person1["age"], not mymodule.person1["age"]
from foo import x,get,change #将模块foo中的x和get导入到当前名称空间
a=x #直接使用模块foo中的x赋值给a
get() #直接执行foo中的get函数
change() #即便是当前有重名的x,修改的仍然是源文件中的x
四 sys.path in Python 环境变量
sys module
Sys is a built-in Python module that contains parameters specific to the system i.e. it contains variables and methods that interact with the interpreter and are also governed by it.
sys.path
sys.path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module.
When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules. If not found it looks through the list of directories(a directory is a folder that contains related modules) defined by sys.path.
Initializing sys.path
There are three ways to specify a path :
DEFAULT
By default, the interpreter looks for a module within the current directory. To make the interpreter search in some other directory you just simply have to change the current directory. The following example depicts /dɪˈpɪkt/ (描述) a default path taken by the interpreter:
# importing module
import sys
# printing all directories for
# interpreter to search
sys.path
THROUGH ENVIRONMENT VARIABLES
An environment variable that contains the path an interpreter can take while looking for modules can be employed. Once set, it hints interpreter with directories to locate a module. The following example shows how this can be done.
PYTHONPATH=C:\Users\Vanshi\Desktop
APPENDING PATH
append() is a built-in function of sys module that can be used with path variable to add a specific path for interpreter to search. The following example shows how this can be done.
# importing module
import sys
# appending a path
sys.path.append('C:/Users/Vanshi/Desktop')
# printing all paths
sys.path