In this beginner Python tutorial, you will learn about the 3 ways that you can use to run Python.
Along the way, you will learn about:
- How to Use Python Interpreters
- How to Run Python in the Command line
- How to Use Integrated Development Environments (IDEs)
- Python Scripts and Modules
- Python Notebooks
Installing Python
In order to use Python, you will need to install the Python Interpreter.
What is the Python Interpreter
The Python interpreter is the software that you need to use in order to Python code and scripts.
To install Python, read one of the following articles:
- Install Python on MacOS
- Install Python on Windows
- Install Python with Anaconda
What are the 3 Ways to Run Python
The three options to run Python code are:
- Run Python in the Command line (Terminal)
- Run Python in an IDE (Integrated Development Environment)
- Run Python in a Python Notebook
1. Run Python with the Terminal (Command-Line)
The first way that you can run Python is using the Terminal. To run Python in the Terminal, or the Command-Line, open your shell.
Check if Python is installed
$ which python3
Open Python
Open the Python Interactive Window using the python3
keyword. Sometimes, the installer is under the python
alias.
$ python3
Run Python
>>> print('hello world')
Exit the Python Interactive Window
Either type Command + Shift + D (on Mac) or type in exit() on Windows.
![run python in the interactive shell](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-1024x419.png)
2. Run Python in an Integrated Development Environment (IDE)
The second way that you can execute Python code is from an Integrated Development Environment.
What is an Integrated Development Environment?
An Integrated Development Environment, also known as an IDE, is a software that gives you the tools to improve the programming workflow.
Some IDEs are Python Specific such as JetBrains’ PyCharm (paid), others can be used with most programming languages such as VS Code (free).
Why Use an IDE?
Integrated development environments are one of the ways that web developers can use to run code. They provide things such as a file management interface, access to a Terminal, run code, along with many other web development features:
- Syntax Highlighting
- Autocomplete
- Debugging
- Terminal
- Git version Control
Run Python in IDE’s Terminal
In VS Code, open a new Terminal using Control + Shift + `
or by selecting Terminal
> New Terminal
in the menu.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-2.png)
From the new Terminal window that just opened, you can run Python just as before.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-1.png)
Run Python in IDE’s Interactive Window
VS Code has a Python interactive window that you can access using Shift + Enter. Alternatively, you can right-click on the line that you want to run and go to Run in interactive Window
and select Run From Line in Interactive Window
.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-8.png)
The Python Interactive Window will open and run whatever line you wanted to execute.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-9-1024x560.png)
If this does not work, make sure that you have installed the Python plugins for VS Code.
3. Run Python in a Python Notebook
The third way that you can execute Python code is from a Python Notebook.
What is a Python Notebook?
A Python Jupyter Notebook is an open-source environment that lets you write, read and share your Python code for data analysis. It provides additional capacity such as run code line-by-line and commenting code using the Markdown syntax.
Some examples of Python notebooks are browser-based such a using Google Colab with Python or using a local version of a Jupyter Notebook.
How to use Notebooks
To use a Python notebook, you have to install Python notebook extension on VS Code or use something such as Google Colab.
You create notebook by using the ipynb extension.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-12-1024x343.png)
Python notebooks have a different file extension than Python files:
- Python file (.py)
- Python Notebooks (.ipynb)
In Visual Studio Code, you can create a code block by clicking on + Code
.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-11-1024x337.png)
And then you can execute the block of code using the play button next to the block.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-13.png)
Only the block, not the entire file will have run.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-10-1024x375.png)
On top of that you can create description for your code by clicking on the + Markdown
button and use the markdown syntax to comment your code.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-14.png)
Python Scripts and Modules
Python scripts and modules are both Python files with the .py extensions that are waiting to be executed;
Difference Between a Python Script and a Module
- Python script: Python file that you run Directly
- Python module: Python file that you import to be used in another Python file
How to Run a Python Script
To run a Python script, you need to use the python keyword with the location of the file to be executed.
$ python3 path/to/file.py
Let’s take this simple Python file.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-3.png)
To execute the Python file in the Terminal, or in the shell of your IDE, you can type the python3 filename.py
command.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-5-1024x444.png)
How to Run a Python Module
To run a Python module, you need to import the module from another Python file and execute that file.
Take this file for example.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-6-1024x272.png)
While we called the hello.py
file a script earlier, it is now a called a module when I import it from module.py
.
You can run this in a similar matter as earlier.
# python3 module.py
And you will see that the the print(‘hello world’) function was execute, but this time as a module.
![](https://www.jcchouinard.com/wp-content/uploads/2023/05/image-7-1024x446.png)
Conclusion
This is it, you now know the 3 ways that you can use to run Python code: using the Terminal, using an IDE or using a Python Interactive Window.
![jean-christophe chouinard seo expert in quebec city](https://www.jcchouinard.com/wp-content/uploads/2019/11/jean-christophe-chouinard-seo-expert-quebec.jpg)
SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. Writer in Python, Information Retrieval, SEO and machine learning. Guest author at SearchEngineJournal, SearchEngineLand and OnCrawl.