title: How to make readline work in a python subprocess?
body: <p>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.</p>
  
  <p>Please note that I have proper readline support without adding a <code>.pdbrc</code> 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.</p>
  
  <p>The problem arises when I want to drop into a pdb shell in a subprocess. I use a nodejs <a href="https://github.com/serverless/serverless" rel="nofollow noreferrer">program</a> along with a <a href="https://github.com/ubaniabalogun/serverless-package-python-functions" rel="nofollow noreferrer">plugin</a> 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: </p>
  
  <pre><code>import pdb; pdb.set_trace()
  </code></pre>
  
  <p>However the pdb shell that I get has no readline support. I tried the following alternatives which didn't work as well:</p>
  
  <pre><code>import ipdb; ipdb.set_trace()
  import rlcompleter, readline
  readline.parse_and_bind('tab: complete')
  readline.parse_and_bind('bind ^I rl_complete')
  </code></pre>
  
  <p>I also added <code>.pdbrc</code> file with above content (minus ipdb import) to no avail. I tried also setting <code>PYTHONSTARTUP</code> point to a file with this content:</p>
  
  <pre><code>import rlcompleter, readline
  readline.parse_and_bind('tab: complete')
  </code></pre>
  
  <p>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).</p>
  
  <p>I also tried patching my nodejs <code>process.env.PATH</code> and <code>process.env.PYTHONPATH</code> and added directories where I have python installation which have readline support to no avail.</p>
  
  <p>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.</p>
  
  <h2>Update I</h2>
  
  <p>I noticed that even without pdb I don't get the readline support:</p>
  
  <pre><code>import code
  code.interact(local=locals())
  </code></pre>
  
  <p>If I run the above code I get a python shell without readline support:</p>
  
  <pre><code>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)
  
  &gt;&gt;&gt;
  &lt;pointer blinks here which is strange, it should be on the line above&gt;
  </code></pre>
  
  <h2>Update II</h2>
  
  <p>Some relevant thread on the net:</p>
  
  <ul>
  <li>Pipenv: <a href="https://github.com/pypa/pipenv/issues/497#issuecomment-328190362" rel="nofollow noreferrer">https://github.com/pypa/pipenv/issues/497#issuecomment-328190362</a></li>
  <li>Pew: <a href="https://github.com/berdario/pew/issues/150" rel="nofollow noreferrer">https://github.com/berdario/pew/issues/150</a></li>
  </ul>
  
  <h2>Update III</h2>
  
  <p>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:</p>
  
  <pre><code>require("repl").start("node&gt; ")
  </code></pre>