如题,报错内容:
ModuleNotFoundError: No module named 'readline'
尝试使用
pip install readline
结果报错:
Collecting readline
Using cached readline-6.2.4.1.tar.gz (2.3 MB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
error: this module is not meant to work on Windows
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
遇到这个问题是因为尝试在Windows环境中安装`readline`模块,但`readline`是一个主要用于Unix-like系统的库,不原生支持Windows。Python在Windows上的标准安装包含一个名为`pyreadline`的替代模块,该模块旨在模仿`readline`的一些功能
对于这种情况,可以尝试安装`pyreadline`模块来代替`readline`
通过以下命令来安装`pyreadline`:
pip install pyreadline
安装`pyreadline`后,代码可能需要进行适当的调整,以确保它使用`pyreadline`而非`readline`
在大多数情况下,这应该是透明的,因为`pyreadline`旨在作为`readline`的一个替代品
如果代码直接引用了`readline`,可能需要将这些引用改为`pyreadline`,或者在代码中进行条件导入,以便在Windows上使用`pyreadline`,而在其他系统上使用`readline`
后续可能会遇到如下问题:
AttributeError: module 'collections' has no attribute 'Callable'
这个错误是因为pyreadline
库中的代码试图访问collections
模块中不存在的Callable
属性。这个问题通常是因为pyreadline
库与您使用的Python版本不完全兼容造成的。从Python 3.3开始,collections.Callable
已经被移动到了collections.abc
模块中
可以尝试手动修改导致问题的pyreadline
代码。找到出错的文件(举个例子是py3k_compat.py
),然后将有问题的行从
return isinstance(x, collections.Callable)
改为
return isinstance(x, collections.abc.Callable)
这样做是因为从Python 3.3开始,Callable
被移到了collections.abc
中。
请注意,直接修改库文件并不是一个推荐的做法,因为这会使环境与其他开发者或生产环境中的环境不一致,而且这些更改在未来安装或更新库时可能会被覆盖