5

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.

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. All work just fine.

The problem arises when I want to drop into a pdb shell in a subprocess. I use a nodejs program along with a plugin which I use to invoke AWS code locally. The first nodejs process starts the second and the second one starts a python process which I have my usual pdb code in it:

import pdb; pdb.set_trace()

However the pdb shell that I get has no readline support. 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. I tried also setting PYTHONSTARTUP point to a file with this content:

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

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.

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

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

If I run the above code I get a python shell without readline support:

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

Some relevant thread on the net:

Update 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. 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. In nodejs the oneliner below will do it:

require("repl").start("node> ")
mehdix
  • 4,984
  • 1
  • 28
  • 36
  • What about other solutions as the ones mentioned here: https://stackoverflow.com/questions/543196/how-do-i-attach-a-remote-debugger-to-a-python-process – rocksportrocker Sep 06 '18 at 18:42
  • 1
    would you show an example of how the python subprocess invoked. – georgexsh Sep 07 '18 at 08:37
  • @rocksportrocker that thread solves a problem that I don't have: dubugging python. What I need is readline support. – mehdix Sep 09 '18 at 08:38
  • @georgexsh This is the main process spawning the plugin which will spawn python: https://github.com/serverless/serverless/blob/master/lib/plugins/aws/invokeLocal/index.js#L175 – mehdix Sep 09 '18 at 08:41
  • @georgexsh this is a plugin of serverless which launches the pyhon script lcoally: https://github.com/ubaniabalogun/serverless-package-python-functions/blob/master/index.js#L96 – mehdix Sep 09 '18 at 08:43
  • 1
    @MehdiSadeghiI don't think you could get an interactive shell with your current setup. while you say you want "readline support", I guess what you want is an interactive shell? – georgexsh Sep 10 '18 at 03:29
  • @georgexsh yes I want an interactive shell, basically I want to get suggestions when I press tab key. I normally have this in my setup, including python, pdb launched from python and my shell prompt. However when I double press tab in the scenario mentioned in the question, I don't get any of this, instead, the prompt moves forward. – mehdix Sep 10 '18 at 09:19
  • 1
    @MehdiSadeghi try change [this line](https://github.com/ubaniabalogun/serverless-package-python-functions/blob/master/index.js#L96) to `ChildProcess.spawnSync(cmd, args, { stdio: 'inherit' })`. – georgexsh Sep 10 '18 at 09:36
  • @georgexsh it didn't help. However here is the thing. My question boils down to launching a python interactive shell from nodejs. If you know how to do it just write it as an answer and I'll accept it. It is basically equivalent for this node oneliner `require("repl").start("node> ");` – mehdix Sep 13 '18 at 09:13

1 Answers1

1
const ChildProcess = require('child_process');                                 

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

works for me.

georgexsh
  • 15,984
  • 2
  • 37
  • 62
  • Do you get an interactive shell if you run it like this: ```ChildProcess.spawnSync('python', ['-c', 'import pdb;pdb.set_trace()'], { stdio: 'inherit' }); ``` (will pressing tab key give you suggestions?) – mehdix Sep 13 '18 at 09:25
  • For me I get suggestions in python but if I run your code with the above parameters I get a pdb shell that does not give me suggestions. Pressing tab key moves the blinker forward. – mehdix Sep 13 '18 at 09:27
  • @MehdiSadeghi what is your exact environment? like os, python version, console type etc. – georgexsh Sep 14 '18 at 09:12