13

I have the following Python script:

import http
import requests
from bs4 import BeautifulSoup
import urllib3
import pyrebase
import numpy as np
import yagmail
import time
from datetime import datetime, timedelta
import sys
import logging
import colorama
import csv
from random import randint
from numpy import genfromtxt
import sched, time
import threading
import http.client

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db


# Fetch the service account key JSON file contents
cred = credentials.Certificate('service-account-credentials.json')
# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://werrwrrw-catalogue.firebaseio.com'
})


config = {
    "apiKey": "BiXzaSdwhjwrhwjjrhwr",
    "authDomain": "whjwrhwjjrhwr.firebaseapp.com",
    "databaseURL": "https://whjwrhwjjrhwr.firebaseio.com",
    "projectId": "whjwrhwjjrhwr",
    "storageBucket": "rewrrrrr.appspot.com",
    "messagingSenderId": "606543434441"
}



firebaseuser = pyrebase.initialize_app(config)

auth = firebaseuser.auth()
dbuser = firebaseuser.database()

subref = db.reference('Subcribers').get()

for key, val in subref.items():
    subcriber_email = val['Email']
    print(key,subcriber_email)

The python file was working fine before, but now when I try to run it, it shows the following error:

>>Traceback (most recent call last):
  File "fax.py", line 1, in <module>
    import requests
  File "/Users/name/anaconda/lib/python3.6/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/Users/name/anaconda/lib/python3.6/site-packages/urllib3/__init__.py", line 8, in <module>
    from .connectionpool import (
  File "/Users/name/anaconda/lib/python3.6/site-packages/urllib3/connectionpool.py", line 11, in <module>
    from .exceptions import (
  File "/Users/name/anaconda/lib/python3.6/site-packages/urllib3/exceptions.py", line 2, in <module>
    from .packages.six.moves.http_client import (
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 646, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 616, in _load_backward_compatible
  File "/Users/name/anaconda/lib/python3.6/site-packages/urllib3/packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/Users/name/anaconda/lib/python3.6/site-packages/urllib3/packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/Users/name/anaconda/lib/python3.6/site-packages/urllib3/packages/six.py", line 82, in _import_module
    __import__(name)
  File "/Users/name/anaconda/lib/python3.6/http/client.py", line 71, in <module>
    import email.parser
  File "/Users/name/Desktop/Google Drive/FEBB/serverless/crwlr/email.py", line 3, in <module>
    from bs4 import BeautifulSoup
  File "/Users/name/anaconda/lib/python3.6/site-packages/bs4/__init__.py", line 35, in <module>
    from .builder import builder_registry, ParserRejectedMarkup
  File "/Users/name/anaconda/lib/python3.6/site-packages/bs4/builder/__init__.py", line 323, in <module>
    from . import _html5lib
  File "/Users/name/anaconda/lib/python3.6/site-packages/bs4/builder/_html5lib.py", line 20, in <module>
    import html5lib
  File "/Users/name/anaconda/lib/python3.6/site-packages/html5lib/__init__.py", line 19, in <module>
    from .serializer import serialize
  File "/Users/name/anaconda/lib/python3.6/site-packages/html5lib/serializer/__init__.py", line 5, in <module>
    from .htmlserializer import HTMLSerializer
  File "/Users/name/anaconda/lib/python3.6/site-packages/html5lib/serializer/htmlserializer.py", line 15, in <module>
    from xml.sax.saxutils import escape
  File "/Users/name/anaconda/lib/python3.6/xml/sax/saxutils.py", line 6, in <module>
    import os, urllib.parse, urllib.request
  File "/Users/name/anaconda/lib/python3.6/urllib/request.py", line 1350, in <module>
    if hasattr(http.client, 'HTTPSConnection'):
AttributeError: module 'http' has no attribute 'client'

I can't really pinpoint the error. How do I read the fix the error based on the traceback above?

MLavoie
  • 9,671
  • 41
  • 36
  • 56
e.iluf
  • 1,389
  • 5
  • 27
  • 69
  • 2
    `import urllib; urllib.request.http.client` works fine for me. Do you have a file / folder named `http` in your project directory by any chance. It should be opening the one located at `anaconda/lib/python3.6/http`. – FHTMitchell Aug 22 '18 at 09:34
  • @FHTMitchell How could I just reinstall it to be sure? – e.iluf Aug 22 '18 at 09:39
  • 2
    `http` is part of the python stdlib, you'd have to fully reinstall anaconda. I wouldn't bother though, that's probably not the issue – FHTMitchell Aug 22 '18 at 09:40
  • @FHTMitchell i was thinking maybe I need to update something – e.iluf Aug 22 '18 at 09:41
  • 1
    Try `import http; print(http.__file__)` on the Python prompt. Or even on the comment line `python3 -c "import http; print(http.__file__)"`. – 9769953 Aug 23 '18 at 11:21
  • @9769953 this is what i got in response /Users/name/anaconda/lib/python3.6/http/__init__.py – e.iluf Aug 23 '18 at 21:31
  • And can you import `http.client` normally on the Python command line? – 9769953 Aug 24 '18 at 04:19
  • @9769953 how can the be done? – e.iluf Aug 24 '18 at 09:58
  • Try this and see `from http import client` – Vijay Anand Pandian Sep 16 '18 at 03:41
  • why are you calling `import http` in line number 1 and `import http.client` in line number 18? That, ok. I guess the error is from Bs4 `from bs4 import BeautifulSoup`. Remove all `import http` and `import http.client` check if you could use bs4 module successfully in same script. – Vijay Anand Pandian Sep 16 '18 at 03:47
  • 1
    `File "/Users/name/Desktop/Google Drive/FEBB/serverless/crwlr/email.py", line 3, in from bs4 import BeautifulSoup` If you have any local `http.py` in your working directory or in the same environment. Kindly rename it to `http_lib.py`. There are lots of chances for shadowing the python library files by our own library files. – Vijay Anand Pandian Sep 16 '18 at 03:50
  • 1
    This seems wired. Have you tried executing the script in a fresh virtualenv ? – Arun Karunagath Sep 17 '18 at 11:03

5 Answers5

7

Here:

File "/Users/name/anaconda/lib/python3.6/http/client.py", line 71, in <module>
    import email.parser
File "/Users/name/Desktop/Google Drive/FEBB/serverless/crwlr/email.py"
    from bs4 import BeautifulSoup

The local email.py in /Users/name/Desktop/Google Drive/FEBB/serverless/crwlr/ shadows the stdlib's one. Now in your local email.py module, you're importing bs4, which imports html5lib, which imports xml.sax.saxutils, which imports urllib.request, which wants to import http.

IOW you end up with an (accidental) circular dependencie. At this point the http module is only partially imported, and doesn't yet have defined "client", hence the error.

The simple solution is to rename your "email.py" module to something else, or (if it's only a script and not a module) move it out of your pythonpath.

EDIT: I just noticed that your code started by importing http, so the http module should be already fully loaded, so even if the problem with your email.py script/module needs to be fixed, this shouldn't lead to this problem. So chances are you have another http.py module or http package in your sys.path shadowing the stdlib's one. To debug this, add this line just after the import http one:

 print(http)

This should print something like:

<module 'http' from '/some/path/to/a/python/file.pyc`>

If the path is not the one to your python install stdlib's "http/init.pyc" then you found the offender. If it's one of your own scripts/modules, the fix is the same as for email.py.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
1

Might be Bs4 is raising the exception, Kindly execute the below script in the existing one validate Bs4 import is working fine

try:
    from bs4 import BeautifulSoup
except Exception as err:
    raise ImportError('Bs4 is not imported correctly. - {}'.format(err))
Vijay Anand Pandian
  • 1,027
  • 11
  • 23
1

I happened to have a local module file named email.py, which called import error of requests.

Ref: Python modules with identical names (i.e., reusing standard module names in packages)

Junji Shimagaki
  • 286
  • 2
  • 9
  • This solved it for me. I had to rename my local calendar.py to be something more unique that did not collide with another Python lib. – MarkHu Aug 01 '22 at 19:56
-1

I am no expert myself, but try doing this in the first line

import http as http_

because I see in error message python is trying to get client from http module
my guess is that is happening because there are http module and http folder where folder contains other modules (like client)

Superior
  • 787
  • 3
  • 17
-3

So

>>> import http
>>> import http.client
>>> if hasattr(http.client, 'HTTPSConnection'):
...     print (type(http.client))
... 
<class 'module'>
>>> 

That http.client should be module and it should not call client attribute on http instance, right?

solution - I don't know :)

Sopan
  • 644
  • 7
  • 13
  • 1
    If you don't know why did you post an answer ? – bruno desthuilliers Sep 21 '18 at 12:40
  • 1
    I tried to bring `http.client` as module into focus. That can give good clue. One need not know complete solution or need not post the solution. One or two clues are also sufficient. – Sopan Sep 21 '18 at 13:57