Tutorial on using third-party modules in Python, python third-party

Source: Internet
Author: User

Tutorial on using third-party modules in Python, python third-party

In Python, third-party modules are installed through the setuptools tool. Python has two package management tools that encapsulate setuptools: easy_install and pip. Pip is currently officially recommended.

If you are using Mac or Linux, skip this step to install pip.

If you are using Windows, see install Python to ensure that pip and Add python.exe to Path are checked during installation.

In the Command Prompt window, try to run pip. If Windows prompts that no command is found, you can run the installer again to add pip.

Now, let's install a third-party Library, Python Imaging Library, which is a very powerful tool Library for image processing in Python. Generally, third-party libraries are registered on the Python official pypi.python.org website. to install a third-party library, you must first know the name of the library and search for it on the official website or pypi, for example, the Python Imaging Library name is PIL. Therefore, the command to install Python Imaging Library is:

pip install PIL

Wait patiently for the download and installation before you can use PIL.

With PIL, image processing is easy. Generate a thumbnail for any image:

>>> import Image>>> im = Image.open('test.png')>>> print im.format, im.size, im.modePNG (400, 300) RGB>>> im.thumbnail((200, 100))>>> im.save('thumb.jpg', 'JPEG')

Other commonly used third-party libraries include MySQL DRIVER: MySQL-python, NumPy library for scientific computing: numpy, and Jinja2 template tool for generating text.
Module search path

When we try to load a module, Python will search for the corresponding. py file in the specified path. If it cannot be found, an error will be returned:

>>> import mymoduleTraceback (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. 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 you want to add your own search directory, there are two methods:

First, directly modify sys. path and add the directory to be searched:

>>> import sys>>> sys.path.append('/Users/michael/my_py_scripts')

This method is modified at runtime and becomes invalid after running.

The second method is to set the environment variable PYTHONPATH. The content of this environment variable is automatically added to the module search path. The setting method is similar to setting the Path environment variable. Note that you only need to add your own search path. The search path of Python itself is not affected.

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.