Installing Python
Before you can write Python programs, you need a working Python environment. In this lesson, you will learn how to install Python, verify that it works, configure your development environment, and run your first Python file.
Learning objectives
By the end of this lesson, you should be able to:
- Install Python on your operating system.
- Check whether Python is installed correctly.
- Understand the difference between Python and pip.
- Configure Python in Visual Studio Code.
- Create and execute a Python file.
- Understand the purpose of PATH.
- Recognize and solve common installation problems.
Before Installing Python
Python is available for the major desktop operating systems, including Windows, macOS and Linux. The installation process is slightly different on each operating system, but the goal is the same:
You want your computer to be able to recognize the Python command from a terminal and execute Python programs.
What do you need?
- A computer running Windows, macOS or Linux.
- An internet connection for downloading Python.
- A code editor such as Visual Studio Code.
- Basic knowledge of opening a terminal.
You do not need an expensive computer to learn Python. Python is designed to run on a wide range of hardware.
Installing Python on Windows
Windows users can install Python using the official Python installer.
Step 1 — Download Python
Visit the official Python website and download the installer appropriate for your computer.
Step 2 — Start the installer
Open the downloaded installer. You will see an installation window with several options.
During installation, pay attention to the option that adds Python to your system PATH. Enabling this option makes it much easier to run Python from the terminal.
Step 3 — Complete installation
Continue through the installer and allow Python to finish installing.
Step 4 — Open Command Prompt
After installation, open Command Prompt or PowerShell and check the Python version.
python --version
If Python is installed and available through PATH, the terminal should display the installed Python version.
Installing Python on macOS
macOS includes some system software, but for learning modern Python development, it is better to install a current Python version explicitly.
Using the official installer
Download the appropriate Python installer for macOS and follow the installation instructions.
Verify the installation
Open Terminal and run:
python3 --version
On many macOS systems, the command
python3 is used to explicitly
refer to Python 3.
Installing Python on Linux
Many Linux distributions already include Python because various system tools depend on it.
However, the version available by default can vary between distributions.
Check Python first
python3 --version
If Python is not installed or the available version does not meet your project's requirements, use your Linux distribution's package manager or the official Python installation instructions.
Do not remove or replace the Python installation that your operating system may use internally without understanding the consequences.
Verify That Python Is Installed
Installing Python is only the first step. You should always verify that your terminal can find and execute it.
Windows
python --version
macOS / Linux
python3 --version
You can also start the Python interpreter directly.
python
On systems where Python 3 is invoked with
python3, use:
python3
You should see the Python interactive interpreter.
Python 3.x.x
>>>
The exact version number will depend on the Python version installed on your computer.
Python vs pip
Beginners often confuse Python and pip. They are related, but they serve different purposes.
| Tool | Purpose |
|---|---|
| Python | Runs Python programs. |
| pip | Installs and manages Python packages. |
Check pip
A common command is:
python -m pip --version
Using python -m pip helps make
it clear which Python installation is being
used to run pip.
A package is reusable Python software that can add functionality to your projects. We will study packages and pip in much greater detail later in the course.
Setting Up Python in Visual Studio Code
Visual Studio Code, commonly called VS Code, is a popular code editor for Python development.
Step 1 — Install VS Code
Download and install Visual Studio Code for your operating system.
Step 2 — Open VS Code
Launch VS Code after installation.
Step 3 — Install the Python extension
Open the Extensions panel and search for the official Python extension.
Install the Python extension and allow VS Code to activate Python development features.
Step 4 — Select the Python interpreter
VS Code may automatically detect your Python installation. If it does not, use the Python interpreter selection command and choose the Python installation you want your project to use.
Python can be installed correctly while VS Code is configured to use a different interpreter. Always check the selected interpreter when a program behaves unexpectedly.
Run Your First Python File
Now let's create an actual Python program.
Step 1 — Create a project folder
Create a folder somewhere convenient, for example:
PythonPractice
Step 2 — Open the folder in VS Code
Open the folder using VS Code.
Step 3 — Create a Python file
Create a new file called:
hello.py
Step 4 — Write Python code
print("Hello, Python!")
Step 5 — Run the program
Open the VS Code terminal and run:
python hello.py
Hello, Python!
You have just created and executed a Python program. From this point onward, your learning journey will focus on understanding what the code actually does.
Understanding PATH
PATH is an environment variable used by your operating system to locate executable programs.
When you type:
python --version
your operating system needs to know where the Python executable is located.
If the Python installation directory is available through PATH, the operating system can find Python without you typing its full file location.
What happens when PATH is missing?
You may see an error saying that Python is not recognized as a command, even though Python may actually be installed.
A PATH-related error does not necessarily mean that Python is completely missing. It can simply mean that your terminal cannot locate the Python executable.
Common Installation Problems
Problem 1 — Python command not recognized
If the terminal cannot find Python, first check that Python is actually installed. Then check whether the correct installation is available through PATH.
Problem 2 — Wrong Python version
Your computer may have more than one Python installation. Check which interpreter your terminal and VS Code are using.
Problem 3 — pip is not recognized
Instead of relying on a standalone
pip command, try:
python -m pip --version
Problem 4 — VS Code runs the wrong interpreter
Open the Python interpreter selector in VS Code and choose the interpreter associated with your project.
Problem 5 — Terminal still shows old information
After changing installation or environment settings, close and reopen the terminal. In some cases, restarting VS Code or opening a new terminal session is necessary.
Practice: Set Up Your Python Environment
Now it is your turn. Complete the following tasks on your own computer.
Check Python
Open your terminal and find out whether Python is installed.
python --version
Check pip
Find the installed pip version.
python -m pip --version
Create hello.py
Create a file named
hello.py and write a program
that prints your name.
For example:
print("My name is Adnan")
Create a Python setup report
Find and record:
- Your Python version
- Your pip version
- Your operating system
- Your VS Code version
This simple habit will become useful when working with real-world Python projects.
Quick Knowledge Check
Test your understanding before moving to the next lesson.
1. What is the main purpose of Python?
2. What is pip mainly used for?
3. What does PATH help the operating system do?
4. Which file extension is commonly used for Python source files?
5. What should you check if VS Code is running the wrong Python environment?
Lesson Summary
You now understand the basic process of preparing a computer for Python development.
.py extension.
Your Python environment is now ready. In the next lesson, we will write and understand our first real Python programs.
Finished Lesson 02?
Mark this lesson complete when you have finished the reading, practice and quiz.