prompt-toolkit / ptpython

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

[Feature Request] A way to pre-execute code from the CLI (add support for the `-c` flag) #586

Closed bmeares closed 4 months ago

bmeares commented 4 months ago

One trick I often use is launching a Python REPL with a few lines of code already executed, usually to import a library or build an object. Is there an equivalent in ptpython?

Here's an example:

python -i -c 'import foo; import sys; print(">>> import foo")'

This launches a REPL with foo already imported, and the line import foo is printed.

>>> import foo
>>> 
bmeares commented 4 months ago

After some digging, I believe the best solution is to create a temporary file and either set PYTHONSTARTUP or just pass the file path directly. Not thrilled about handling temp files but it'll get the job done.