Tutorial on using Third-party modules in Python _python

Source: Internet
Author: User
Tags in python

In Python, the installation of a Third-party module is done through the Setuptools tool. Python has two package management tools encapsulated in Setuptools: Easy_install and Pip. PIP is currently recommended for use by the official.

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

If you are using Windows, refer to the installation of the Python section to ensure that the PIP and add Python.exe to Path are selected during installation.

Try running the PIP under the Command Prompt window, and if Windows prompts not find the command, you can rerun Setup to add the PIP.

Now, let's install a third-party library of--python Imaging libraries, 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 Web site, to install a third-party library, you must first know the name of the library, you can search the official website or pypi, such as the Python Imaging library name PiL, so The command to install the Python Imaging Library is:

Pip Install PiL

After waiting for download and installation, you can use PIL.

With PIL, it's easy to deal with pictures. Grab a picture and create a thumbnail:

>>> import Image
>>> im = Image.open (' test.png ')
>>> print Im.format, Im.size, Im.mode
PNG (MB) RGB
>>> Im.thumbnail ((m))
>>> im.save (' thumb.jpg ', ' JPEG ') )

Other commonly used Third-party libraries are also MySQL-driven: Mysql-python, a numpy library for scientific Computing: NumPy, template tools 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 is not found, an error occurs:

>>> Import MyModule
traceback (most recent call last):
 File ' <stdin> ', line 1, in <module>
   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 placed 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 Sys.path directly and add directories to search for:

>>> Import sys
>>> sys.path.append ('/users/michael/my_py_scripts ')

This method is modified at run time and is invalidated at the end of the run.

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. Set in the same way that you set the PATH environment variable. Note that you only need to add your own search path, and Python's own search path is unaffected.

Related Article

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.