简体   繁体   English

如何使readline在python子进程中工作?

[英]How to make readline work in a python subprocess?

I have spent quite something enabling readline support in pdb on MacOS Sierra in a subprocess which I don't understand why it fails, hence the question. 我花了很多东西在MacOS Sierra上的pdb中启用了readline支持的子进程,我不明白为什么它失败了,因此问题。

Please note that I have proper readline support without adding a .pdbrc file in all my python environemnts, including python2 and 3 installations and also in virtual environments created with pipenv, venv, or pew. 请注意,我没有在我的所有python环境中添加.pdbrc文件,包括python2和3安装,以及使用pipenv,venv或pew创建的虚拟环境中,我都有正确的readline支持。 All work just fine. 一切正常。

The problem arises when I want to drop into a pdb shell in a subprocess. 当我想在子进程中放入pdb shell时出现问题。 I use a nodejs program along with a plugin which I use to invoke AWS code locally. 我使用nodejs 程序和一个插件 ,我用它在本地调用AWS代码。 The first nodejs process starts the second and the second one starts a python process which I have my usual pdb code in it: 第一个nodejs进程启动第二个,第二个进程启动一个python进程,我有我常用的pdb代码:

import pdb; pdb.set_trace()

However the pdb shell that I get has no readline support. 但是我得到的pdb shell没有readline支持。 I tried the following alternatives which didn't work as well: 我尝试了以下替代方案,但效果不佳:

import ipdb; ipdb.set_trace()
import rlcompleter, readline
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('bind ^I rl_complete')

I also added .pdbrc file with above content (minus ipdb import) to no avail. 我还添加了.pdbrc文件以及上面的内容(减去ipdb导入)无济于事。 I tried also setting PYTHONSTARTUP point to a file with this content: 我也尝试将PYTHONSTARTUP指向具有以下内容的文件:

import rlcompleter, readline
readline.parse_and_bind('tab: complete')

It didn't help too. 它也没有帮助。 People had reported that these solutions have worked for them, but they didn't have readline support to start with (which for me it works just fine without these tricks). 人们已经报告说这些解决方案对他们有用,但他们没有开始的readline支持(对我来说,没有这些技巧它可以正常工作)。

I also tried patching my nodejs process.env.PATH and process.env.PYTHONPATH and added directories where I have python installation which have readline support to no avail. 我也尝试修补我的nodejs process.env.PATHprocess.env.PYTHONPATH并添加了我有python安装的目录,其中有readline支持无济于事。

I appreciate if anybody can explain whether there is a fundamental difference between launching pdb from a sub-sub-...-process and directly from terminal (which in anycase it is a subprocess too). 我很感激,如果有人能够解释从子子程序启动pdb和直接从终端启动pdb之间是否存在根本区别(在任何情况下它都是子进程)。 Moreover, I appreciate any suggestion that might help me solve this problem. 此外,我感谢任何可能帮助我解决这个问题的建议。

Update I 更新我

I noticed that even without pdb I don't get the readline support: 我注意到即使没有pdb我也没有得到readline支持:

import code
code.interact(local=locals())

If I run the above code I get a python shell without readline support: 如果我运行上面的代码,我得到一个没有readline支持的python shell:

Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

>>>
<pointer blinks here which is strange, it should be on the line above>

Update II 更新II

Some relevant thread on the net: 网上有一些相关的帖子:

Update III 更新III

After giving some thought to the problem and thanks to the georgexsh comment, I think my problem boils down to launching an interactive python REPL from nodejs. 在考虑了问题之后,感谢georgexsh评论,我认为我的问题归结为从nodejs启动交互式python REPL。 It must run in its own process and inputs such pressing TAB key should be sent to the python process and its stdout should be printed on the screen. 它必须在自己的进程中运行并且输入这样的按下TAB键应该被发送到python进程并且它的stdout应该被打印在屏幕上。 In nodejs the oneliner below will do it: 在nodejs中,下面的oneliner将执行此操作:

require("repl").start("node> ")
const ChildProcess = require('child_process');                                 

const ret = ChildProcess.spawnSync('python', [], { stdio: 'inherit' }); 

works for me. 适合我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM