-3

In the two following statements:

  1. import base64
  2. from base64 import *

since both would import the complete library yet we need to write base64.b64encode() for first case (to call b64encode() function from base64 library) and using the same for second case, we get a

NameError: name 'base64' is not defined.

For the second case b64encode() is used to call the function.

If they import the library completely then what causes this difference.

NOTE: base64 library is used only as an example. EDIT: I was hoping for an explanation to why this happens and not which is better to use.

Rakholiya Jenish
  • 3,165
  • 18
  • 28

1 Answers1

0

import module imports the module/package, so it's available via module.

If you use from module import *, everything which is within the module/package, will be imported to the (let's call it) global namespace. Thus module itself is not imported.

Please consider some basic research when asking (the next) questions, this one has already been answered like a zillion times.

tamasgal
  • 24,826
  • 18
  • 96
  • 135
  • *this one has already been answered like a zillion times.* better skip the answer and vote to close as duplicate then :-) – Tim May 19 '15 at 19:37
  • 1
    I was one of the first voters to close, however I could not resist to spend one minute ;-) You know: there's someone wrong on the internet!!! – tamasgal May 19 '15 at 19:38