如何修复错误“AttributeError:'module'对象在python3中没有属性'client'? [英] How to fix error "AttributeError: 'module' object has no attribute 'client' in python3?

查看:2574
本文介绍了如何修复错误“AttributeError:'module'对象在python3中没有属性'client'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码。

import http
h1 = http.client.HTTPConnection('www.bing.com')

我认为没关系。但是python给我以下错误:

I think it's ok.But python give me the following error:


AttributeError:'module'对象没有属性'client'。

AttributeError: 'module' object has no attribute 'client'.

我想知道为什么以及如何修复它。谢谢。

I wanted to know why and how to fix it.Thanks.

推荐答案

首先,导入包没有' t自动导入其所有子模块。*

First, importing a package doesn't automatically import all of its submodules.*

所以试试这个:

import http.client






如果没有工作,然后很可能你有一个名为 http.py 的文件,或一个名为 http 的目录,某处你的 sys.path 上的其他内容(很可能是当前目录)。你可以很容易地检查:


If that doesn't work, then most likely you've got a file named http.py, or a directory named http, somewhere else on your sys.path (most likely the current directory). You can check that pretty easily:

import http
http.__file__

这应该给出一些目录,如 /usr/lib/python3.3/http / __ init __。py /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http / __ init __。py 或其他看起来很明显的系统y和STDLIB-Y;如果你改为 /home/me/src/myproject/http.py ,这就是你的问题。通过重命名模块来修复它,使其与您要使用的stdlib模块名称不同。

That should give some directory like /usr/lib/python3.3/http/__init__.py or /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/__init__.py or something else that looks obviously system-y and stdlib-y; if you instead get /home/me/src/myproject/http.py, this is your problem. Fix it by renaming your module so it doesn't have the same name as a stdlib module you want to use.

如果这不是问题,那么您可能有一个破碎的Python安装,或两个相互混淆的Python安装。最常见的原因是安装第二个Python编辑了 PYTHONPATH 环境变量,但是当你输入<$ c $时,你的第一个Python仍然是运行的Python c> python 。

If that's not the problem, then you may have a broken Python installation, or two Python installations that are confusing each other. The most common cause of this is that installing your second Python edited your PYTHONPATH environment variable, but your first Python is still the one that gets run when you just type python.

*但有时确实如此。这取决于模块。有时您无法判断某些东西是非模块成员的包(如 http ),还是包含子模块的模块( os )。幸运的是,没关系;无论是否有必要,它总是保存到 import os.path import http.client

* But sometimes it does. It depends on the module. And sometimes you can't tell whether something is a package with non-module members (like http), or a module with submodules (os). Fortunately, it doesn't matter; it's always save to import os.path or import http.client, whether it's necessary or not.

这篇关于如何修复错误“AttributeError:'module'对象在python3中没有属性'client'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆