I would like to know which is the difference between
import httplib and
from httplib import *
By the way i am using python 2.73.

- 7
- 2
-
possible duplication of [Confirming the difference between import * and from xxx import *](http://stackoverflow.com/questions/4436401/confirming-the-difference-between-import-and-from-xxx-import) – May 25 '13 at 00:17
2 Answers
With import httplib
, you would have to do
httplib.HTTPConnection(host)
With from httplib import *
, you could do
HTTPConnection(host)
It is considered better to specify what you are importing in the second method, as in from httplib import HTTPConnection

- 45,752
- 12
- 65
- 86
-
It's preference, but if you use the `from` method, it's best to specify what you are importing instead of using the `*` operator. – SethMMorton May 25 '13 at 00:21
Here's the different, by example:
>>> import httplib
>>> SEE_OTHER
NameError: name 'SEE_OTHER' is not defined
>>> httplib.SEE_OTHER
303
>>> from httplib import *
>>> SEE_OTHER
303
>>> httplib.SEE_OTHER
NameError: name 'httplib' is not defined
The from httplib import *
is almost* never what you want, except possibly while experimenting with httplib
in the interactive prompt.
Sometimes you want to import a few names out of a library—but in that case, specify the names explicitly:
from httplib import SEE_OTHER, MOVED_PERMANENTLY
Both import the module, but the former creates a single new name, httplib
, in your current globals, while the latter instead copies all of the globals from httplib
into your current globals.**
* "Almost" because there are a few good use cases even in scripts. For example, the idiomatic way to provide a Python implementation of a module with C accelerators is to end the Python script foo.py
with from _foo import *
.
** This isn't quite true, because of __all__
and various other details, but close enough for now.

- 354,177
- 51
- 601
- 671