Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用Python3的ImportError

使用Python3的ImportError
EN

Stack Overflow用户
提问于 2015-07-07 20:15:23
回答 1查看 974关注 0票数 1

我正在尝试从ThinkStats2代码中运行一个示例。我从github复制了所有东西--文件结构与github上的文件结构完全相同。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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如下所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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上会出现导入错误呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-07 20:20:45

您在nsfg上得到导入错误,因为它内部导入matplotlib (不是直接导入matplotlib,而是导入thinkplot,它导入matplotlib,后者依赖于six模块)。而且您的计算机上没有安装该库,因此导入失败。

很可能您没有six模块,您可以尝试使用- pip install six安装它。

或者从这里获取它,解压缩并使用- python setup.py install安装它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31283429

复制
相关文章
python3报:ImportError: No module named 'MySQLdb'
项目在转到python3.6时,原先的导入MySQLdb模块都提示无法导入,pip install mysqldb也安装失败。 问题原因: python2和python3在数据库模块支持这里存在区别,python2是mysqldb,而到了python3就变成mysqlclient,pip install mysqlclient即可。
墨文
2020/02/28
1.8K0
Python 的 ImportError 错误
问题 错误:ImportError: No module named XXX 在Python的工程中,偶尔会遇到文件无法引用的情况,即PyCharm IDE支持文件跳转,但是当文件执行时,找不到模块。
小小科
2018/05/03
1.1K0
Python 的 ImportError 错误
python ImportError:
 >>> import paramiko Traceback (most recent call last): File "<stdin>", line 1, in <module> python ImportError: No module named paramiko 模块没有安装 接下来安装 root@scpman:~# apt-cache search python* |grep paramiko python-paramiko - Make ssh v2 connections with Python apt-get install python-paramiko root@scpman:~# python Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import paramiko /usr/lib/python2.6/dist-packages/Crypto/Util/randpool.py:40: RandomPool_DeprecationWarning: This application uses RandomPool, which is BROKEN in older releases. See http://www.pycrypto.org/randpool-broken RandomPool_DeprecationWarning) >>> import paramiko >>> >>> print "www.scpman.com" www.scpman.com >>> 现在好用了
py3study
2020/01/06
5430
python3升级pip报错ImportError: cannot import name 'main'
把系统的python版本从默认的2切换到3后,使用pip3安装依赖报错,如下: Traceback (most recent call last): File "/usr/bin/pip3", line 9, in <module> from pip import main ImportError: cannot import name 'main' 解决办法: 解决:pip文件在usr/bin目录下,cd进去,进行以下修改 把下面的三行 from pip import main if __na
墨文
2020/02/28
1K0
ImportError: No modu
报错:ImportError: No module named bs4 我的Python版本 python --version python 2.7.5 按照网上方法尝试安装BeautifulSoup无果,BeautifulSoup据说能在python2的版本很好兼容,在python3版本兼容不是太好。 yum install BeautifulSoup     (不可行) yum install bs4               (不可行) 正确解决方法 yum install python-beautifulsoup 启发 rpm -qa|grep python可以看到python许多兼容库 安装的版本是 python-BeautifulSoup-3.2.1-7.el7.noarch 使用from bs4 import BeautifulSoup任然报错 使用import BeautifulSoup不报错
py3study
2020/01/08
3690
ImportError: No mod
  File "/usr/local/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
py3study
2020/01/08
6340
Python中ImportError:
Python脚本在编译的时候,经常会遇到ImportError: No module named *** 的错误 错误提示: ImportError: No module named request 问题分析: 原因是Python中有些模块未导入。 解决方法: 检查 from *** import *** 中模块名称是否有错误,如果没有错,就通过命令行 pip install *** 下载该模块,如果安装不成功,可以到http://www.lfd.uci.edu/~gohlke/pythonlibs/ 搜索下载。
py3study
2020/01/13
1.3K0
python错误 ImportError
python错误:ImportError: No module named setuptools
py3study
2020/01/09
7480
python3的socket使用
如果需要设置两台机器的端口,请查看博文 centos7开放端口和防火墙设置 需要实现两台机器的信息交互,使用 socket 进行调度。其中服务端为: #!/usr/bin/env python # -*- coding: utf-8 -*- import socket # 服务端ip server_address = ('192.168.229.129',10000) # 客户端ip client_address = ("192.168.229.130",10000) s = socket.socket(
机器学习和大数据挖掘
2019/07/01
6740
使用Python绘图库Matplotlib提示"ImportError: No module named 'tkinter'"
问题现象 通过pip3 install matplotlib安装完该绘图库后,进入Python交互式命令行中执行导入操作,返回错误如下 问题解决 看来,应该是Matplotlib模块依赖于tkint
用户1456517
2019/03/05
4.1K0
使用Python绘图库Matplotlib提示"ImportError: No module named 'tkinter'"
关于 python ImportError: No module named 的问题
今天在 centos 下安装 python setup.py install 时报错:ImportError: No module named sysconfig, 当时急着用,就顺手直接源码编译了一把,make install 后就 ok 了。 然后又在 cygwin 下安装时同样的问题,这下 cygwin 源码编译也不行了,因为会调用很多 linux 特有的类库。 虽然最后解决了 import 的问题,但是又报了其它错。比如 ldconfig 啥的,可能是我cygwin环境没装全。 只有
用户1177713
2018/02/24
2.9K0
关于 python ImportError: No module named 的问题
ImportError: No module named 'ConfigParser'
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
云雀叫了一整天
2019/09/29
2.3K0
uwsgi 的 ImportError: No module named 'encodings' 错误
在做的项目使用Django REST做后端,发现使用python3 manage.py runserver时能够正常启动,使用uwsgi时报错:
fanzhh
2019/08/20
2.7K0
Python3装饰器的使用
装饰器 简易装饰器模板 def wrapper(func): def inner(*args,**kwargs): print('主代码前添加的功能') ret=func(*args,**kwargs) print('主代码后添加的功能') return ret return inner @wrapper def func(): print('主代码') 利用装饰器完成的登录认证 def wrap
GhostCN_Z
2020/04/03
2550
Python3 goto 语句的使用
首先安装一个 goto 的包(因为官方是没有 goto 语句的)pip install goto-statement具体的语法from goto import with_goto @with_gotodef range(start, stop): i = start result = [] label .begin if i == stop: goto .end result.append(i) i +
狼啸风云
2019/11/05
3.8K0
Python3基础:集合的使用
a = {1:12,2:12,3:13,4:14,5:15,6:16} b = {1,2,3,4,5,6} print(type(a),type(b)) # <class 'dict'> <class 'set'> Python3集合练习 集合天生具备一个功能,就是所有值是唯一的,通俗点也可以理解为去重。
红芽
2020/08/19
4860
Python3中列表的使用
列表操作常用操作包含以下方法: 1、list.append(obj):在列表末尾添加新的对象 2、list.count(obj):统计某个元素在列表中出现的次数 3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) 4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置 5、list.insert(index, obj):将对象插入列表 6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 7、list.remove(obj):移除列表中某个值的第一个匹配项 8、list.reverse():反向列表中元素 9、list.sort([func]):对原列表进行排序
py3study
2020/01/03
2.7K0
[1207]ImportError:无法导入名称“ RandomizedLogisticRegression”
ImportError:无法导入名称“ RandomizedLogisticRegression”
周小董
2023/10/10
4280
[1207]ImportError:无法导入名称“ RandomizedLogisticRegression”
ImportError:无法导入名称“ RandomizedLogisticRegression”
周小董
2023/10/10
4140
python3——print使用
print的初步认识:对于科班出身的或有相关经验的人来说,学习python是相当有趣的事,因为可以做日常任务, 比如自动备份你的MP3;可以做网站,如YouTube就是Python写的;可以做网络游戏的后台,很多在线游戏的后台都 是Python开发的;可以爬数据,得到你想要的内容。总之就是能干很多很多事啦。而对于非科班的我来说,学习python 是非常辛苦的,从一行行print打印,到一个个关键字,从思想上就是一种改革,这个过程是既枯燥又有趣的,而我会的 第一条代码就是print('life is sh
py3study
2020/01/13
5380

相似问题

Python3和扩展ImportError

43

ImportError在python3中的作用

11

在Python3中使用相对导入的ImportError

14

python3 ImportError:无法导入名称

30

Python3 - ImportError:没有命名的模块

26
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文