Thread: Import question

  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Rep Power

    Smile Import question

    I'm looking at some code examples for sockets, and find one example that shows:

    import socket

    and another that shows:

    from socket import *

    I don't understand the difference, and find that I can't do both because in one condition the 'import socket' instruction works for one example, but if I do both imports, it no longer works. Can someone please tell me what's going on here.

    Thanks much
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Rep Power
    The first is a standard import.
    import module
    anytime you want something from that module you have to qualify it with the module name; ie:
    The star import on the other hand
    from module import *
    dumps the entire contents of the module into your global namespace and you could access all of its contents unqualified.

    Star imports are bad; especially for neophytes that don't understand why they are bad. Don't use them and try to avoid any tutorials that do. It is much easier to never start using than to quit once already addicted.


IMN logo majestic logo threadwatch logo seochat tools logo