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

后端 未结 1 1210
无人共我
无人共我 2021-02-15 16:51

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

1条回答
  •  难免孤独
    2021-02-15 17:34

    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.

    0 讨论(0)
提交回复
热议问题