How To Set Up Python On Windows, Linux, Or Mac, and Run Your First Python Program?

Share this

Here are the steps on how to set up Python on Windows, Linux, or Macbook, and run your first Python program.

1. Windows

Step 1: Download Python

Go to the official Python website: https://www.python.org/downloads/The website usually detects your operating system and presents the appropriate download link. Click on the Download Python (version) button (e.g., Python 3.x.x).

Step 2: Run the Installer

  • After downloading, open the installer (python-x.x.x.exe).
  • IMPORTANT: Check the box that says “Add Python to PATH” at the bottom of the setup screen.
  • Click Install Now (this installs Python with default settings).

Step3: Verify Installation

  • Open Command Prompt (search for cmd).
  • Type: python --version
  • This should show the installed Python version (e.g., Python 3.x.x).

Step 4: Install pip (Optional)

  • Pip (Python package installer) is usually installed with Python. To verify: run
    pip --version
  • If pip is not installed, use this command to install it:
    python -m ensurepip --upgrade

2. macOS

Step 1: Check Default Python Version

  • macOS comes with Python 2.x pre-installed. To check the installed version, open Terminal, and type:
    python --version
  • If you see Python 2.x and you want Python 3, follow the next steps.

Step 2: Install Homebrew (if not already installed)

  • Homebrew is a package manager for macOS, which simplifies the installation of software. Open Terminal and type:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • Follow the on-screen instructions.

Step 3: Install Python using Homebrew

  • Once Homebrew is installed, use this command in Terminal to install Python 3:
    brew install python

Step 4: Verify Installation

  • Check if Python 3 is installed correctly:
    python3 --version
  • You can also check if pip3 (Python package installer) is installed:
    pip3 --version

3. Linux (Ubuntu/Debian)

Step 1: Update Package List

  • Open Terminal and run the following commands:
    sudo apt update
    sudo apt upgrade

Step 2: Install Python

  • To install Python 3, use the following command:
    sudo apt install python3

Step 3: Verify Installation

  • Once installed, verify the Python installation by typing:
    python3 --version

Step 4: Install pip

  • If pip is not installed by default, install it using:
    sudo apt install python3-pip

General Post-Installation Steps (All OS)

Set Up a Virtual Environment (Optional)

  • It’s good practice to use virtual environments for Python projects. To set it up run:
    python3 -m venv myenv
  • Activate the environment:
    • For Windows:
      myenv\Scripts\activate
    • On macOS/Linux:
      source myenv/bin/activate

Write and execute the First Python Program

  • Create a new text file. Save the file with a .py extension.
  • Write your Python code. You can start by writing a simple print statement, such as:
print("Hello, world!")
  • Save the file and run it. To run your Python program, open a command prompt or terminal window and navigate to the directory where you saved the file. Then, type the following command:
python filename.py
or
python3 filename.py
or
py filename.py

This will run your Python program and print “Hello, world!” to the console. Congratulations, you just did the Python setup in your local development environment and ran your first Python program. (y)

Further Readings

Check out our other blogs on Python Basics in this tutorial series.

Feel free to share your thoughts on this topic in the comments section below 👇 We would be happy to hear and discuss the same 🙂

Share this

Leave a comment

Your email address will not be published. Required fields are marked *