How to Run Python Code from the Command Line with python -c
Sometimes you need one quick line of Python: print a version, make a random secret, check a calculation. Writing a script file for that is overkill. python -c runs Python code given right on the command line. This guide shows it on Python 3.15.2.
Syntax
python -c "<Python statements, separated by ;>"
| Part | What it means |
|---|---|
-c | Run the code that follows instead of a script file |
"<statements>" | The code, in quotes so the shell passes it as one piece. Separate statements with ; |
Python's command line documentation says it will "Execute the Python code in command". In a uv project, put uv run in front so the project's Python runs it.
The Example
The first command prints a greeting. The second imports sys and prints the major and minor version of the Python that ran it.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Run a One-Line Program
uv run python -c "print('Hello from the command line')"
Hello from the command line
The whole program is the text in double quotes. Inside it, the greeting uses single quotes so the two kinds don't clash.
Step 2: Run Several Statements
uv run python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")'
3.15

; separates the two statements. This time the outer quotes are single, so the f-string inside can use double quotes. Commands like this are handy inside other commands: "$(python -c '...')" puts the printed text into a shell variable. Both commands worked unchanged in PowerShell 7.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
The same quote mark inside and outside: python -c "print("Hello")" | The shell ends the text at the second ", so Python receives print(Hello): NameError: name 'Hello' is not defined | Use the other kind of quote inside: "print('Hello')" |
Writing a block, such as a for loop, after ;: "import sys; for i in range(2): print(i)" | SyntaxError: invalid syntax, pointing at for: a statement that opens a block can't follow ; | Put longer code in a .py file |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
NameError for a word that was meant to be text | The quotes around that text were eaten by the shell | Switch the inner quotes to the other kind |
| The wrong Python version answers | python found a different Python on your PATH | Run it through uv run, or call the Python you want by its full path |
FAQ
Can python -c read arguments?
Yes. Words after the code end up in sys.argv, where the documentation notes "the first element of sys.argv will be "-c"".
When should I write a script instead?
As soon as the code needs more than a couple of statements, or a block such as a loop or an if.
Conclusion
python -c "print('Hello from the command line')" ran Python without a file, and a second command chained two statements with ;. Mind the quotes: the inner ones must differ from the outer ones.
Additional Resources
- -c — the command line option.
