76

I import tqdm as this:

import tqdm

I am using tqdm to show progress in my python3 code, but I have the following error:

Traceback (most recent call last):
  File "process.py", line 15, in <module>
    for dir in tqdm(os.listdir(path), desc = 'dirs'):
TypeError: 'module' object is not callable

Here is the code:

path = '../dialogs'
dirs = os.listdir(path)

for dir in tqdm(dirs, desc = 'dirs'):
    print(dir)
Zhao
  • 2,043
  • 1
  • 17
  • 36
  • Possible duplicate of [TypeError: 'str' object is not callable (Python)](http://stackoverflow.com/questions/6039605/typeerror-str-object-is-not-callable-python) – Morgan Thrapp Sep 05 '16 at 03:18
  • It almost seems like the devs should make a custom error for this because it is such a common mistake. – eric May 11 '21 at 21:24

4 Answers4

155

The error is telling you are trying to call the module. You can't do this.

To call you just have to do

tqdm.tqdm(dirs, desc='dirs') 

to solve your problem. Or simply change your import to

from tqdm import tqdm

But, the important thing here is to review the documentation for what you are using and ensure you are using it properly.

idjaw
  • 25,487
  • 7
  • 64
  • 83
17

You Have Used Only tqdm, Actually it is tqdm.tqdm So, Try

from tqdm import tqdm

for dir in tqdm(dirs, desc = 'dirs'):
print(dir)
Kranthi
  • 452
  • 4
  • 3
  • Wow! Actually solved my issue. I do stupid imports and then regret later. I should have checked earlier. – SAch Jul 12 '23 at 02:12
9

tqdm is a module (like matplotlib or pandas) that contains functions. One of these functions is called tqdm. Therefore, you have to call tqdm.tqdm to call the function within the module instead of the module itself.

Jake
  • 683
  • 6
  • 5
1
from tqdm import tqdm
with open(<your data>, mode='r', encoding='utf-8') as f:
    for _, line in enumerate(tqdm(f)):
       pass
Yap
  • 41
  • 4
  • 5
    Please add some context to this, possibly an explanation why the code should be altered like that to help people with similar problems in the future. Thank you! – creyD Mar 25 '19 at 05:22