42

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
  • 1,533
  • 1
  • 13
  • 31
  • 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

95

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
  • 21,992
  • 6
  • 53
  • 69
6

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
  • 183
  • 2
  • 4
4

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
  • 204
  • 2
  • 2
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
  • 3
    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