Python 3 – Pycharm – Jira REST API – Error: module 'http' has no attribute 'client'

Question:

hope you’re all fine

You’ll see, I’m using PyCharm 2022.3 (Community Edition) with Python 3.9.7, and I’m trying to execute the following code:

#!/usr/bin/env python3

# Required libraries for the program.
import requests as req
from requests.auth import HTTPBasicAuth
import json

url = "https://your-domain.atlassian.net/rest/api/2/user"

auth = HTTPBasicAuth("[email protected]", "xXx_token_xXx")

headers = {
  "Accept": "application/json"
}

query = {
  'accountId': '934250915yfe67e125d6062v'
}

response = req.request(
   "GET",
   url,
   headers=headers,
   params=query,
   auth=auth
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

It comes from: Jira REST API – Get user

And I’m getting the following error:

Traceback (most recent call last):
  File "C:UsersusernameDesktopGitHubProjectsTestsemail.py", line 4, in <module>
    import requests as req
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesrequests__init__.py", line 43, in <module>
    import urllib3
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesurllib3__init__.py", line 11, in <module>
    from . import exceptions
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesurllib3exceptions.py", line 3, in <module>
    from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 565, in module_from_spec
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesurllib3packagessix.py", line 234, in create_module
    return self.load_module(spec.name)
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesurllib3packagessix.py", line 209, in load_module
    mod = mod._resolve()
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesurllib3packagessix.py", line 118, in _resolve
    return _import_module(self.mod)
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesurllib3packagessix.py", line 87, in _import_module
    __import__(name)
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libhttpclient.py", line 71, in <module>
    import email.parser
  File "C:UsersusernameDesktopGitHubProjectsTestsemail.py", line 5, in <module>
    from requests.auth import HTTPBasicAuth
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesrequestsauth.py", line 16, in <module>
    from ._internal_utils import to_native_string
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesrequests_internal_utils.py", line 10, in <module>
    from .compat import builtin_str
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libsite-packagesrequestscompat.py", line 47, in <module>
    from http import cookiejar as cookielib
  File "C:UsersusernameAppDataLocalProgramsPythonPython39libhttpcookiejar.py", line 36, in <module>
    import urllib.parse, urllib.request
  File "C:UsersusernameAppDataLocalProgramsPythonPython39liburllibrequest.py", line 1379, in <module>
    if hasattr(http.client, 'HTTPSConnection'):
AttributeError: module 'http' has no attribute 'client'

The weird thing is… it only happens when I run the code like this:
Running code with Pycharm default runner

But, it works when I select "Run File in Python Console" (as you can see the choice in the image above)

So… I don’t know what is going on

I have tried some things:

  • pip install http
  • pip install -U http
  • pip install -U setuptools

And a lot of other bunch of commands

I’m a bit lost on what to do…
Could you give me a hand?

Thanks in advance

Asked By: snowbreacker

||

Answers:

The name of your file that you created is email.py. The name of a module required for the JIRA Python library to work is also called email. This causes a conflict which creates the error.

This is noticeable in your traceback:

 import email.parser
  File "C:UsersusernameDesktopGitHubProjectsTestsemail.py", line 5, in <module>

You can fix this by renaming your file to something else.

Answered By: Xiddoc