【问题标题】:Errors when I start the Python shell, errors from a script I ran a week ago启动 Python shell 时出错,一周前运行的脚本出错
【发布时间】:2015-03-06 14:21:56
【问题描述】:

我一直在学习 python,但我的一个系统上的 Python 交互式 shell 存在问题。它在每个 python-interactive-mode 启动时运行一个脚本(不带参数)。不知道去哪里找做这个的进程,我在这个系统上编了很多小脚本,也能看到什么脚本乱七八糟的,

当我这样做时:

user@Host ~/Python Scripts> python

我明白了:

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()

这是我运行的文件,在每次 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')

这是什么原因造成的,我该如何解决?

编辑#1

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

系统已 11 天未重新启动。

这里是:

~/Python Scripts> file tryexcept.py
tryexcept.py: Python script, ASCII text executable

编辑#2

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

~/Python Scripts> ls | grep readline
readline.py

【问题讨论】:

  • 您是否在 Windows 机器上编辑/写入了该文件? file &lt;that-script&gt; 说什么?
  • 不相关但你可以print(i, end=' '),你已经在遍历文件对象,你也应该关闭文件或使用with打开它
  • @EtanReisner 我会编辑我的问题,我忘了通知系统,我会添加。

标签: python linux file shell


【解决方案1】:

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))

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

在你的解释器中输入import readline;print(readline.__file__),看看你到底在导入什么

【讨论】:

    【解决方案2】:

    我在 Fedora 21 系统中遇到了同样的问题。在我的情况下发生的事情是我试图在已删除的文件夹中运行 python 解释器。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多