Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:04:59+00:00 2026-06-15T01:04:59+00:00

Normally, if I imported socket , I would be able to easily catch exceptions:

  • 0

Normally, if I imported socket, I would be able to easily catch exceptions:

>>> import socket
>>> try:
...     socket.gethostbyname('hello')
... except socket.gaierror:
...     print('oops')
...
oops

But if I just import socket.gethostbyname, it won’t work:

>>> from socket import gethostbyname
>>> try:
...     gethostbyname('hello')
... except socket.gaierror:
...     print('oops')
...
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
NameError: name 'socket' is not defined

I also get a NameError if I try to catch gaierror.

Is there any workaround for this? Is it not possible to catch an exception with a string (eg. except 'socket.gaierror':)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:05:00+00:00Added an answer on June 15, 2026 at 1:05 am

    If you do not want to import the full module you can simply import the exception aswell. PEP8 states that you are allowed to do.

    from socket import gethostbyname, gaierror
    

    http://www.python.org/dev/peps/pep-0008/#imports

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Normally I would set the pool size as development: adapter: postgresql encoding: unicode database:
Normally you would use something like attr_accessible :name, :email to allow for mass assignment
We normally use Eclipse for a particular Java project, but recently I imported the
Normally, I would have to do: $str = classname; //classname is the name of
Normally, one would expect, and hope, that two casts are needed to first unbox
Normally if you wanted to execute a MySQL command you would just open up
Normally if you wanted to execute a MySQL command you would just open up
Normally you make a call to a service, it blocks until a thread in
Normally, you don't actually edit a file in vim. If you run vim foo
Normally all the document related to an iOS application are stored in a document

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.