There are a few different ways to run Python programs.
In particular, you have a distinction between using interactive prompts, where you type Python code and it’s immediately executed, and saving a Python program into a file, and executing that.
Let’s start with interactive prompts.
If you open your terminal and type
python, you will see a screen like this:
This is the Python REPL (Read-Evaluate-Print-Loop)
>>> symbol, and the cursor after that. You can type any Python code here, and press the
enter key to run it.
For example try defining a new variable using
name = "Flavio"
and then print its value, using
Note: in the REPL, you can also just type
name, press the
enterkey and you’ll get the value back. But in a program, you are not going to see any output if you do so - you need to use
Any line of Python you write here is going to be executed immediately.
quit() to exit this Python REPL.
You can access the same interactive prompt using the IDLE application that’s installed by Python automatically:
This might be more convenient for you because with the mouse you can move around, copy/paste more easily than with the terminal.
Those are the basics that come with Python by default. However I recommend to install IPython, probably the best command line REPL application you can find.
Install it with
pip install ipython
Make sure the pip binaries are in your path, then run
ipython is another interface to work with a Python REPL, and provides some nice features like syntax highlighting, code completion, and much more.
The second way to run a Python program is to write your a Python program code into a file, for example
and then run it with
Note that we save Python programs with the
.pyextension, that’s a convention.
In this case the program is executed as a whole, not one line at a time. And that’s typically how we run programs.
We use the REPL for quick prototyping and for learning.
On Linux and macOS a Python program can also be transformed into a shell script, by prepending all its content with the a special line that indicates which executable to use to run it.
On my system the Python executable is located in
/usr/bin/python3, so I type
#!/usr/bin/python3 in the first line:
Then I can set execution permission on the file:
chmod u+x program.py
and I can run the program with
This is especially useful when you write scripts that interact with the terminal and in general little utilities.
We have many other ways to run Python programs.
One of them is using VS Code, and in particular the official Python extension from Microsoft:
After installing this extension you will have Python code autocompletion and error checking, automatic formatting and code linting with
pylint, and some special commands, including:
Python: Start REPL to run the REPL in the integrated terminal
Python: Run Python File in Terminal to run the current file in the terminal.
Python: Run Current File in Python Interactive Window:
and many more. Just open the command palette (View -> Command Palette, or Cmd-Shift-P) and type
python to see all the Python-related commands:
Another way to easily run Python code is to use repl.it, a very nice website that provides a coding environment you can create and run your apps on, in any language, Python included:
Signup (it’s free), then under “create a repl” click Python:
and you will be immediately shown an editor with a
main.py file, ready to be filled with a lot of Python code:
Once you have some code, click “Run” to run it on the right side of the window:
I think repl.it is handy because you can easily share code just by sharing the link, multiple people can work on the same code, and you can create long-running programs directly here, for free, you can install packages and it provides you even a key-value database for more complex applications.
More python tutorials:
- Introduction to Python
- Installing Python 3 on macOS
- Running Python programs
- Python 2 vs Python 3
- The basics of working with Python
- Python Data Types
- Python Operators
- Python Strings
- Python Booleans
- Python Numbers
- Python, Accepting Input
- Python Control Statements
- Python Lists
- Python Tuples
- Python Sets
- Python Dictionaries
- Python Functions
- Python Objects
- Python Loops
- Python Modules
- Python Classes
- The Python Standard Library
- Debugging Python
- Python variables scope
- Python, accept arguments from command line
- Python Recursion
- Python Nested Functions
- Python Lambda Functions
- Python Closures
- Python Virtual Environments
- Use a GoPro as a remote webcam using Python
- Python, how to create a list from a string
- Python Decorators
- Python Docstrings
- Python Introspection
- Python Annotations
- Python, how to list files and folders in a directory
- Python, how to check if a number is odd or even
- Python, how to get the details of a file
- Python, how to check if a file or directory exists
- Python Exceptions