我正在关注这个答案 https://stackoverflow.com/a/47595405/2153235将 Python 命令历史记录写入文件,该文件依赖于readline模块和写入历史文件其中的功能。我必须考虑在 Windows 10 上使用 Conda 提示符的差异,这只是为 Conda 设置了环境变量的 CMD 提示符。对于此用例,c:\User.Name 中没有历史文件,这是典型的情况。另外,我需要pyreadline3 https://stackoverflow.com/a/51964654/2153235.
这是我找到“模块路径”的方法写入历史文件 in pyreadline3:
from pprint import pprint
import inspect, pyreadline3
pprint(inspect.getmembers(pyreadline3.Readline))
<...snip...>
('write_history_file',
<function BaseReadline.write_history_file at 0x000001D67D83D280>)]^M^[[25l
<...snip...>
有一些令人费解的面向终端的控制字符,因为我使用了 Cygwin 的 Bash 和typescript
启动 Conda(参见here https://stackoverflow.com/a/76520838/2153235),但“路径”显示为BaseReadline.write_history_file。我得到的语法是写入历史文件 from 这个答案 https://stackoverflow.com/a/47595405/2153235。这是我使用它的方式,导致“AttributeError”:
>>> pyreadline3.BaseReadline.write_history_file('pyHistory')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\User.Name\.conda\envs\py39\lib\site-packages\pyreadline3\rlmain.p y",
line 180, in write_history_file
self.mode._history.write_history_file(filename)
AttributeError: 'str' object has no attribute 'mode'
即使我修饰文件名,我也会得到相同的 AttributeErrorpyHistory
带双引号。我在上面的 AttributeError 错误中唯一能找到的是本次问答 https://stackoverflow.com/questions/54969871/attributeerror-str-object-has-no-attribute-mode但它似乎并不适用,因为答案是提供了错误类型的参数。
还有什么方法可以追踪此错误的原因?
对我来说,越简单越好。我正在尝试访问实时命令历史记录,因为它可以帮助我尝试并开始使用 Python(和 Conda),来自历史记录始终可用的 Matlab。