我正在尝试从ThinkStats2代码中运行一个示例。我从github复制了所有东西--文件结构与github上的文件结构完全相同。
chap01soln.py
from __future__ import print_function
import numpy as np
import sys
import nsfg
import thinkstats2
def ReadFemResp(dct_file='2002FemResp.dct',
dat_file='2002FemResp.dat.gz',
nrows=None):
"""Reads the NSFG respondent data.
dct_file: string file name
dat_file: string file name
returns: DataFrame
"""
dct = thinkstats2.ReadStataDct(dct_file)
df = dct.ReadFixedWidth(dat_file, compression='gzip', nrows=nrows)
CleanFemResp(df)
return df
def CleanFemResp(df):
"""Recodes variables from the respondent frame.
df: DataFrame
"""
pass
def ValidatePregnum(resp):
"""Validate pregnum in the respondent file.
resp: respondent DataFrame
"""
# read the pregnancy frame
preg = nsfg.ReadFemPreg()
# make the map from caseid to list of pregnancy indices
preg_map = nsfg.MakePregMap(preg)
# iterate through the respondent pregnum series
for index, pregnum in resp.pregnum.iteritems():
caseid = resp.caseid[index]
indices = preg_map[caseid]
# check that pregnum from the respondent file equals
# the number of records in the pregnancy file
if len(indices) != pregnum:
print(caseid, len(indices), pregnum)
return False
return True
def main(script):
"""Tests the functions in this module.
script: string script name
"""
resp = ReadFemResp()
assert(len(resp) == 7643)
assert(resp.pregnum.value_counts()[1] == 1267)
assert(ValidatePregnum(resp))
print('%s: All tests passed.' % script)
if __name__ == '__main__':
main(*sys.argv)
我得到的ImportError如下所示:
Traceback (most recent call last):
File "C:\wamp\www\ThinkStats_py3\chap01soln.py", line 13, in <module>
import nsfg
File "C:\wamp\www\ThinkStats_py3\nsfg.py", line 14, in <module>
import thinkstats2
File "C:\wamp\www\ThinkStats_py3\thinkstats2.py", line 34, in <module>
import thinkplot
File "C:\wamp\www\ThinkStats_py3\thinkplot.py", line 11, in <module>
import matplotlib
File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 105, in <module>
import six
ImportError: No module named 'six'
所有文件都列在src文件夹下。没有包裹在src下。我尝试为nsfg和thinkstats添加包,但出现了另一个错误。我尝试将python2.7升级到python3.4。我还是会犯同样的错误。我知道我需要安装matplotlib的六个包,但是为什么我在nsfg上会出现导入错误呢?
发布于 2015-07-07 20:20:45
您在nsfg
上得到导入错误,因为它内部导入matplotlib (不是直接导入matplotlib,而是导入thinkplot
,它导入matplotlib
,后者依赖于six
模块)。而且您的计算机上没有安装该库,因此导入失败。
很可能您没有six
模块,您可以尝试使用- pip install six
安装它。
或者从这里获取它,解压缩并使用- python setup.py install
安装它
https://stackoverflow.com/questions/31283429
复制相似问题