If you are told cannot by Python import, review the file name

Reference: [Python] [Study] Introduction to Python (35) --Import processing

tqdm.py


from tqdm import tqdm
from time import sleep

for i in tqdm(range(10)):
  sleep(1)

Output


Traceback (most recent call last):
  File "./tqdm.py", line 1, in <module>
    from tqdm import tqdm
  File "~/tqdm.py", line 1, in <module>
    from tqdm import tqdm
ImportError: cannot import name tqdm

This is an error that occurred when I casually made the file name the same as a new module test.

This can be avoided by simply renaming the file.

Why

Quoted from reference


__1. Find the module file to import __ A module search path is used to search for module files. Basically, the module search path is set automatically. The module search path has four elements. The elements are described below.

  1. Home directory of top-level files
  2. The directory specified by the environment variable PYTHONPATH
  3. Standard library module directory
  4. Contents of .pth file

When changing the module search path, usually change the environment variable PYTHONPATH or .pth. 1 is searched first, otherwise 2. Finally 4 is searched.

__ Home directory of top level files __ The home directory is usually the directory where top-level files are located. The first location to be searched when importing a module.

__ Environment variable PYTHONPATH__ An environment variable that describes the directory path that depends on the environment. Multiple values can be set, and if multiple values are specified, the search will be performed in order from the left end. The set value will be the location next to the home directory of the top-level file.

__Standard Library Module Directory __ The directory where the standard library is installed. Where to look next to the value of the environment variable PYTHONPATH.

__.pth file contents __ You can specify multiple lines, one pass per line. If you put this file in the "appropriate place" Searches for the location of the value in each line, starting from the first line in the contents of the .pth file. It is currently unknown where the "appropriate place" is.

In summary

__ "Oss! Oh, tqdm.py is a file! I call tqdm with import, but I don't know such a module. But I wonder what to do because I'm called ... I'll just throw an error! !! "__

It's almost right.

Recommended Posts

If you are told cannot by Python import, review the file name
Read the file line by line in Python
Read the file line by line in Python
The file name was bad in Python and I was addicted to import
Check if the characters are similar in Python
When I name the file flask.py in Flask, I get Import Error: cannot import name'Flask'
If you think the PyCharm environment is broken, it's because of the file name
What to do if you are told "Import Error: cannot import name'HTTPSHandler'" when building a virtual environment using virtualenv
I tried searching for files under the folder with Python by file name
The material covered by the module name cannot be called correctly at the import destination / source.
[Python] You can save an object to a file by using the pickle module.
The avi file output by OpenCV cannot be played
Read the xml file by referring to the Python tutorial
Python program that looks for the same file name
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
What to do when "cannot import name xxx" [Python]
[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.
How to check if the contents of the dictionary are the same in Python by hash value
[Python3 / ElementTree] If you cannot access the XPath format well, check the XML hierarchy carefully ... (self-advised)
[python] Change the image file name to a serial number
How to switch the configuration file to be read by Python
If __name__ == Raise your hand, if you write the code under'__main__'
How to import a file anywhere you like in Python
[Note] Import of a file in the parent directory in Python
(Python3) No. oO (Are you using the standard library?): 5 shaders
Try to decipher the garbled attachment file name with Python
If you think that the person you put in with pip doesn't work → Maybe you are using python3?
If I thought I didn't see the pyc file recently, it was quarantined in pycache by python3.
Checking if you are Sudoku
Python "if __name__ == ‘__main__’: ”means
[Python] if __name__ == What is'__main__' :?
ImportError: cannot resolve import name
ImportError: cannot resolve import name
Can you delete the file?
If you get stuck in Cannot load mkl_intel_thread.dll in Python on Windows
python Note: Determine if command line arguments are in the list
Check if the password hash generated by PHP matches in Python
from scipy.misc If "ImportError: cannot import name imread" occurs in import imread, imresize
Sort the file names obtained by Python glob in numerical order
Import modules that are often used when starting the python interpreter
What to do if you get the error Target WSGI script'/var/www/xxx/xxx.wsgi' cannot be loaded as python module