标签:
30、模块:分为内置模块,自定义模块,第三方模块
1、为什么要有模块:
将代码归类,方便调用。
2、导入模块的依据
根据 sys.path路径去找,先找当前目录,在按照sys.path中去找.
3、模块的名称
一定不要将自己定义的模块名称和内置模块的名称相同。
4、导入模块的方式
#1 from xx import xxx
>>> from sys import path
>>> path
[‘‘, ‘/usr/local/python3/lib/python35.zip‘, ‘/usr/local/python3/lib/python3.5‘, ‘/usr/local/python3/lib/python3.5/plat-linux‘, ‘/usr/local/python3/lib/python3.5/lib-dynload‘, ‘/usr/local/python3/lib/python3.5/site-packages‘]
#2 from xxx import xxx as xxx(别名)
>>> from sys import path as ipath
>>> ipath
[‘‘, ‘/usr/local/python3/lib/python35.zip‘, ‘/usr/local/python3/lib/python3.5‘, ‘/usr/local/python3/lib/python3.5/plat-linux‘, ‘/usr/local/python3/lib/python3.5/lib-dynload‘, ‘/usr/local/python3/lib/python3.5/site-packages‘]
5、os.system()和os.popen()在linux中的应用:
>>> s1 = os.popen(‘df -h‘)
>>> for i in s1:
... print(i)
...
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 4.9G 2.8G 1.9G 61% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 485M 33M 427M 8% /boot
/dev/mapper/VolGroup-LogVol00 13G 160M 12G 2% /home
>>> s2 = os.system(‘df -h‘)
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 4.9G 2.8G 1.9G 61% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 485M 33M 427M 8% /boot
/dev/mapper/VolGroup-LogVol00 13G 160M 12G 2% /home
>>> print(s2)
0
6、第三方模块安装方法:以安装requests 为例。
#1、pip安装pip install package
[root@localhost ~]# python3 -m pip install requests
Collecting requests
Downloading requests-2.11.0-py2.py3-none-any.whl (514kB)
100% |████████████████████████████████| 522kB 46kB/s
Installing collected packages: requests
Successfully installed requests-2.11.0
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the ‘pip install --upgrade pip‘ command.
[root@localhost ~]# python3
Python 3.5.2 (default, Jul 21 2016, 22:25:33)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> import tab
>>> requests
requests
>>> requests.
requests.ConnectTimeout( requests.__delattr__( requests.__path__ requests.get(
requests.ConnectionError( requests.__dict__ requests.__reduce__( requests.head(
requests.DependencyWarning( requests.__dir__( requests.__reduce_ex__( requests.hooks
requests.FileModeWarning( requests.__doc__ requests.__repr__( requests.logging
requests.HTTPError( requests.__eq__( requests.__setattr__( requests.models
requests.NullHandler( requests.__file__ requests.__sizeof__( requests.options(
requests.PreparedRequest( requests.__format__( requests.__spec__ requests.packages
requests.ReadTimeout( requests.__ge__( requests.__str__( requests.patch(
requests.Request( requests.__getattribute__( requests.__subclasshook__( requests.post(
requests.RequestException( requests.__gt__( requests.__title__ requests.put(
requests.Response( requests.__hash__( requests.__version__ requests.request(
requests.Session( requests.__init__( requests.adapters requests.session(
requests.Timeout( requests.__le__( requests.api requests.sessions
requests.TooManyRedirects( requests.__license__ requests.auth requests.status_codes
requests.URLRequired( requests.__loader__ requests.certs requests.structures
requests.__author__ requests.__lt__( requests.codes requests.utils
requests.__build__ requests.__name__ requests.compat requests.warnings
requests.__cached__ requests.__ne__( requests.cookies
requests.__class__( requests.__new__( requests.delete(
requests.__copyright__ requests.__package__ requests.exceptions
#2 tarball安装
window下,解压后会产生setup.py cd 进setup.py所在的目录,执行
python3 setup.py install
模块基础
标签:
原文地址:http://www.cnblogs.com/cfj271636063/p/5765457.html