Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix error "AttributeError: 'module' object has no attribute 'client' in python3?

Tags:

python

http

The following is my code.

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

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

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

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

like image 966
tianzhi0549 Avatar asked Aug 13 '14 05:08

tianzhi0549


1 Answers

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

So try this:

import http.client

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__

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.


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.


* 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.

like image 109
abarnert Avatar answered Sep 17 '22 14:09

abarnert