- exception
try:
num = int(input("请输入一个整数"))
result = 8 / num
print(result)
except ZeroDivisionError :
print("不可以输入零")
except ValueError :
print("请勿输入除整数外的字符")
except Exception as result :
#if we can not find the error ,can do like this
print("未知错误 %s"%result)
else :
#if there is no exception ,will be excuted here.anyone exception appreaced , it can never work
finally:
#it ignore wether there is a exception ,it must be worked
- raise exception
def input_password():
pwd = input("input password")
if len(pwd) >=8:
return pwd
print("raise exception")
ex = Exception("password is too short")
raise ex
try:
print(input_password())
except Exception as result:
print(result)
- assert
def func(num, div):
assert (div != 0), "input is error 0"#
running and check the error,will stop and show this error
return num / div
print(func(5,0))
- import:
import module1,module2
import module1 as m1
#when module1's name is too long can called it m1
from module import function_name
#if we just need a little functions from a module, we can use this to import it ,and
when we use the functions ,need not to use module's name
file: a.py(def aa())
file: b.py
from a.py import aa()
aa()
from a.py import * #it can import the whole function in a.py
if we import two module which the same name function in the two module, the latter function will rewrite the farther one.it can not rewrite after we renamed.
- __file__
can show where is the file
import random
import
random
if
__name__ ==
"__main__"
:
print
(__name__)
print(random.__file__)
- __name__
if a module want to text it function or works to show something which need not show for the importer .we can add the "__name__" in the module for text ,"__name__" will be change to "__main__"when the module excuted by itself;will be change to "module_name"
def main() :
print
if __name__ =="__main__" :#when we can put the text in the main function
print
- Package
a Package must need one file :__init__.py , it tells the importer which function can be import
Package_name: h_message
__init__py:
from . import send_message
from . import receive_message
receive_message.py:
def receive():
return "this is come from .."
send_message.py:
def send(text):
print("%s"%text)
importer:
import h_message
print(h_message.receive_message.receive())
h_message.send_message.send("uk")
- publish module
create "setup.py" in the directory which include the the Package of list 6 :
the tree like this: the root dirctory is D:\workplace\publish_mudole_text
setup.py
│
├─.idea
│ │ misc.xml
│ │ modules.xml
│ │ publish_mudole_text.iml
│ │ workspace.xml
│ │
│ └─inspectionProfiles
└─h_message
receive_message.py
send_message.py
__init__.py
setup.py:
from distutils.core import setup
setup(
name = "h_message",
version="1.0",
description="it is send and receive message module",
author="chis",
url="chistoiy.top",
py_modules=["h_message.send_message",
"h_message.receive_message"]
)
after setup.py:
D:\workplace\publish_mudole_text>python36 setup.py build
D:.
│ setup.py
│
├─.idea
│ │ misc.xml
│ │ modules.xml
│ │ publish_mudole_text.iml
│ │ workspace.xml
│ │
│ └─inspectionProfiles
├─
build
│ └─lib
│ └─h_message
│ receive_message.py
│ send_message.py
│ __init__.py
│
└─h_message
receive_message.py
send_message.py
__init__.py
after this:
D:\workplace\publish_mudole_text>python36 setup.py sdist
the dirctory is :
D:\workplace\publish_mudole_text>tree /f
卷 系统 的文件夹 PATH 列表
卷序列号为 2D07-64DF
D:.
│ MANIFEST
│ setup.py
│
├─.idea
│ │ misc.xml
│ │ modules.xml
│ │ publish_mudole_text.iml
│ │ workspace.xml
│ │
│ └─inspectionProfiles
├─build
│ └─lib
│ └─h_message
│ receive_message.py
│ send_message.py
│ __init__.py
│
├─
dist
│ h_message-1.0.tar.gz
│
└─h_message
receive_message.py
send_message.py
__init__.py
if we want to install this tar.gz module:
python36 setup.py install
>>> import h_message as h
>>> h.send_message("nihao")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>> h.send_message.send("nihao")
nihao
uninstall this module:
>>> print(h_message.__file__)
D:\workplace\publish_mudole_text\dist\h_message-1.0\h_message-1.0\h_message
\__init__.py
delete this dictory is ok.
- pip install module
in linux:
for 2.x:
pip install module_name
pip uninstall module_name
for 3.x:
pip3 install module_name
pip3 install module_name
pip list#to show the list of module