Python遇到的一些问题


1. 使用 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\ 目录下。


2. 模块搜索路径(Window)

当加载一个模块时,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种方法

  1. 是直接修改sys.path,添加要搜索的目录:

    >>> import sys
    >>> sys.path.append(‘自己模块的目录’)

    注:这种方法是在运行时修改,运行结束后失效。

  2. 设置环境变量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’, …]

3. 导module失败(TypeError: ‘module’ object is not callable)

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

4. Mac下,Pip安装IPython失败

安装IPython时,依赖于six的库,失败原因参考这篇文章

解决方案:

// Mac系统下,先升级siz包
$ sudo pip install six --upgrade --ignore-installed six
$ sudo pip install ipython

5. 进入到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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值