启动 Python shell 时出现错误,一周前运行的脚本出现错误

som*_*ing 3 python linux shell file

我一直在学习 python,但我的一个系统上的 Python 交互式 shell 有问题。它在每次 python 交互模式启动时运行一个脚本(不带参数)。我不知道在哪里寻找执行此操作的进程,我在这个系统上编写了很多小脚本,并且我可以看到什么脚本在乱搞,

当我这样做时:

user@Host ~/Python Scripts> python
Run Code Online (Sandbox Code Playgroud)

我得到:

Python 3.4.2 (default, Feb 21 2015, 22:19:02) 
[GCC 4.9.2 20150212 (Red Hat 4.9.2-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
# ! / u s r / b i n / e n v   p y t h o n 
 finished
Failed calling sys.__interactivehook__
Traceback (most recent call last):
  File "/usr/local/opt/python-3.4.2/lib/python3.4/site.py", line 396, in register_readline
    import rlcompleter
  File "/usr/local/opt/python-3.4.2/lib/python3.4/rlcompleter.py", line 161, in <module>
    readline.set_completer(Completer().complete)
AttributeError: 'module' object has no attribute 'set_completer'
>>> quit()
Run Code Online (Sandbox Code Playgroud)

这是我运行的文件,并且在每个 python 启动时运行:

#!/usr/bin/env python

try:
    number = int(input("Enter a number: "))
    print(number)
    aFile = open('modules.py')
    for i in aFile:
        print(aFile.readline(), end=' ')

except ValueError:
    print('Not a number, please re-enter:')
    number = int(input('Enter a number: '))
    print(number)

except IOError:
    print('Cannot open file')

print('finished')
Run Code Online (Sandbox Code Playgroud)

造成这种情况的原因是什么以及如何解决?

编辑#1

系统是Fedora 21,文件modules.py和启动交互式shell时运行的脚本是从未在Windows系统上编辑过的脚本。

系统已经 11 天没有重新启动了。

这是:

~/Python Scripts> file tryexcept.py
tryexcept.py: Python script, ASCII text executable
Run Code Online (Sandbox Code Playgroud)

编辑#2

我当前的工作目录中有一个 readline.py :

~/Python Scripts> ls | grep readline
readline.py
Run Code Online (Sandbox Code Playgroud)

Pad*_*ham 5

最后一部分rlcompleter.py尝试导入 readline,然后运行出错的行。

try:
    import readline
except ImportError:
    pass
else:
    readline.set_completer(Completer().complete)
    # Release references early at shutdown (the readline module's
    # contents are quasi-immortal, and the completer function holds a
    # reference to globals).
    atexit.register(lambda: readline.set_completer(None))
Run Code Online (Sandbox Code Playgroud)

您正在导入的路径中的某个位置必须有一个readline.py文件,而不是实际的 python 模块。如果您没有 readline.py 但曾经有过,则查找文件readline.pyc

在你的解释器中放入 aimport readline;print(readline.__file__)并查看你到底要导入什么


McL*_*ary 5

我在 Fedora 21 系统中遇到了同样的问题。在我的例子中,我试图在已删除的文件夹中运行 python 解释器。

发生这种情况是因为我导航到一个文件夹,比如在命令行中的Dropbox 。然后我使用“文件”应用程序删除Dropbox文件夹。返回控制台,当前文件夹仍然是Dropbox

从这个已删除的文件夹运行 python 会导致相同的错误。