How to execute Python on your computer

Python has become a powerhouse in the programming world. Python ranks among the top languages due to its versatility and ease of use. Developers love Python for data analysis, machine learning, and web development. Python's popularity continues to rise, with many professionals adopting the latest versions.

The goal of this blog is to guide you through running Python on your computer. Whether you are a beginner or looking to refresh your skills, this guide will help you get started with Python quickly and efficiently.

Installing Python

Downloading Python

Where to download Python

To get started with Python, visit the official website at python.org. The site offers the latest versions for download. The homepage features a prominent "Downloads" button. Click on this button to access the download page.

Choosing the right version

Choosing the correct version of Python is crucial. The website provides both Python 3 and Python 2. Opt for Python 3 as it includes the latest features and improvements. Avoid using Python 2 unless a specific project requires it. Check the system requirements to ensure compatibility with your operating system.

Installing Python

Installation steps for Windows

  1. Download the Python installer for Windows from the official website.

  2. Run the downloaded installer file.

  3. Select "Add Python to PATH" before clicking "Install Now."

  4. Follow the on-screen instructions to complete the installation.

  5. Open Command Prompt and type python to verify the installation.

Installation steps for macOS

  1. Download the Python installer for macOS from the official website.

  2. Open the downloaded .pkg file.

  3. Follow the installation wizard steps.

  4. Open Terminal and type python3 to verify the installation.

  5. If necessary, install additional tools like Homebrew for easier package management.

Installation steps for Linux

  1. Open Terminal.

  2. Update the package list by typing sudo apt update.

  3. Install Python by typing sudo apt install python3.

  4. Verify the installation by typing python3 --version.

  5. For other distributions, refer to the specific package manager instructions.

Setting Up Python Environment

Configuring Environment Variables

Setting PATH on Windows

To run Python from any directory, you need to set the PATH variable. Follow these steps:

  1. Open the Start menu and search for "Environment Variables."

  2. Click on "Edit the system environment variables."

  3. In the System Properties window, click on "Environment Variables."

  4. Under "System variables," find the "Path" variable and select it.

  5. Click "Edit" and then "New."

  6. Add the path to your Python installation directory (e.g., C:\Python39).

  7. Click "OK" to save and close all windows.

  8. Open Command Prompt and type python to verify the setup.

Setting PATH on macOS

Setting the PATH variable on macOS involves editing the shell profile:

  1. Open Terminal.

  2. Type nano ~/.bash_profile to open the profile in the nano editor.

  3. Add the line export PATH="/usr/local/bin/python3:$PATH".

  4. Press Control + X to exit, then press Y to save changes.

  5. Type source ~/.bash_profile to apply the changes.

  6. Verify by typing python3 in Terminal.

Setting PATH on Linux

Linux users can set the PATH variable by editing the .bashrc file:

  1. Open Terminal.

  2. Type nano ~/.bashrc to open the file in the nano editor.

  3. Add the line export PATH="/usr/bin/python3:$PATH".

  4. Press Control + X to exit, then press Y to save changes.

  5. Type source ~/.bashrc to apply the changes.

  6. Verify by typing python3 in Terminal.

Installing an Integrated Development Environment (IDE)

Popular IDEs for Python

Choosing a good IDE can enhance your coding experience. Here are some popular options:

  • PyCharm: Known for its powerful features and user-friendly interface.

  • VS Code: Lightweight and highly customizable.

  • Jupyter Notebook: Ideal for data analysis and machine learning projects.

  • Spyder: Great for scientific computing.

Installation steps for PyCharm

Follow these steps to install PyCharm:

  1. Visit the PyCharm website.

  2. Choose the Community edition for free use.

  3. Download the installer for your operating system.

  4. Run the installer and follow the on-screen instructions.

  5. Open PyCharm and configure the Python interpreter.

Installation steps for VS Code

To install VS Code, follow these steps:

  1. Visit the VS Code website.

  2. Download the installer for your operating system.

  3. Run the installer and follow the on-screen instructions.

  4. Open VS Code and go to the Extensions view.

  5. Search for "Python" and install the official Python extension.

  6. Configure the Python interpreter in VS Code settings.

Writing and Running Your First Python Script

Writing a Simple Python Script

Using a text editor

Start by opening a simple text editor like Notepad on Windows or TextEdit on macOS. Create a new file and type the following code:

print("Hello, World!")

Save the file with a .py extension, such as hello.py. This file contains your first Python script.

Using an IDE

Open your chosen Integrated Development Environment (IDE), such as PyCharm or VS Code. Create a new project or file within the IDE. Type the same code:

print("Hello, World!")

Save the file with a .py extension. The IDE will provide features like syntax highlighting and error checking, making the coding process smoother.

Running the Python Script

Running from the command line

To run your Python script from the command line, follow these steps:

  1. Open Command Prompt on Windows, Terminal on macOS, or a terminal emulator on Linux.

  2. Navigate to the directory where you saved your script using the cd command. For example:

    cd path\to\your\script
  3. Type the following command and press Enter:

    python hello.py

You should see the output Hello, World! printed on the screen.

Running from an IDE

Running your Python script from an IDE is straightforward. Open the script file in your IDE. Look for a "Run" button or option in the menu. Click it to execute the script. The output Hello, World! will appear in the IDE's built-in console.

Recap the journey of installing Python, setting up the environment, and running your first script. You have taken significant steps toward mastering Python. Now, dive deeper into Python's vast resources.

For further learning, visit:

Happy coding!