I. Standard template using built-in module SYS
#hello. py file
#!/usr/bin/env Python3
#-*-Coding:utf-8-*-#本文件使用utf-8 encoding
' A test module ' #任何模块代码的第一个字符串都被视为模块的文档注释, does not print output
__author__ = ' Michael Liao ' #作者名
Import SYS #导入模块sys
def test ():
args = sys.argv #SYS.ARGV is a list that stores a string of command-line input
Print (args)
If Len (args) ==1:
Print (' Hello, world! ')
Elif len (args) ==2:
Print (' Hello,%s! '% args[1])
Else
Print (' Too many arguments! ')
If __name__== ' __main__ ': #函数入口, the direct runtime executes the statement under T, and when used as a module, does not output the following statement;
Test ()
Second, call the Hello module
1. The modulation module does not use if __name__== ' __main__ ':
A. New get.py file Call hello.py
#get. py
#!/usr/bin/env Python3
#-*-Coding:utf-8-*-
' A test module '
__author__ = ' Sume ' #作者名
From Hello Import Test #导入模块hello
Def get ():
Print (' Lalala ')
Get ()
Test ()
b.hello.py file
#-*-Coding:utf-8-*-
' A test module '
__author__ = ' Michael Liao ' #作者名
Print (' The first print ') #添加此处print
Import SYS #导入模块sys
def test ():
args = sys.argv #sys. argv is a list that stores the string entered by the command line
Print (args)
If Len (args) ==1:
Print (' Hello, world! ')
Elif len (args) ==2:
Print (' Hello,%s! '% args[1])
Else
Print (' Too many arguments! ')
Test ()
2. The modulated module uses the if __name__== ' __main__ ':
a.get.py file
#!/usr/bin/env Python3
#-*-Coding:utf-8-*-
' A test module '
__author__ = ' Sume ' #作者名
From Hello Import Test #导入模块hello
Def get ():
Print (' Lalala ')
Get ()
Test ()
b.hello.py file
#-*-Coding:utf-8-*-
' A test module '
__author__ = ' Michael Liao '
Print (' The first print ') #添加此处print
import sys #导入模块sys
def test ():
args = sys.argv < br> print (args)
if Len (args) ==1:
Print (' Hello, world! ')
elif len (args) ==2:
print (' Hello,%s! '% args[1]
Else:
print (' Too many arguments! ')
if __name__== ' __main__ ': # add here
Test ()
Use of Python modules