Python 尝试导入 pylab 时出现“导入错误:没有名为 pytz 的模块”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26359028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'ImportError: No module named pytz' when trying to import pylab?
提问by nale
As far as I can tell, I don't even need pytz for what I'm doing.
据我所知,我什至不需要 pytz 来完成我正在做的事情。
I'm re-learning python for image processing using the O'Reilly book 'Programming Computer Vision with Python' for work (and I'm also new to mac, so on both counts I apologise if this is a stupid question). Along with a 'empire.jpg' picture, I'm trying to run the script on page 16 which goes as follows:
我正在使用 O'Reilly 的书“使用 Python 编程计算机视觉”重新学习用于图像处理的 Python(我也是 mac 的新手,所以在这两个方面,如果这是一个愚蠢的问题,我深表歉意)。连同“empire.jpg”图片,我正在尝试运行第 16 页上的脚本,如下所示:
from PIL import Image
from pylab import *
# read image to array
im = array(Image.open('empire.jpg')) # plot the image
imshow(im)
# some points
x = [100,100,400,400]
y = [200,500,200,500]
# plot the points with red star-markers
plot(x,y,'r*')
# line plot connecting the first two points
plot(x[:2],y[:2])
# add title and show the plot
title('Plotting: "empire.jpg"')
show()
but I get the following output with an error:
但我得到以下输出错误:
File "plotch1.py", line 2, in <module>
from pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/pylab.py", line 208, in <module>
from matplotlib import mpl # pulls in most modules
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/mpl.py", line 4, in <module>
from matplotlib import axes
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py", line 18, in <module>
from matplotlib import dates as mdates
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/dates.py", line 82, in <module>
import pytz
ImportError: No module named pytz
I'm using OS X 10.9.4. I've installed matplotlib and numpy from macpython.org and I've installed scipy 0.11.0 all for python 2.5.
我使用的是 OS X 10.9.4。我已经从 macpython.org 安装了 matplotlib 和 numpy,并且我已经为 python 2.5 安装了 scipy 0.11.0。
Do I even need pytz? If not, how can I get around this error?
我什至需要 pytz 吗?如果没有,我该如何解决这个错误?
回答by Itay Kahana
pylabrequires pytz.
The easiest way to install a package in Python is to run pip install pytz.
pylab需要pytz。在 Python 中安装包的最简单方法是运行pip install pytz.
Today, Python comes with pip pre-installed, but use these instructions if you need to install it: Installation: Do I need to install pip?
今天,Python 预装了 pip,但如果您需要安装它,请使用以下说明:安装:我需要安装 pip 吗?
回答by Vicrobot
Firstly if you have installed pip then remove it by deleting the folder of pip inside python directory. Then install pip by this command:
首先如果你已经安装了 pip 然后通过删除 python 目录中的 pip 文件夹来删除它。然后通过以下命令安装pip:
$ sudo easy_install pip
Then you need to install pytz again by this command:
然后你需要通过这个命令再次安装pytz:
$ sudo pip install pytz
Don't update pip to 10th version because it might contain bugs which is causing problems.
不要将 pip 更新到第 10 版,因为它可能包含导致问题的错误。

