Ankaj Gupta
August 04, 2020

AttributeError: module 'whois' has no attribute 'whois'

How to Fix AttributeError: Module 'whois' Has No Attribute 'whois'

In this post, I am going to tell you how you can easily fix the problem of AttributeError: module 'whois' has no attribute 'whois' error.

The Problem

Python code given below:

import whois

host = 'google.com'
res = whois.whois(host)
print(res)

This code results in: AttributeError: module 'whois' has no attribute 'whois'

The Solution

Switch module from pip install whois to pip install python-whois.

About python-whois:

  • • Create a simple importable Python module which will produce parsed WHOIS data for a given domain
  • • Able to extract data for all the popular TLDs (com, net, org, etc.)
  • • Query a WHOIS server directly instead of going through an intermediate web service
  • • Works with Python 2 & 3

Step-by-Step Fix

Step 1: Uninstall whois Module

Uninstall the whois module from your distribution:

pip uninstall whois

Step 2: Install python-whois

Now, install python-whois:

pip install python-whois

Step 3: Execute Your Code

Finally, execute your Python script:

python whois.py

Windows

python3 whois.py

Linux/Mac

Corrected Code

After installing python-whois, your code will work correctly:

import whois

host = 'google.com'
res = whois.whois(host)
print(res)

Example Output:

{
    "domain_name": "google.com",
    "whois_server": "whois.markmonitor.com",
    "registrar": "MarkMonitor, Inc.",
    "name_servers": [...],
    "creation_date": "1997-09-15T00:00:00",
    "expiration_date": "2028-09-14T00:00:00",
    ...
}

Why Does This Happen?

❌ Package Name Conflict

The whois package is different from python-whois. They have incompatible APIs.

✅ Correct Package

The python-whois package provides the WHOIS functionality you need.

Package Comparison

Package Description Status
whois Different package with incompatible API ❌ Not for this use case
python-whois Parsed WHOIS data for domains ✅ Recommended

Advanced Usage

Fetching Multiple Domains

import whois

domains = ['google.com', 'github.com', 'stackoverflow.com']

for domain in domains:
    try:
        info = whois.whois(domain)
        print(f"{domain}: {info.registrar}")
    except whois.parser.PywhoisError as e:
        print(f"{domain}: Error - {e}")

Extracting Specific Information

import whois

domain = 'google.com'
info = whois.whois(domain)

print(f"Domain: {info.domain_name}")
print(f"Registrar: {info.registrar}")
print(f"Creation Date: {info.creation_date}")
print(f"Expiration Date: {info.expiration_date}")
print(f"Name Servers: {info.name_servers}")

Troubleshooting

Still Not Working?

If there is still a problem, try the following:

  1. Restart your device and run it again
  2. Clear Python cache: find . -type d -name __pycache__ -exec rm -r {} +
  3. Check Python version compatibility
  4. Verify installation: pip list | grep -i whois

Common Errors

  • • Connection timeout: Check your internet connection
  • • TLD not supported: Some TLDs may not be supported by python-whois
  • • Rate limiting: Some WHOIS servers limit query frequency

Summary

To fix the AttributeError: module 'whois' has no attribute 'whois' error:

  1. Uninstall the incorrect package: pip uninstall whois
  2. Install the correct package: pip install python-whois
  3. Run your code again: python whois.py

The python-whois package provides a clean API to fetch and parse WHOIS data for any domain.

Python programming

Join the discussion

Comments

0 comments

No comments yet — be the first to share your thoughts.

Related Posts