0

I encountered an error on Eclipse PyDev AttributeError: '_socketobject' object has no attribute 'getservbyname' with the following code:

from socket import *
help(socket.getservbyname)
Paul Lo
  • 6,032
  • 6
  • 31
  • 36
sohow
  • 3
  • 1
  • Possible duplicate http://stackoverflow.com/questions/1857146/python-attribute-error-type-object-socketobject-has-no-attribute-gethostbyn – dotnetstep Dec 20 '14 at 04:14

2 Answers2

3

when you are doing a star import, you dont have to append the module name to the method.

try this:

import socket
help(socket.getservbyname)

In case, In case you want to do a star import, which is not recommended because of namespace polluting, this is what you should be doing:

from socket import *
help(getservbyname)
Tarun Gaba
  • 1,103
  • 1
  • 8
  • 16
0

With python, you have three forms of import you can do

import socket
help(socket.getservbyname)

This form imports the whole of the socket module into your code with the same name. You then access things in it from their prefixed name

Next up:

import socket as othersocket
help(othersocket.getservbyname)

This imports the whole of the socket module into your code, but with an alternate name. You then access things from it with the alternate prefixed name

Finally:

from socket import *
# or
from socket import getservbyname
help(getservbyname)

This way, you import just specific bits from the module into your code, with their names without a prefix.

As you can hopefully see, there's a few ways to do it, you just need to be consistent about how you import vs how you use

Gagravarr
  • 47,320
  • 10
  • 111
  • 156