Search code examples
pythonpython-importlibrariespython-moduletqdm

Why do I need to "from tqdm import tqdm" instead of just "import tqdm"?


Probably a simple question, but I'm just getting started with Python and try to understand how the library stuff works.

So my question is why do I need to type

from tqdm import tqdm

instead of just

import tqdm

like with other libraries?

I get that when you only need one part of a library you can do this. But in this case my program doesn't work if I don't do it. Shouldn't be everything included with the second expression? If I run my program with it I get the error:

"TypeError: 'module' object is not callable"


Solution

  • The package tqdm has a module named tqdm inside it. Now to your tqdm you can

    1. Import all the modules in package tqdm and use the module called tqdm by:
    import tqdm
    
    for i in tqdm.tqdm(range(10)):
      pass
    

    OR

    1. Just import the tqdm module of the tqdm package by:
    from tqdm import tqdm
    for i in tqdm(range(10)):
      pass