Blog > Python Programming > How to Install Python on Windows, Mac and Linux

How to Install Python on Windows, Mac and Linux

by | Apr 25, 2024

Overview

Python, developed by Guido van Rossum and released in 1991, has rapidly ascended to become one of the most favoured programming languages globally. Its versatility allows it to be applied in an array of fields, from machine learning and web development to software testing.

As a general-purpose language, Python is designed for various practical applications, making it an indispensable tool in data science, automation, and beyond. Its user-friendly nature also makes it accessible to developers and non-developers alike, solidifying its position as a critical skill in the tech industry.

If you’re embarking on a basic Python course or looking to acquire a machine learning certification, and wondering how do I install Python, follow this step-by-step guide.

Pre-installation Checklist

Before diving into Python installation on your operating system, it’s essential to ensure your system meets the necessary software and hardware requirements. Here’s a concise guide to what you’ll need:

  • Operating System Compatibility: Confirm that your version of the operating system (OS) is supported. Python can be installed on Windows, macOS, and Linux, but specific versions of Python may require updated OS versions.
  • Administrator Access: You may need administrator privileges to install Python, especially on Windows and macOS. Ensure you have the necessary permissions or credentials.
  • Sufficient Disk Space: While Python itself isn’t overly large, ensure you have ample free disk space for the installation and future projects.
  • Internet Connection: You will need a stable and active internet connection to download Python from the official website and to install additional packages or libraries later on.

Ensure these prerequisites are met to facilitate a smooth installation process and prepare your system for diving into Python programming.

How To Install Python On Windows

Here’s a detailed guide on installing Python on Windows:

Step 1: Download the Installer

  1. Visit the Python website: Use your web browser and go to the official Python website.
  2. Navigate to downloads: Hover over or click on the “Downloads” tab. The website should automatically recognise your operating system and offer the latest Python version for Windows.
  3. Download the executable installer: Click on the “Download Python (version)” button. The version number will change over time as newer versions are released.

Step 2: Run the Installer

  1. Locate the executable installer: Find the installer in your “Downloads” folder. The file name should be something like python-3.x.x.exe (“x.x” represents the version number).
  2. Begin the installation: Double-click the installer to start the installation process.

Step 3: Add Python to PATH

(Optional) Select install location: You can change the install location by clicking on “Customise installation”. However, for most users, the default install location is suitable.

  1. Add Python to PATH: Before proceeding with the installation, make sure to check the box at the bottom of the installer window that says “Add python 3.x.x to PATH”. This option makes it easier to run Python from the command line.
  2. Continue the installation: Click on “Install Now” to start the installation with the default settings. If you choose to customise the installation, follow the prompts and then click on “Install”.

Step 4: Wait for the Installation to Be Completed

The installation duration will be based on your system’s capabilities. You’ll be able to see the progress on the installer window.

Upon completion, a message indicating “Setup was successful” will appear. At this point, you will also have the option to “Disable path length limit,” which may prove advantageous for specific Python programs.

Step 5: Verify the Installation

  1. Open the Command Prompt: Press “Win + R”, type “cmd”, and press Enter.
  2. Check Python version: Type “python-3.x.x” (with “x.x” being the number of the version installed) and press Enter. This command should return the Python version you have installed, confirming that Python is correctly installed and accessible from the command line.
  3. Check Pip installation: Type “pip-3.x.x” (with “x.x” being the version number) and press Enter. This command confirms that pip, Python’s package installer, is also ready for use.

Updating Python

When it’s time to upgrade to a more recent version of Python, simply obtain the newest installer from the Python website and proceed with the same installation process. The installer is built to manage updates seamlessly.

How To Install Python On Mac

Selected macOS may come with Python pre-installed. However, it is usually an older and possibly outdated version hence it is best to check its version. Here’s how you can verify which version is installed:
  1. Open Terminal: Open your Finder, navigate to Applications and open the Utilities folder. In this folder, locate Terminal or search for it using Spotlight (Cmd + Space).
  2. Search for Python in Terminal: Type “python –version” or “python3 –version” and press Enter. If Python is installed, a message like “python 3.x.x” followed by the Python prompt will appear. It will look like “>>>”. “3.x.x” with “x.x” being the version number.
If you’re content with your current Python version, no further action is required, but it’s advisable to use the latest version for enhanced security, access to new features, and better performance. To download the newest version, simply follow these steps:

Step 1: Download the Installer

  1. Visit the Python website: Use your web browser and go to the official Python website.
  2. Navigate to downloads: Hover over or click on the “Downloads” tab. The website should automatically recognise your operating system and offer the latest Python version for your macOS.
  3. Download the executable installer: Click on the “Download Python (version)” button to get the .pkg installer file. The version number will change over time as newer versions are released.

Step 2: Run the Installer

  1. Open the installer: Locate the .pkg file and double-click it to begin the installation.
  2. Follow the installation wizard: The installer will guide you through the necessary steps. Click “Continue” and read through the information provided, including the software license agreement.
  3. (Optional) Choose the install location: By default, Python will be installed on your system’s main disk. You can change this, but for most users, this is not necessary.
  4. Complete the installation: Click “Install”, enter your admin password if prompted, and allow the installer to complete the process.

Step 3: Verify the Installation

  1. Check if the installation is successful: Open Terminal again and type “python –version” or “python3 –version” and press Enter. If Python is installed, a message like “python 3.x.x” followed by the Python prompt will appear. It will look like “>>>”. “3.x.x” with “x.x” being the version number.
  2. Ensure pip is installed: In Terminal, type “pip-3.x.x” (with “x.x” being the version number). This is Python’s package installer and should be installed automatically with Python 3.4 and later.

Step 4: Set up Path (If Required)

For Python 3.6 and later, the installer provides an option to add Python to your PATH automatically. However, if you’re using an earlier version, or if Python is not recognised in Terminal, you may need to add it to your PATH manually by following these steps:

  1. In Terminal, enter the following: “Edit ~/.bash_profile” or “~/.zshrc” (for newer macOS versions)
  2. Add the following line: “export PATH=”/usr/local/bin/python3:$PATH” to the file.
  3. Save the file and restart Terminal.

How To Install Python On Linux

Similar to macOS, Python comes pre-installed on most Linux distributions. You can check which version is on your machine by following these steps:

  1. Open Terminal: Press Ctrl+Alt+T to open a terminal window.
  2. Search for Python in Terminal: Type “python –version” or “python3 –version” and press Enter. If Python is installed, Python 2.x or Python 3.x will be displayed with “x” being the version number.

If you are satisfied with the existing version of Python, further steps aren’t necessary, but updating to the latest version is recommended for improved security, new functionalities, and enhanced efficiency. How to install Python will depend on your Linux distribution. However, here are instructions for the two most common package management systems:

Installing Python on Linux Debian, Ubuntu, and derivatives:

  1. Update package list: Run sudo apt update to update the list of available packages from the repositories.
  2. Install Python 3: Run sudo apt install python3. This command installs Python 3 along with its dependencies. If you need pip (Python’s package installer), you can install it by running sudo apt install python3-pip.

Installing Python on Linux Fedora:

Use the dnf package manager by running sudo dnf install python3. This command installs Python 3. For pip, execute sudo dnf install python3-pip.

Installing Python on Linux CentOS:

CentOS 7 and below might not have the dnf package manager by default, so you have to use yum instead. Run sudo yum install python3 to install Python 3. Install pip by running sudo yum install python3-pip.

Verifying the Installation on Linux

After installation, verify that Python is correctly installed by running python3 –version in the terminal. This command should return the installed Python version. Similarly, you can check that pip is installed by running pip3 –version.

Installing Pip

Pip is the package installer for Python. Ensuring it is correctly installed and updated is essential as it enables the installation and management of extra libraries and dependencies not included in the standard library.

If you have installed Python 3.4 or newer versions, pip should have been installed by default. To check if it is installed, you can open a terminal window for macOS or command prompt for Windows and run:

  1. “pip –version”
  2. “pip3 –version” (for Python 3.x)

If pip is not installed, or you’re using an older Python version, here’s how to install pip:

  1. Download get-pip.py by visiting the link: https://bootstrap.pypa.io/get-pip.py
  2. Open a terminal or command prompt.
  3. Navigate to the directory where get-pip.py was downloaded.
  4. Run the script with Python: “python get-pip.py” or “python3 get-pip.py”.

Upgrading Pip

To upgrade an existing pip installation to the latest version, run “pip install –upgrade pip”. For Python 3.x, you can use “pip3 install –upgrade pip”.

Setting Up A Virtual Environment

A virtual environment in Python is a self-contained directory that enables the management of dependencies specific to a project, ensuring that each project has access to only its necessary libraries. This is crucial for avoiding conflicts between project requirements and maintaining a clean development workspace.

How to Create a Virtual Environment

To create a virtual environment, you’ll need the venv module that comes with Python 3.3 and later. Here’s how to set one up:

  1. Open a terminal or command prompt.
  2. Navigate to your project’s directory.
  3. Run the following command: “python -m venv myprojectenv”*.
  4. Replace “myprojectenv” with the name of your virtual environment. This command creates a myprojectenv directory within your project, containing the virtual environment.

*Note: If you are running Python 3.x, ensure you’re using python3 in the command.

How to Activate a Virtual Environment

Before you can start using the virtual environment, you need to activate it. Activation scripts are located in the bin directory (or Scripts on Windows) inside the env directory.

  1. On Unix or macOS, run: “source myprojectenv/bin/activate”.
  2. On Windows, run: “myprojectenv\Scripts\activate”.

Once activated, your terminal prompt might change to show the name of the activated environment. While activated, any Python or pip commands will use the versions in the virtual environment, not the global ones.

How to Deactivate a Virtual Environment

To stop using a virtual environment and go back to the global Python, you simply run: “deactivate”. This command deactivates the environment, and you’ll return to using your system’s global Python setup.

How to Manage Dependencies

With an activated virtual environment, you can use pip to install packages locally within the environment. For example, by running “pip install requests”, you can install the requests library in the current virtual environment, isolated from the global Python environment.

Troubleshooting Common Issues With Python Installation

While installing Python on all operating systems is generally a straightforward process, you may encounter common issues such as:

1. Python Is Not Recognised as an Internal or External Command

This usually indicates that Python’s executable file is not in your system’s PATH environment variable and is specific to Windows. To resolve this, you can try these steps:

  1. Locate the folder with the installed Python version as its name in X:\Program Files (where X is the drive where Windows is installed; e.g.,C:\Program Files\Python36). If such a folder does not exist, download and install the latest version of Python.
  2. Open this folder and copy its path.
  3. Right-click on “This PC”, then go to Properties > Advanced system settings > Environment variables.
  4. In the window that appears when a PATH variable exists, select it, and click “Edit”; otherwise, click “New”.
  5. In the next dialogue box, click on “New” and paste the previously copied path of the folder; then, click “OK”.

2. Python Command Not Found

This issue is similar to the one above. However, this error message typically appears on Unix-based operating systems like Linux or macOS. In this case, the root issue also lies in your operating system being unable to locate the python command, because the location of its binary file is not defined in the system PATH.

To resolve this, you can simply use your package manager to install the latest version of Python and then try running the command again. These operating systems will typically install Python under “/usr/bin” or “/usr/local/bin” by default.

3. Permission Denied Error During Installation

This error typically occurs on macOS and Linux when you don’t have adequate permissions to install Python in the desired directory. Use the sudo command to run your install command with administrative privileges, e.g., “sudo apt-get install python3” on Ubuntu.

On macOS, ensure you’re installing Python from the official Python website to avoid permission issues with the system’s Python version.

Begin Your Python Journey

Congratulations! Python has been successfully installed on your machine. You’re all set to embark on your programming adventure and uncover the transformative power of Python. This step marks the beginning of a journey that could even lead to achieving a Python certification, opening up a world of opportunities in the ever-evolving tech industry.

Upskill Today With Heicoders Academy

Secure your spot in our next cohort! Limited seats available.