这是一个简单的源代码,但它不运行

2024-04-29 06:15:21 发布

您现在位置:Python中文网/ 问答频道 /正文

import tailer

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

它和上面的代码一样简单,但不起作用

(我之所以保存它,是因为它在实验过程中成功了一次,但稍后再次运行时出错。)

错误内容:

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)

Tags: pytestimporttxtmostopenvscodeusers
2条回答

看起来您的文件名为tailer.py,因此当它执行import tailer操作时,它会尝试加载自身,这通常会导致混淆

您将程序命名为tailer.py。执行import tailer操作时,本地文件夹的优先级高于所有其他文件夹,您将再次导入tailer.py。创建导入圆

换句话说:您的程序和您试图导入的库之间存在名称冲突。只需将文件重命名为其他文件,然后重试

相关问题 更多 >