''' 一、什麼是模塊? 在計算機程序的開發過程當中,隨着程序代碼越寫越多,在一個文件裏代碼就會愈來愈長,愈來愈不容易維護。 爲了編寫可維護的代碼,咱們把不少函數分組,分別放到不一樣的文件裏,這樣,每一個文件包含的代碼就相對較少,不少編程語言都採用這種組織代碼的方式。 在Python中,一個.py文件就稱之爲一個模塊(Module)。 二、使用模塊的好處: 最大的好處是大大提升了代碼的可維護性。 編寫代碼沒必要從零開始,當一個模塊編寫完畢,就能夠被其餘地方引用。 使用模塊還能夠避免函數名和變量名衝突。 三、模塊的分類: 內置標準模塊(又稱標準庫)執行help('modules')查看全部python自帶模塊列表 第三方開源模塊,可經過pip install 模塊名 聯網安裝 自定義模塊 四、模塊的調用(模塊一旦被調用,即至關於執行了另一個py文件裏的代碼): import module 導入模塊 from module import xx,xx 從 module 模塊中再導入 xx from module.xx.xx import xx as rename as 起別名 from module.xx.xx import * * 表明全部,不建議使用,變量名容易衝突 ''' import os import sys from os import rmdir from os import rmdir,rename #使用逗號分隔,導入多個 print(help('modules')) #查看全部python自帶模塊列表
help('modules') Please wait a moment while I gather a list of all available modules... __future__ _warnings http secrets _abc _weakref idlelib select _ast _weakrefset imaplib selectors _asyncio _winapi imghdr setup _bisect abc imp setup_cython _blake2 aifc importlib setuptools _bootlocale antigravity inspect shelve _bz2 argparse interpreterInfo shlex _codecs array io shutil _codecs_cn ast ipaddress signal _codecs_hk asynchat itertools site _codecs_iso2022 asyncio json sitecustomize _codecs_jp asyncore keyword smtpd _codecs_kr atexit lib2to3 smtplib _codecs_tw audioop linecache sndhdr _collections backend_interagg locale socket _collections_abc base64 logging socketserver _compat_pickle bdb lzma sqlite3 _compression binascii macpath sre_compile _contextvars binhex mailbox sre_constants _csv bisect mailcap sre_parse _ctypes builtins marshal ssl _ctypes_test bz2 math stat _datetime cProfile mimetypes statistics _decimal calendar mmap string _distutils_findvs cgi modulefinder stringprep _dummy_thread cgitb msilib struct _elementtree chardet msvcrt subprocess _functools chunk multiprocessing sunau _hashlib cmath netrc symbol _heapq cmd nntplib symtable _imp code nt sys _io codecs ntpath sysconfig _json codeop nturl2path tabnanny _locale collections numbers tarfile _lsprof colorsys opcode telnetlib _lzma compileall operator tempfile _markupbase concurrent optparse test _md5 configparser os test_pydevd_reload _msi contextlib parser tests_pydevd _multibytecodec contextvars pathlib tests_pydevd_mainloop _multiprocessing copy pdb tests_pydevd_python _opcode copyreg pickle textwrap _operator crypt pickletools this _osx_support csv pip threading _overlapped ctypes pipes time _pickle curses pkg_resources timeit _py_abc dataclasses pkgutil tkinter _pydecimal datetime platform token _pydev_bundle dbm plistlib tokenize _pydev_imps decimal poplib trace _pydev_runfiles difflib posixpath traceback _pydevd_bundle dis pprint tracemalloc _pydevd_frame_eval distutils profile tty _pyio doctest pstats turtle _queue dummy_threading pty turtledemo _random easy_install py_compile types _sha1 email pyclbr typing _sha256 encodings pycompletionserver unicodedata _sha3 ensurepip pydev_app_engine_debug_startup unittest _sha512 enum pydev_coverage urllib _signal errno pydev_ipython uu _sitebuiltins faulthandler pydev_pysrc uuid _socket filecmp pydev_run_in_console venv _sqlite3 fileinput pydevconsole warnings _sre fnmatch pydevd wave _ssl formatter pydevd_concurrency_analyser weakref _stat fractions pydevd_file_utils webbrowser _string ftplib pydevd_plugins winreg _strptime functools pydoc winsound _struct gc pydoc_data wsgiref _symtable genericpath pyexpat xdrlib _testbuffer getopt queue xml _testcapi getpass quopri xmlrpc _testconsole gettext random xxsubtype _testimportmultiple glob re zipapp _testmultiphase gzip reprlib zipfile _thread hashlib rlcompleter zipimport _threading_local heapq runfiles zlib _tkinter hmac runpy _tracemalloc html sched Enter any module name to get more help. Or, type "modules spam" to search for modules whose name or summary contain the string "spam".
#模塊一旦被調用,即至關於執行了另一個py文件裏的代碼 #自定義模塊 #這個最簡單, 建立一個.py文件,就能夠稱之爲模塊,就能夠在另一個程序裏導入 import sys print(sys.path) ''' 輸出: [ 'E:\\Python\\work\\myFirstPro\\第4章', 'E:\\Python', 'C:\\Python37\\python37.zip', 'C:\\Python37\\DLLs', 'C:\\Python37\\lib', 'C:\\Python37', 'C:\\Python37\\lib\\site-packages', 'C:\\JetBrains\\PyCharm 2018.2.1\\helpers\\pycharm_matplotlib_backend' ] sys.path.append('E:\\Python\\work\\myFirstPro\\第4章') print(sys.path) #import my_modules #del my_modules 小結: python解釋器會按照列表順序去依次到每一個目錄下去匹配你要導入的模塊名, 只要在一個目錄下匹配到了該模塊名,就馬上導入,再也不繼續日後找。 注意:列表第一個元素爲空,即表明當前目錄,因此你本身定義的模塊在當前目錄會被優先導入。 '''
https://pypi.python.org/pypi 是python的開源模塊庫,截止2017年9.30日 ,已經收錄了118170個來自全世界python開發者貢獻的模塊,幾乎涵蓋了你想用python作的任何事情。 事實上每一個python開發者,只要註冊一個帳號就能夠往這個平臺上傳你本身的模塊,這樣全世界的開發者均可以容易的下載並使用你的模塊。html
直接在上面這個頁面上點download,下載後,解壓並進入目錄,執行如下命令完成安裝python
編譯源碼 python setup.py build 安裝源碼 python setup.py install
直接經過pip安裝 git
pip3 install paramiko #paramiko 是模塊名 安裝
pip3 uninstal paramiko 卸載
import paramiko 使用
pip命令會自動下載模塊包並完成安裝。web
軟件通常會被自動安裝你python安裝目錄的這個子目錄裏sql
/your_python_install_path/3.6/lib/python3.6/site-packages
sudo pip install -i http://pypi.douban.com/simple/ alex_sayhi --trusted-host pypi.douban.com #alex_sayhi是模塊名
Microsoft Windows [版本 10.0.17134.285] (c) 2018 Microsoft Corporation。保留全部權利。 E:\Python>pip3 install PyTyrion Collecting PyTyrion Using cached https://files.pythonhosted.org/packages/c4/22/167ec8e203b0f930582a82a1bfcf7faf9d14af3 Using cached https://files.pythonhosted.org/packages/c4/22/167ec8e203b0f930582a82a1bfc Using cached https://files.pythonhosted.org/packages/c4/22/167ec8e203b0f930582a82a1b Using cached https://files.pythonhosted.org/packages/c4/22/167ec8e203b0f930582a82a Using cached https://files.pythonhosted.org/packages/c4/22/167ec8e203b0f93058 2a Using cached https://files.pythonhosted.org/packages/c4/22/167ec 8b0f930582a82a1bfcf7faf9d14af3a0d6abc807dbb22ed52f8/PyTyrion-1.0.1.tar e203b0f930582a82a1bfcf7faf9d14af3a0d6abc807dbb22ed52f8/PyTyrion-1. Using cached https://files.pythonhosted.org/packages/c4/22/167 ec 8e203b0f930582a82a1bfcf7faf9d14af3a0d6abc807dbb22ed52f8/PyTyrion -1 Using cached https://files.pythonhosted.org/packages/c4/22/1 67 ec8e203b0f930582a82a1bfcf7faf9d14af3a0d6abc807dbb22ed52f8/PyTy ri Using cached https://files.pythonhosted.org/packages/c4/22/167ec8e20 3b0f930582a82a1bfcf7faf9d14af3a0d6abc807dbb22ed52f8/PyTyrion-1.0.1.t ar.gz Installing collected packages: PyTyrion Running setup.py install for PyTyrion ... done Successfully installed PyTyrion-1.0.1 You are using pip version 10.0.1, however version 18.0 is availabl eou should consider upgrading via the 'python -m pip install --upgrad You are using pip version 10.0.1, however version 18.0 is avai l. You are using pip version 10.0.1, however version 18.0 is a va You are using pip version 10.0.1, however version 18.0 is a You are using pip version 10.0.1, however version 18.0 Running setup.py install for PyTyrion ... done Successfully installed PyTyrion-1.0.1 You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip inst all --upgrade pip' command. E:\Python>pip3 uninstall PyTyrion Uninstalling PyTyrion-1.0.1: Would remove: c:\python37\lib\site-packages\pytyrion-1.0.1-py3.7.eg g-info c:\python37\lib\site-packages\tyrion\* Proceed (y/n)? y Successfully uninstalled PyTyrion-1.0.1 You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip inst all --upgrade pip' command. E:\Python>pip3 install PyTyrion Collecting PyTyrion Using cached https://files.pythonhosted.org/packages/c4/22/167ec8e203b0f930582a82a1bfcf7faf 9d14af3a0d6abc807dbb22ed52f8/PyTyrion-1.0.1.tar.gz Installing collected packages: PyTyrion Running setup.py install for PyTyrion ... done Successfully installed PyTyrion-1.0.1 You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. (venv) E:\Python>pip3 install -i http://pypi.douban.com/simp le/alex_sayhi --trusted-host pypi.douban.com You must give at least one requirement to install (see "pip help install") You are using pip version 9.0.3, however version 10.0.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. (venv) E:\Python>pip3 install -i http://pypi.douban.com/simp le/ alex_sayhi --trusted-host pypi.douban.com Collecting alex_sayhi Downloading http://pypi.doubanio.com/packages/84/14/b59d93276c86f6ab556cfa7c2d860b742c1611b601cc 4c7743d129b4b52a/alex_sayhi-1.0.0.tar.gz Installing collected packages: alex-sayhi Running setup.py install for alex-sayhi ... done Successfully installed alex-sayhi-1.0.0 You are using pip version 9.0.3, however version 10.0.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. E:\Python>
E:\Python>pip3 install paramiko Collecting paramiko Downloading https://files.pythonhosted.org/packages/3e/db/cb7b6656e0e7387637ce850689084dc0b9 4b44df31cc52e5fc5c2c4fd2c1/paramiko-2.4.1-py2.py3-none-any.whl (194kB) 78% |█████████████████████████▎ | 153kB 255kB/s eta 0:00:01 84% |███████████████████████████ | 163kB 255kB/s eta 0:00:0 89% |████████████████████████████▋ | 174kB 276kB/s eta 0:00 94% |██████████████████████████████▎ | 184kB 275kB/s eta 0: 100% |████████████████████████████████| 194kB 383kB/s Collecting pyasn1>=0.1.7 (from paramiko) Downloading https://files.pythonhosted.org/packages/d1/a1/7790cc85db38daa874f6a2e6308131b995 3feb1367f2ae2d1123bb93a9f5/pyasn1-0.4.4-py2.py3-none-any.whl (72kB) 84% |███████████████████████████ | 61kB 393kB/s eta 0:00:01 98% |███████████████████████████████▋| 71kB 417kB/s eta 0: 100% |████████████████████████████████| 81kB 436kB/s Collecting pynacl>=1.0.1 (from paramiko) Downloading https://files.pythonhosted.org/packages/6c/93/29931e02f3add7952f2ea1976960c8a591 a92dcc57a76cd3d736e1c554d0/PyNaCl-1.2.1-cp37-cp37m-win_amd64.whl (165kB) 80% |█████████████████████████▊ | 133kB 278kB/s eta 0:00:01 86% |███████████████████████████▋ | 143kB 936kB/s eta 0:00: 92% |█████████████████████████████▋ | 153kB 655kB/s eta 0:0 98% |███████████████████████████████▋| 163kB 595kB/s eta 0 100% |████████████████████████████████| 174kB 936kB/s Collecting bcrypt>=3.1.3 (from paramiko) Downloading https://files.pythonhosted.org/packages/5c/35/5cedb29e9d18108fa54fd383f76e086ad8 ebab9d1d476e8411445c61cccd/bcrypt-3.1.4-cp37-cp37m-win_amd64.whl Collecting cryptography>=1.5 (from paramiko) Downloading https://files.pythonhosted.org/packages/39/dd/43985388f82ac0b4698671e96235c6324b df14339e21eb3647f4e5b99017/cryptography-2.3.1-cp37-cp37m-win_amd64.whl (1.3MB) 79% |█████████████████████████▍ | 1.0MB 122kB/s eta 0:00:03 79% |█████████████████████████▋ | 1.0MB 141kB/s eta 0:00:02 80% |█████████████████████████▉ | 1.1MB 157kB/s eta 0:00:02 81% |██████████████████████████ | 1.1MB 133kB/s eta 0:00:02 82% |██████████████████████████▍ | 1.1MB 138kB/s eta 0:00:0 83% |██████████████████████████▋ | 1.1MB 101kB/s eta 0:00:0 83% |██████████████████████████▉ | 1.1MB 101kB/s eta 0:00:0 84% |███████████████████████████ | 1.1MB 147kB/s eta 0:00:0 85% |███████████████████████████▍ | 1.1MB 115kB/s eta 0:00: 86% |███████████████████████████▋ | 1.1MB 114kB/s eta 0:00: 87% |███████████████████████████▉ | 1.1MB 83kB/s eta 0:00:0 87% |████████████████████████████ | 1.1MB 77kB/s eta 0:00:0 88% |████████████████████████████▍ | 1.2MB 82kB/s eta 0:00: 89% |████████████████████████████▋ | 1.2MB 87kB/s eta 0:00: 90% |████████████████████████████▉ | 1.2MB 78kB/s eta 0:00: 90% |█████████████████████████████ | 1.2MB 47kB/s eta 0:00: 91% |█████████████████████████████▍ | 1.2MB 37kB/s eta 0:00 92% |█████████████████████████████▋ | 1.2MB 37kB/s eta 0:00 93% |█████████████████████████████▉ | 1.2MB 31kB/s eta 0:00 94% |██████████████████████████████ | 1.2MB 27kB/s eta 0:00 94% |██████████████████████████████▍ | 1.2MB 30kB/s eta 0:0 95% |██████████████████████████████▋ | 1.2MB 27kB/s eta 0:0 96% |██████████████████████████████▉ | 1.3MB 27kB/s eta 0:0 97% |███████████████████████████████ | 1.3MB 25kB/s eta 0:0 97% |███████████████████████████████▍| 1.3MB 24kB/s eta 0: 98% |███████████████████████████████▋| 1.3MB 35kB/s eta 0: 99% |███████████████████████████████▉| 1.3MB 42kB/s eta 0: 100% |████████████████████████████████| 1.3MB 41kB/s Collecting cffi>=1.4.1 (from pynacl>=1.0.1->paramiko) Downloading https://files.pythonhosted.org/packages/ca/f2/e375b7469a2dfe9d1feac81a10df97f18c d771b9a10ac62ca9864b760f7c/cffi-1.11.5-cp37-cp37m-win_amd64.whl (165kB) 80% |█████████████████████████▊ | 133kB 108kB/s eta 0:00:01 86% |███████████████████████████▋ | 143kB 134kB/s eta 0:00: 92% |█████████████████████████████▋ | 153kB 139kB/s eta 0:0 98% |███████████████████████████████▋| 163kB 136kB/s eta 0 100% |████████████████████████████████| 174kB 161kB/s Collecting six (from pynacl>=1.0.1->paramiko) Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29 a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl Collecting asn1crypto>=0.21.0 (from cryptography>=1.5->paramiko) Downloading https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad 7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB) 90% |█████████████████████████████ | 92kB 212kB/s eta 0:00: 100% |████████████████████████████████| 102kB 220kB/s Collecting idna>=2.1 (from cryptography>=1.5->paramiko) Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a 97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB) 87% |████████████████████████████▏ | 51kB 234kB/s eta 0:00: 100% |████████████████████████████████| 61kB 262kB/s Collecting pycparser (from cffi>=1.4.1->pynacl>=1.0.1->paramiko) Downloading https://files.pythonhosted.org/packages/8c/2d/aad7f16146f4197a11f8e91fb81df177ad cc2073d36a17b1491fd09df6ed/pycparser-2.18.tar.gz (245kB) 79% |█████████████████████████▎ | 194kB 361kB/s eta 0:00:01 83% |██████████████████████████▋ | 204kB 278kB/s eta 0:00:0 87% |████████████████████████████ | 215kB 248kB/s eta 0:00: 91% |█████████████████████████████▎ | 225kB 249kB/s eta 0:0 95% |██████████████████████████████▋ | 235kB 239kB/s eta 0: 99% |████████████████████████████████| 245kB 239kB/s eta 0 100% |████████████████████████████████| 256kB 292kB/s Installing collected packages: pyasn1, pycparser, cffi, six, pynacl, bcrypt, asn1crypto, idna, cryptography, paramiko Running setup.py install for pycparser ... done Successfully installed asn1crypto-0.24.0 bcrypt-3.1.4 cffi-1.11.5 cryptography-2.3.1 idna-2.7 paramiko-2.4.1 pyasn1-0.4.4 pycparser-2.18 pynacl-1.2.1 six-1.11.0 You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. 使用國內豆瓣源下載速度對比: Microsoft Windows [版本 10.0.17134.285] (c) 2018 Microsoft Corporation。保留全部權利。 E:\Python>pip3 install -i http://pypi.douban.com/simple/ paramiko Looking in indexes: http://pypi.douban.com/simple/ Collecting paramiko The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead The repository located at pypi.douban.com is not a trusted or secure host and i s being ignored. If this repository is available via HTTPS we recommend you use HTT PS instead, otherwise you may silence this warning and allow it anyway with '--trus The repository located at pypi.douban.com is not a trusted or secure host an d i s being ignored. If this repository is available via HTTPS we recommend you us e H TTPS instead, otherwise you may silence this warning and allow it anyway with The repository located at pypi.douban.com is not a trusted or sec ure host and is being ignored. If this repository is available via HTT PS we recommend you use HTTPS instead, otherwise you may silence this war The repository located at pypi.douban.com is not a trusted or s ec ure host and is being ignored. If this repository is available vi a HTTPS we recommend you use HTTPS instead, otherwise you may silen ce this warning and allow it anyway with '--trusted-host pypi.douba n. com'. HTTPS we recommend you use HTTPS instead, otherwise you may si len ce this warning and allow it anyway with '--trusted-host pypi. dou ban.com'. HTTPS we recommend you use HTTPS instead, otherwise you may may silence this warning and allow it anyway with '--tr ust pypi.douban.com'. sted-host pypi.douban.com'. Could not find a version that satisfies the requireme may silence this warning and allow it anyway with '--t rusted-host pypi.douban.com'. HTTPS we recommend you use HTTPS instead, otherw is e you may silence this warning and allow it anyw ay with '--trusted-host pypi.douban.com'. HTTPS we recommend you use HTTPS instead, ot herwise you may silence this warning and a ll ow it anyway with '--trusted-host pypi.dou ba n.com'. Could not find a version that satisfies herwise you may silence this warning and a requirement paramiko (from versions: ) llow it anyway with '--trusted-host pypi. d ouban.com'. Could not find a version that satisfies the requirement paramiko (from versions: )ou should consider upgrading via the 'pyt No matching distribution found for parami the requirement paramiko (from versions : ) No matching distribution found for param Could not find a version that satisfies the requirement paramiko (from versions: ) No matching distribution found for paramiko You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgra You are using pip version 10.0.1, however version 18.0 is availab lou should consider upgrading via the 'python -m pip install --upg You are using pip version 10.0.1, however version 18.0 is avail ab You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' comma You are using pip version 10.0.1, however version 18.0 is availa b You are using pip version 10.0.1, however version 18 .0 You are using pip version 10.0.1, however vers i 18.0 is available. You are using pip version 10.0.1, however ve ron 18.0 is available. You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. E:\Python>pip3 install -i http://pypi.douban.com/simple/ paramiko --trusted-host pypi.douban.com Looking in indexes: http://pypi.douban.com/simple/ Collecting paramiko Downloading http://pypi.doubanio.com/packages/3e/db/cb7b6656e0e7387637ce850689084dc0b94b44df31cc52e5fc5c2c4fd2c1/paramiko- 2.4.1-py2.py3-none-any.whl (194kB) 100% |████████████████████████████████| 194kB 655kB/s Requirement already satisfied: pynacl>=1.0.1 in c:\python37\lib\site-packages (from paramiko) (1.2.1) Requirement already satisfied: pyasn1>=0.1.7 in c:\python37\lib\site-packages (from paramiko) (0.4.4) Requirement already satisfied: cryptography>=1.5 in c:\python37\lib\site-packages (from paramiko) (2.3.1) Requirement already satisfied: bcrypt>=3.1.3 in c:\python37\lib\site-packages (from paramiko) (3.1.4) Requirement already satisfied: cffi>=1.4.1 in c:\python37\lib\site-packages (from pynacl>=1.0.1->paramiko) (1.11.5) Requirement already satisfied: six in c:\python37\lib\site-packages (from pynacl>=1.0.1->paramiko) (1.11.0) Requirement already satisfied: idna>=2.1 in c:\python37\lib\site-packages (from cryptography>=1.5->paramiko) (2.7) Requirement already satisfied: asn1crypto>=0.21.0 in c:\python37\lib\site-packages (from cryptography>=1.5->paramiko) (Requirement already satisfied: idna>=2.1 in c:\python37\lib\site-packages (from cryptography>=1.5->pa ramiko) (2.7) Requirement already satisfied: idna>=2.1 in c:\python37\lib\site -p ackages (from cryptography>=1.5->paramiko) (2.7) Requirement already satisfied: asn1crypto>=0.21.0 in c:\python37 \l ib\site-packages (from cryptography>=1.5->paramiko) (0.24.0) Requirement already satisfied: pycparser in c:\python37\lib\site Requirement already satisfied: asn1crypto>=0.21.0 in c:\ plib\site-packages (from cryptography>=1.5->paramiko) (0.24.0) ython37\lib\site-packages (from cryptography>=1.5->param ipackages (from cffi>=1.4.1->pynacl>=1.0.1->paramiko) (2.18) ko) (0.24.0) Requirement already satisfied: pycparser in c:\python37\ lou are using pip version 10.0.1, however version 18.0 is availa ib\site-packages (from cffi>=1.4.1->pynacl>=1.0.1->param iou should consider upgrading via the 'python -m pip install --u Requirement already satisfied: pycparser in c:\python37\lib\site-packages (from cffi>=1.4.1->pynacl>=1.0.1->paramik o) (2.18) Installing collected packages: paramiko Successfully installed paramiko-2.4.1 You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.