prompt-toolkit / ptpython

A better Python REPL
BSD 3-Clause "New" or "Revised" License
5.23k stars 281 forks source link

{ptpython.ptipython}.embed() should load ptpython config by default #518

Open wookayin opened 1 year ago

wookayin commented 1 year ago

When running ptpython.ipython.embed() or ptpython.repl import embed(), configs and history are not loaded -- it just uses the default config. It is not an intuitive behavior because when ~/.config/ptpython/config.py exists. Currently the config file is readed and applied ONLY when running as the entry-point program (e.g. python -m ptpython or python -m pypython.entry_points.run_ptipython.

In order to make the config loaded, at this moment one should explicitly set configure=...:

from ptpython.ipython import embed
from ptpython.repl import run_config
from ptpython.run_ptpython import get_config_and_history_file

# NOTE: should use get_config_and_history_file to respect 
# $PTPYTHON_CONFIG_HOME and platform-dependent directories,
# but this requires a refactoring to make the function reusable
# Note that some people might be using a different config file path
config_file = os.path.expanduser("~/.config/ptpython/config.py")

embed(..., configure=lamdda repl: run_config(repl, config_file))

which is too verbose; reading ptpython config files should be the default behavior. The current implementation (and #126) may read some config files from ~/.ipython/profile_default, but not the ptpython config itself.

What I suggest is to move the logic of reading and loading ptpython config into embed() and read the config instead of using the default config. This should be a straightforward fix, but @jonathanslenders do you have any opinions or is there something that I might have missed?