安装第三方模块
在 Python 中,安装第三方模块,是通过 setuptools 这个工具完成的。Python 有两个封装了setuptools 的包管理工具:easy_install 和 pip。目前官方推荐 pip。
如果你正在使用 Mac 或 Linux,安装 pip 本身这个步骤就可以跳过了。
如果你正在使用 Windows,请参考安装 Python 一节的内容,确保安装时勾选了 pip 和 Add python.exe to Path。
在命令提示符窗口下尝试运行 pip,如果 Windows 提示未找到命令,可以重新运行安装程序添加 pip。
现在,让我们来安装一个第三方库 —- Python Imaging Library,这是 Python 下非常强大的处理图像的工具库。一般来说,第三方库都会在 Python 官方的 pypi.python.org 的网站注册,需要安装一个第三方库,必须先知道该库的名称,可以在官网或者 pypi 上搜索,比如 Python Imaging Library 的名称叫 PIL,因此,安装 Python Imaging Library 的命令就是:
pip install PIL
使用当前的版本会安装失败,建议修改为32位版本或者下载其他版本,参考:这里
这里使用的是2.7.15-64位:python-2.7.15 命令也有所区别
C:\Users\Administrator>pip install pillow
Collecting pillow
Downloading https://files.pythonhosted.org/packages/b5/ff/8bd40241d1345331cdc4
5f4412386e7464d031f90420c2aebd3ab7f7a1c9/Pillow-5.4.1-cp27-cp27m-win_amd64.whl (
1.8MB)
38% |████████████▎ | 696kB 66kB/s eta 0:00:17
38% |████████████▌ | 706kB 55kB/s eta 0:00:20
39% |████████████▋ | 716kB 72kB/s eta 0:00:16
...
99% |███████████████████████████████▉| 1.8MB
99% |████████████████████████████████| 1.8MB
100% |████████████████████████████████| 1.8M
B 56kB/s
Installing collected packages: pillow
Successfully installed pillow-5.4.1
You are using pip version 9.0.3, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' comm
and.
安装完成后,使用 PIL 对图片生成缩略图:
C:\Users\Administrator>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL.Image as Image # 原资料为 import Image
>>> im = Image.open('test.png')
>>> print im.format, im.size, im.mode
PNG (500, 409) RGBA
>>> im.thumbnail((200, 100))
>>> im.save('thumb.jpg', 'JPEG')
Traceback (most recent call last):
File "", line 1, in
File "D:\Python27\lib\site-packages\PIL\Image.py", line 1994, in save
save_handler(self, fp, filename)
File "D:\Python27\lib\site-packages\PIL\JpegImagePlugin.py", line 622, in _save
raise IOError("cannot write mode %s as JPEG" % im.mode)
IOError: cannot write mode RGBA as JPEG
参考:https://github.com/python-pillow/Pillow/issues/2609
D:\>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL.Image as Image
>>> im = Image.open('test.png')
>>> print im.format, im.size, im.mode
PNG (500, 409) RGBA
>>> im.thumbnail((200, 100))
>>> im = im.convert("RGB")
>>> im.save("test.jpg")
>>>
以下第三方库未测试:
其他常用的第三方库还有 MySQL的驱动:MySQL-python,用于科学计算的 NumPy库:numpy,用于生成文本的模板工具 Jinja2,等等。
模块搜索路径
当我们试图加载一个模块时,Python 会在指定的路径下搜索对应的 .py 文件,如果找不到,就会报错:
>>> import mymode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mymode
>>>
默认情况 下,Python 解释器会搜索当前目录、所有已安装的内置模块和第三方模块,搜索路径存放在 sys 模块的 path 变量中:
>>> import sys
>>> sys.path
['', 'D:\\Python27\\lib\\site-packages\\distribute-0.6.14-py2.7.egg', 'C:\\Windo
ws\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Pyt
hon27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27', 'D:\\Python2
7\\lib\\site-packages']
>>>
如果我们要添加自己的搜索目录,有两种方法:
一是直接修改 sys.path,添加要搜索的目录:
>>> import sys
>>> sys.path.append('/Users/michael/my_py_scripts')
这种方法是在运行时修改,运行结束后失效。
第二种方法是设置环境变量 PYTHONPATH,该环境变量的内容会被自动添加到模块搜索路径中。设置方式与设置 Path 环境变量类似。注意只需要添加你自己的搜索路径,Python 自己本身的搜索路径不受影响。