Skip to content
Latchkey

python -c: Run a One-Liner in CI

Execute a quick Python snippet without a file.

python -c runs the Python code passed as a string. CI scripts use it for quick assertions - verifying an import works or printing a value - without a temp file.

What it does

Executes the supplied program string and exits. The exit code reflects success or an unhandled exception, so it doubles as a check in a pipeline step.

Common usage

Terminal
python -c "import sys; print(sys.version)"
python -c "import requests; print(requests.__version__)"
python -c "import myapp; print('import ok')"

Common CI gotcha: shell quoting

Mixing the shell’s quotes with Python string quotes breaks the snippet. Wrap the program in double quotes and use single quotes inside, or vice versa.

Terminal
# Use double outside, single inside:
python -c "import json; print(json.dumps({'ok': True}))"

Options

FlagDoes
-c CMDRun CMD as a program
-W ACTIONSet a warnings filter

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →