2

I have Python 3.6.5 on Mac 10.14. In the Python interpreter, editing/navigation shortcuts (such as arrows, ^e, ^a, etc.) do not work, instead giving me escape chars, e.g.:

Python 3.6.5 (default, Jun 17 2018, 12:13:06)

[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

import blah ^[[A^[[C^[[D^A^E

pip refuses to install readline giving a weird error; I installed gnureadline: it works but ONLY if I explicitly do:

import gnureadline

as a first command in the interpreter. Which is very ugly.

Is there a better way?

1 Answer 1

0

You can make use of the PYTHONSTARTUP environment variable to point to a dotfile that imports the library. This env var is only used for running the interactive prompt and doesn't affect your other projects.

You'll first need to create that environment variable if you don't have it already. You should be able to do this with the export command, which modifies your current shell environment. To make sure your shell environment always knows about this new environment variable, you'll need to include your export statement in either your ~/.bash_profile, ~/.bashrc, ~/.profile, or other shell configuration file found in your home directory. If you're not aware of how to find those, run the command ls -a ~ (that's a tidle). To search your home directory. Once you find the right file, add this to the bottom:

export PYTHONSTARTUP=/Users/yourusername/.pythonstartup

Once you've saved that in the file, you'll now create the startup file where you can include your import statement. As referenced above, this goes in your home directory under the name .pythonstartup. Within that file include your import statement:

import gnureadline

You may also wish to include a print function just to let yourself know that you're always importing gnureadline in the interactive prompt:

print("gnureadline imported via .pythonstartup file in home dir")

You should open a new terminal tab or new terminal instance once this is all finished. Then start up your python interpreter and you should see the above message and be able to use your library and the shortcuts it affords.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .