Executing Python Code in the Shell
Executing Python code in the shell, also known as the command line or terminal, is a fundamental skill for Python developers. The shell provides a powerful interactive environment where you can write, test, and debug your Python code directly, without the need for a separate Python script file.
Using the Python Interpreter
The most straightforward way to execute Python code in the shell is by using the Python interpreter. The Python interpreter is a program that reads and executes Python code line by line. To start the Python interpreter, simply type python
(or python3
if you have multiple versions of Python installed) in the shell and press Enter.
$ python
Python 3.9.7 (default, Sep 16 2021, 16:59:28)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Once the Python interpreter is running, you can start typing your Python code directly. The >>>
prompt indicates that you are in the interactive Python shell, and you can execute any valid Python statement or expression.
>>> print("Hello, World!")
Hello, World!
>>> x = 5
>>> y = 10
>>> print(x + y)
15
To exit the Python interpreter, simply type exit()
or press Ctrl + D
.
Running Python Scripts
In addition to the interactive Python shell, you can also execute Python scripts directly from the shell. To do this, create a Python script file (e.g., my_script.py
) and add your Python code to it. Then, you can run the script by typing python my_script.py
in the shell.
# my_script.py
print("This is a Python script.")
print("The sum of 5 and 10 is:", 5 + 10)
$ python my_script.py
This is a Python script.
The sum of 5 and 10 is: 15
If your Python script has a #!/usr/bin/env python
shebang line at the beginning, you can also make the script executable and run it directly, without the python
command.
$ chmod +x my_script.py
$ ./my_script.py
This is a Python script.
The sum of 5 and 10 is: 15
Using the Python REPL
The Python REPL (Read-Eval-Print Loop) is another way to execute Python code in the shell. The REPL is a more advanced interactive environment that provides additional features, such as tab completion, history, and access to the system environment.
To start the Python REPL, type python -i
in the shell. This will start the Python interpreter and enter the REPL mode.
$ python -i
Python 3.9.7 (default, Sep 16 2021, 16:59:28)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 5
>>> y = 10
>>> print(x + y)
15
>>> import os
>>> os.getcwd()
'/home/user'
>>>
In the Python REPL, you can execute Python code, import modules, and interact with the system environment, all within the interactive shell.
Visualizing the Workflow
Here's a Mermaid diagram that illustrates the different ways to execute Python code in the shell:
In summary, executing Python code in the shell is a versatile and powerful technique that allows you to quickly test, debug, and experiment with your Python code. Whether you use the interactive Python interpreter, run Python scripts, or leverage the Python REPL, mastering these shell-based Python execution methods will greatly enhance your productivity as a Python developer.