3

Trying to execute the following code to check how the progress bar works with the tqdm module but receiving an "ImportError: cannot import name 'tqdm' from 'tqdm' "

Code

from tqdm import tqdm
for i in tqdm(range(0,100)):
    print(i)
Community
  • 1
  • 1
Mohammed Ansari
  • 31
  • 1
  • 1
  • 2

2 Answers2

3

Step 1: pip install tqdm
Step 2: If your filename is tqdm.py then you needed to rename it to another name. hope it will work.

1

That is because python is trying to import tqdm from the wrong file, not from the actual tqdm pacakge, if you are sure you have installed tqdm correctly you need to change the code to this:

import tqdm
print(tqdm.__file__)

If you are on Linux you should get something like:

/usr/lib/python3.7/site-packages/tqdm/__init__.py

To find out where the tqdm file is located, if you realize this is one of your own files that was mistakenly named tqdm you can rename that file and your problem should be fixed in case you have installed tqdm correctly.

Also that Is not how you would use tqdm you use it for tasks that don't output anything themselves and their progress is not visible in any other way(like from what they are printing). If you want to use tqdm then you shouldn't print anything in your loop, try something like this:

import time
from tqdm import tqdm

for i in tqdm(range(0,100)): 
    time.sleep(1) 

P.S. It's highly recommended that you never install python packages with pip into the root python directory, and instead you get used to always using a virtualenvironment, or if not at least use pip install --user to install them to your own home directory. Otherwise you might pick bad habits and ruin the lives of others whenever you use a system with multiple users.

yukashima huksay
  • 5,834
  • 7
  • 45
  • 78