pip install PIL
安装PIL失败,提示如下信息Could not find a version that satisfies the requirement PIL (from versions: )
No matching distribution found for PIL
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the ‘python -m pip install –upgrade pip’ comm
and.
原因: PIL已经停止更新,现在使用 pillow。
解决方法: 安装pillow pip install pillow
,如果网速不行,就使用豆瓣的镜像源,
安装命令:pip --default-timeout=100 install xx -i http://pypi.douban.com/simple
相关资源:Pillow文档
Lib库所在的下载目录: D:\...\Python27\Lib\site-packages\
目录下。
当加载一个模块时,Python会在指定的路径下搜索对应的.py文件,如果找不到,就会报错:
>>> import module_name
Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named mymodule
默认情况下,Python解释器会搜索当前目录、所有已安装的内置模块和第三方模块,搜索路径存放在sys模块的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’, …]
问题: 如何添加自己的搜索目录?
解决方法: 2种方法
是直接修改sys.path,添加要搜索的目录:
>>> import sys
>>> sys.path.append(‘自己模块的目录’)
注:这种方法是在运行时修改,运行结束后失效。
设置环境变量PYTHONPATH,该环境变量的内容会被自动添加到模块搜索路径中。设置方式与设置Path环境变量类似。注意只需要添加你自己的搜索路径,Python自己本身的搜索路径不受影响。
>>> import sys
>>> sys.path
[”, ‘E:\workspace_py‘,
‘/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’, …]
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
def print_score(self):
print '%s %s' % (self.name, self.score)
问题: 在IDLE中导入Student时报错:
>>> import Student
>>> s = Student(“张三”, 22)
Traceback (most recent call last):
File “< pyshell#5 >”, line 1, in
s = Student(“aa”, 22)
“TypeError”: ‘module’ object is not callable
问题分析: Python导入模块的方法有两种
1. import module
2. from module import
区别: 前者所有导入的东西使用时需加上模块名的限定,而后者不要。
解决方案: 正确的导入姿势:
>>> import Student
>>> ss = Student.Student(“张三”, 22)
>>> print ss.name
或
>>> from Student import *
>>> ss = Student(“张三”, 22)
>>> print ss.name
安装IPython时,依赖于six的库,失败原因参考这篇文章
解决方案:
// Mac系统下,先升级siz包
$ sudo pip install six --upgrade --ignore-installed six
$ sudo pip install ipython
# 直接输入IPython,提示没有IPython命令
$ IPython
-bash: ipyhon: command not found
解决方案:参考这篇文章
# 查看IPython版本
$ python -m IPython --version
5.4.1
# 进入IPython终端
$ python -m IPython
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
Type "copyright", "credits" or "license" for more information.
IPython 5.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
# 如何运行.py文件
$ cd 进入到文件所在目录
$ %run 文件名.py