AttributeError: 'module' 对象没有属性 'compile'

ach*_*low 1 python regex

我有以下代码保存为 re.py

import sys
pattern  ="Fred"
import re
regexp = re.compile(pattern)

for line in sys.stdin:
    result = regexp.search(line)
    if result:
        sys.stdout.write(line)
Run Code Online (Sandbox Code Playgroud)

当我在终端中执行此文件时 -

$python re.py < names.txt
Run Code Online (Sandbox Code Playgroud)

出现错误

regexp = re.compile(pattern)
AttributeError: 'module' object has no attribute 'compile'
Run Code Online (Sandbox Code Playgroud)

当我将文件名更改为 test.py

$python test.py < names.txt 
Run Code Online (Sandbox Code Playgroud)

仍然产生相同的错误

regexp = re.compile(pattern)
AttributeError: 'module' object has no attribute 'compile'
Run Code Online (Sandbox Code Playgroud)

是什么导致了错误以及如何修复它?谢谢!!