#############################################
############### #module #######################
#############################################
in Python, a. py file is called a module.
Greatly improve the maintainability of the Code;
Writing code does not have to start from scratch. When a module is written, it can be referenced elsewhere
1. Simple invocation of the module
lianxi.py
def add (x, y):
Return X+y
Def Jian (x, y):
return x-y
def multi (x, y): # #作为一个模块
Return X*y
def divi (x, y):
return x/Y
Import Lianxi # #调用lianxi模块, pour directly into the module, using Lianxi.add (x, y)
From Lianxi Import Jian # #导入lianxi模块里面的add方法
From Lianxi import add as Myadd # #如果当前代码包含add方法, but still want to use the Add function in the Lianxi module, you can rename the function
Print Jian (10,2)
Print Myadd
Print Lianxi.divi (5,2)
2. Add a path to allow the path search to import the appropriate custom module
The steps are as follows:
Import Sys
Sys.path
Sys.path.append (")
Search by Path
In [1]: Import sys
In [3]: Sys.path.append ('/root/pycharmprojects/pythonbasic/day5 ')
# #添加路径
In [4]: Sys.path.insert (0, '/root/pycharmprojects/pythonbasic/day6 ')
In [5]: Sys.path
OUT[5]:
['/root/pycharmprojects/pythonbasic/day6 ',
‘‘,
'/usr/bin ',
'/usr/lib64/python27.zip ',
'/usr/lib64/python2.7 ',
'/usr/lib64/python2.7/plat-linux2 ',
'/usr/lib64/python2.7/lib-tk ',
'/usr/lib64/python2.7/lib-old ',
'/usr/lib64/python2.7/lib-dynload ',
'/usr/lib64/python2.7/site-packages ',
'/usr/lib64/python2.7/site-packages/gtk-2.0 ',
'/usr/lib/python2.7/site-packages ',
'/usr/lib/python2.7/site-packages/ipython/extensions ',
'/root/.ipython ',
'/root/pycharmprojects/pythonbasic/day5 ']
In [7]: Import Hello # #引入模块
In [8]: Hello.hello1 () # #路径搜索
Hello1 in Day 6
3. Introduction of the Package
when the module is introduced into the package, format 1 is: Package. Module use method for package. Module. Method ()
format 2 is: Import Package use Method: Package. Module. Method (), if required modules are imported in __init__.py
Modules in Python