In Python, the installation of a third-party module is done through the Setuptools tool.
If you are using Mac or Linux, install Setuptools itself this step can be skipped.
If you are using Windows, please download ez_setup.pyfrom this address first:
Https://pypi.python.org/pypi/setuptools#windows
After downloading, simply place it in a directory and run the following command to install Setuptools:
python ez_setup.py
When you try to run under a command Prompt window easy_install
, Windows prompts you not to find the command because the easy_install.exe
path has not been added to the environment variable Path
. Please add C:\Python27\Scripts
to environment variable Path
:
Reopen the command Prompt window and you are ready to run easy_install
:
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:
easy_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, 300) RGB>>> im.thumbnail((200, 100))>>> im.save(‘thumb.jpg‘, ‘JPEG‘)
Other commonly used third-party libraries are MySQL drivers: MySQL-python
numpy Libraries for Scientific Computing: numpy
template tools for generating text, and Jinja2
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 "<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 stored in the sys
variables of the module path
:
>>> 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 directly sys.path
, add the directory to search:
>>> 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.
Python installs third-party modules