brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python -i script.py leaves you in a prompt with the script's variables

Python
~/tips/tip-python-interactive-flag-after-script

python3 -i script.py

Runs a script and then keeps the interpreter open so you can inspect its state.

When a script ends and I still want to poke at its data, I do not add print lines, I ask python to stay.

python3 -i script.py
>>> df.head()
>>> _

-i runs the script and then opens the interactive prompt in the same namespace, so every global, function and open object is still there. It also drops you at the prompt after an uncaught exception, which lets you inspect the state with import pdb; pdb.pm(). In the prompt, _ holds the result of the last expression. Setting PYTHONINSPECT=1 in the environment has the same effect without changing the command.

Further reading: Python command line documentation

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link