Understanding of Modules
module, which implements a set of code for a function with a mound code.
Like functional programming and process-oriented programming, functional programming accomplishes a function, and other code is used to invoke it, providing reusability of code and coupling between the code. For a complex function, multiple functions may be required to complete (functions can be in different. py files), and the Code collection consisting of N. py files is called a module.
such as: OS is a system-related module; file is a module related to the operation of files
The modules are divided into three types:
- Custom Modules
- Built-in Modules
- Third-party open source modules
Custom Modules1. Define the module
2. Import Module
Python is used more and more widely, and to a certain extent relies on it providing programmers with a large number of modules to use, and if you want to use modules, you need to import them. There are several ways to import a module:
1 Import module2 from module.xx.xx import xx3 from module.xx.xx import xx as rename 4 from module.xx.xx import *
The import module actually tells the Python interpreter to explain the py file.
- Import a py file, the interpreter interprets the py file
- Import a package, the interpreter interprets the __init__.py file under the package
So the question is, is the module being imported based on that path as a benchmark? namely: Sys.path
1 Import sys2 Print Sys.path
Results:
1 ['/home/zh/pycharmprojects/s12/day1 ', '/usr/local/lib/python2.7/dist-packages/setuptools-18.1-py2.7.egg ', '/usr ' /local/lib/python2.7/dist-packages/pip-7.1.0-py2.7.egg ', '/usr/local/lib/python2.7/dist-packages/ Django-1.8.16-py2.7.egg ', '/home/zh/pycharmprojects/s12 ', '/usr/lib/python2.7 ', '/usr/lib/python2.7/plat-x86_64- Linux-gnu ', '/usr/lib/python2.7/lib-tk ', '/usr/lib/python2.7/lib-old ', '/usr/lib/python2.7/lib-dynload ', '/usr/ Local/lib/python2.7/dist-packages ', '/usr/lib/python2.7/dist-packages ', '/usr/lib/python2.7/dist-packages/ Pilcompat ', '/usr/lib/python2.7/dist-packages/gtk-2.0 ', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client ']
If we want to add our own search directory, there are two ways:
One is to modify directly sys.path
, add the directory to search:
>>> Import sys>>> sys.path.append ('/users/zh/my_py_scripts ')
This method is modified at run time and is invalidated after the run is finished.
The second method is to set the environment variable PYTHONPATH
, and the contents of the environment variable are automatically added to the module search path. Settings are similar to setting the PATH environment variable. Note that you only need to add your own search path, and Python's own search path will not be affected.
Various directories can be obtained through the OS module, for example:
1 Import sys2 import os3 4 Pre_path = Os.path.abspath (' ... /') 5 sys.path.append (Pre_path)
third-party open source modules1. Download and install
There are two ways to download the installation:
Method One: Use the source code to install
On the github.com website you can download the source code of the third-party library (or other means), after getting the source code, install it locally.
In general, the resulting code format is probably the zip, tar.zip, tar.bz2 format of the compressed package. Unpack these packages and go to their folders and usually see a setup.py file. If it's Linux or MAC (I'm using Ubuntu, especially recommended), run the shell here and execute the command:
Python setup.py Install
If you are using Windows, you need to open the command line mode and execute the instructions above.
In this way, the third library can be installed into the system. The exact location depends on the operating system and the path you set when you installed the Python environment. By default, Windows is in C:\Python2.7\Lib\site-packages
, Linux in /usr/local/lib/python2.7/dist-packages
(this just reference, different distributions will have a difference, specifically please readers according to their own operating system, look for themselves), Mac in /Library/Python/2.7/site-packages
.
There will be uninstall, uninstall the installed library is very simple, just to the corresponding system Site-packages directory, directly delete the library file is unloaded.
Compiled source python setup.py build5 installation source python setup.py install
Note: When using source installation, you need to use the GCC compilation and Python development environment, so you need to do it first:
1 Yum install gcc2 yum install Python-devel3 or 4 apt-get Python-dev
After the installation is successful, the module is automatically installed in a directory in Sys.path, such as:
1/usr/lib/python2.7/site-packages/
Method Two: Pip
Installed with the source code, not my recommendation, I recommend the use of third-party library management tools to install. There is a website that is dedicated to storing third-party libraries, all on this site, can be installed with PIP or easy_install this installation tool. Address of this website: https://pypi.Python.org/pypi
First of all, to install Pip (Python is officially recommended this, of course I have to follow the trend, so, just introduce and use this tool only). If the reader is the same as me, using Ubuntu or some other Linux, basically do not use this operation, the installation of the operating system has been installed by default this thing (this is not used for Ubuntu reason?). )。 If for any reason, there is no installation, you can use the following methods:
Debian and ubuntu:sudo apt-get install python-pip
Fedora and centos:sudo yum install Python-pip
Of course, you can also download the file get-pip.py here and then execute it Python get-pip.py
to install it. This method also applies to Windows.
Pip is ready to install. If you are installing a third-party library, you only need to execute pip install XXXXXX
(XXXXXX represents the name of a third-party library).
When the third-party library is installed, the next use is the same as in the previous standard library.
2. Import Module
How to import with custom modules
3. Example: Installing a third-party module Paramiko
Paramiko is a module for remote control, which can be used to command or file the remote server, it is worth saying that the fabric and ansible internal remote management is the use of Paramiko to reality.
1 #Pycrypto, since the Paramiko module is internally dependent on Pycrypto, download the installation first Pycrypto2 3 #Download and install Pycrypto4wget http://files.cnblogs.com/files/wupeiqi/pycrypto-2.6.1. tar.gz5TAR-XVF pycrypto-2.6.1. tar.gz6CD pycrypto-2.6.17 python setup.py build8 python setup.py Install9 Ten #Enter the python environment, import crypto to check if the installation was successful One A #Download and install Paramiko -wget http://files.cnblogs.com/files/wupeiqi/paramiko-1.10.1. tar.gz -TAR-XVF paramiko-1.10.1. tar.gz theCD paramiko-1.10.1 - python setup.py build - python setup.py Install - + #Enter the python environment, import Paramiko to check if the installation was successful
Python Basics 8--Modules-Custom modules and third-party open source modules