Tutorials for using third-party modules in Python

Source: Internet
Author: User
In Python, the installation of a third-party module is done through the Setuptools tool. Python has two package management tools that encapsulate Setuptools: Easy_install and Pip. Currently, it is recommended to use PIP.

If you are using Mac or Linux, install the PIP itself this step can be skipped.

If you are using Windows, please refer to the section on installing Python to make sure Pip and add Python.exe to Path are checked during installation.

Under the Command Prompt window, try to run Pip, and if the Windows prompt does not find a command, you can rerun Setup to add Pip.

Now, let's install a third-party library,--python Imaging Library, which is a very powerful tool library for processing images under Python. In general, third-party libraries will be registered in the official Python pypi.python.org website, to install a third-party library, you must first know the name of the library, you can search on the official website or pypi, such as the name of the Python Imaging library is called PiL, so, The command to install the Python Imaging Library is:

Pip Install PIL

After you have waited patiently for the download and installation, you can use PIL.

With PIL, it's easy to handle pictures. Just look for a picture to generate a thumbnail image:

>>> import Image>>> im = Image.open (' test.png ') >>> print Im.format, Im.size, Im.modepng (400, Rgb>>> Im.thumbnail ((max.)) >>> im.save (' thumb.jpg ', ' JPEG ')

Other commonly used third-party libraries are MySQL drivers: Mysql-python, the NumPy Library for Scientific Computing: NumPy, the template tool for generating text Jinja2, and so on.
Module Search Path

When we try to load a module, Python searches for the corresponding. py file under the specified path, and if it does not, it will get an error:

>>> Import Mymoduletraceback (most recent call last): File "
 
  
   
  ", line 1, in 
  
   
    
   importerror : No module named MyModule
  
   
 
  

By default, the Python interpreter searches the current directory, all installed built-in modules, and third-party modules, and the search path is stored in the PATH variable of the SYS module:

>>> import sys>>> sys.path[', '/library/python/2.7/site-packages/ Pycrypto-2.6.1-py2.7-macosx-10.9-intel.egg ', '/library/python/2.7/site-packages/ Pil-1.1.7-py2.7-macosx-10.9-intel.egg ', ...]

If we want to add our own search directory, there are two ways:

One is to modify the Sys.path directly and add the directories to be searched:

>>> Import sys>>> sys.path.append ('/users/michael/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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.