1
import tailer

test = tailer.tail(open("test.txt"), 1)
#print(lines[1])

It's as simple as the code above, but it doesn't work.

(I saved it because it was successful once during the experiment, but an error occurs when I run it again later.)

Error content:

Traceback (most recent call last):
  File "c:\Users\user\Documents\VSCODE\python\V1\tailer.py", line 1, in <module>
    import tailer
  File "c:\Users\user\Documents\VSCODE\python\V1\tailer.py", line 3, in <module>
    test = tailer.tail(open("test.txt"), 1)
AttributeError: partially initialized module 'tailer' has no attribute 'tail' (most likely due to a circular import)
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
i y
  • 25
  • 3
  • Obviously something's wrong so it won't work, but... I don't know what's wrong... I am not an expert, but when I use "import" I always use it the same way.... – i y Jan 14 '21 at 04:34
  • Suspect there’s something conflict in your environment - is there suspicious files by the name ? – Daniel Hao Jan 14 '21 at 04:37
  • It looks like it should work the way it is. Try the same code with tailer.head() and see if it works – Mick Jan 14 '21 at 04:37
  • Can you share you python file name. – Hanumanth Reddy Aredla Jan 14 '21 at 04:51
  • The python file was named tailer.py. I changed the name to aaa.py and it ran immediately... It was good learning. Thanks for any help. – i y Jan 14 '21 at 04:53

2 Answers2

1

Looks like your file is called tailer.py, so when it does import tailer, it tries to load itself, which is usually a recipe for confusion.

Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15
-1

You named your program tailer.py. When you do an import tailer the local folder has priority over all other folders and you will import tailer.py again. Creating an import circle.

In other words: you have a name clash between your program and the library you are trying to import. Just rename the file to something else and try again.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48