Installing Python and Setting Up Your Development Environment (Beginner Guide)

Technogic profile picture By Technogic
Thumbnail image for Installing Python and Setting Up Your Development Environment (Beginner Guide)

Introduction

Before you can start writing and executing Python code, you need to set up your development environment. This includes installing Python, configuring it correctly on your operating system, and choosing the tools that will help you write and manage your code more efficiently.

A well-configured environment makes coding smoother and helps avoid frustrating errors. Whether you're using Windows, macOS, or Linux, this guide will walk you through the essential steps needed to get Python up and running on your machine.

We'll also look at setting up a code editor (like Visual Studio Code), installing Python’s package manager (pip), and creating a virtual environment to manage dependencies. If you're interested in data science or scientific computing, we’ll also introduce you to the Anaconda distribution as an alternative Python setup.

By the end of this post, you'll be ready to write your first Python program and start your journey into the world of programming with confidence.

Checking for Pre-installed Python

Before installing Python, it's essential to check if it's already present on your system. Many operating systems come with Python pre-installed, especially Unix-based systems like macOS and Linux. Here's how you can verify Python's presence on your machine:

🪟 Windows

  1. Open Command Prompt:

    • Press Win + R, type cmd, and press Enter.

  2. Check for Python Installation:

    • Type the following command and press Enter:

      python --version
    • If Python is installed and added to your system's PATH, you'll see the version number, e.g., Python 3.11.3.

  3. Alternative Checks:

    • If the above command doesn't work, try:

      py --version

      or

      python3 --version
    • You can also check the installation path:

      where python

      This command will display the path(s) where Python is installed.

  4. Note:

    • If you receive a message like 'python' is not recognized as an internal or external command, Python is not installed or not added to the system's PATH.

🍎 macOS

  1. Open Terminal:

    • Press Cmd + Space, type Terminal, and press Enter.

  2. Check for Python 3 Installation:

    • Type the following command and press Enter:

      python3 --version
    • If Python 3 is installed, you'll see the version number, e.g., Python 3.9.6.

  3. Check for Python 2 Installation (if needed):

    • Type:

      python --version
    • Note: macOS Catalina and later versions do not include Python 2 by default.

  4. Verify Installation Path:

    • To see where Python 3 is installed, type:

      which python3

      This will display the path to the Python 3 executable.

  5. Note:

    • If you see a message like zsh: command not found: python3, Python 3 is not installed on your system.

🐧 Linux (Ubuntu/Debian)

  1. Open Terminal:

    • Use the shortcut Ctrl + Alt + T or search for "Terminal" in your applications menu.

  2. Check for Python 3 Installation:

    • Type the following command and press Enter:

      python3 --version
    • If Python 3 is installed, you'll see the version number, e.g., Python 3.8.10.

  3. Check for Python 2 Installation (if needed):

    • Type:

      python --version
    • Note: Many modern Linux distributions have deprecated Python 2.

  4. Verify Installation Path:

    • To find the path of the Python 3 executable, type:

      which python3

      This will display the installation path, e.g., /usr/bin/python3.

  5. Note:

    • If you receive a message like command not found, Python is not installed on your system.

By following these steps, you can determine whether Python is already installed on your system and identify its version. If Python is not installed, proceed to the next section to learn how to install it on your operating system.

Installing Python

Setting up Python correctly is the first step in your programming journey. This section provides step-by-step instructions to install Python on various operating systems.

🪟 Installing Python on Windows

Method 1: Using the Official Python Installer

  1. Download the Installer:

  2. Run the Installer:

    • Double-click the downloaded .exe file.

    • In the installer window:

      • Check the box that says "Add Python to PATH".

      • Click on "Install Now".

  3. Complete Installation:

    • Once the installation is complete, you can optionally click on "Disable path length limit" to avoid potential issues with long file paths.

    • Click "Close" to exit the installer.

  4. Verify Installation:

    • Open Command Prompt and type:

      python --version

      You should see the installed Python version displayed.

Method 2: Using Microsoft Store

  1. Open Microsoft Store:

    • Search for "Microsoft Store" in the Start menu and open it.

  2. Search for Python:

    • In the store's search bar, type "Python".

    • Select the latest version provided by the Python Software Foundation.

  3. Install Python:

    • Click "Get" to download and install Python.

  4. Verify Installation:

    • Open Command Prompt and type:

      python --version

      Confirm that the Python version is displayed.

🍎 Installing Python on macOS

Method 1: Using the Official Python Installer

  1. Download the Installer:

  2. Run the Installer:

    • Open the downloaded .pkg file.

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

  3. Verify Installation:

    • Open Terminal and type:

      python3 --version

      You should see the installed Python version.

Method 2: Using Homebrew

  1. Install Homebrew (if not already installed):

    • Open Terminal and run:

      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install Python:

    • After Homebrew installation, run:

      brew install python
  3. Verify Installation:

    • Check the Python version:

      python3 --version

      Confirm that the Python version is displayed.

🐧 Installing Python on Linux (Ubuntu/Debian)

Using APT Package Manager

  1. Update Package List:

    • Open Terminal and run:

      sudo apt update
  2. Install Python 3:

    • Run the following command:

      sudo apt install python3
  3. Verify Installation:

    • Check the Python version:

      python3 --version

      Confirm that the Python version is displayed.

By following these instructions, you should have Python installed and ready to use on your system. Next, we'll set up a code editor to write and run Python scripts efficiently.

Setting Up a Code Editor or IDE

To write and manage Python code efficiently, a robust code editor or Integrated Development Environment (IDE) is essential. Visual Studio Code (VS Code) stands out as a popular choice due to its versatility, extensive extension support, and user-friendly interface. Let's walk through the installation process for VS Code on various operating systems.

🪟 Installing Visual Studio Code on Windows

  1. Download the Installer:

  2. Run the Installer:

    • Locate the downloaded .exe file (e.g., VSCodeUserSetup-{version}.exe) and double-click to launch it.

  3. Installation Steps:

    • Accept the license agreement.

    • Choose the installation location or proceed with the default path.

    • Select additional tasks:

      • Add to PATH: Enables launching VS Code from the command line using code.

      • Create a desktop icon (optional).

    • Click "Install" to begin the installation process.

  4. Launch VS Code:

    • Once installation is complete, ensure the "Launch Visual Studio Code" option is checked and click "Finish".

  5. Verify Installation:

    • Open Command Prompt and type:

      code --version

      This should display the installed version of VS Code.

🍎 Installing Visual Studio Code on macOS

  1. Download the Installer:

  2. Install VS Code:

    • Once the download is complete, open the .zip file to extract the application.

    • Drag and drop the "Visual Studio Code" application into the "Applications" folder.

  3. Add VS Code to PATH (Optional):

    • Launch VS Code.

    • Press Cmd + Shift + P to open the Command Palette.

    • Type shell command and select "Shell Command: Install 'code' command in PATH".

    • This allows you to open VS Code from the terminal using the code command.

  4. Verify Installation:

    • Open Terminal and type:

      code --version

      You should see the installed version of VS Code.

🐧 Installing Visual Studio Code on Linux (Ubuntu/Debian)

  1. Download the .deb Package:

  2. Install VS Code:

    • Open Terminal and navigate to the directory containing the downloaded .deb file.

    • Run the following command to install:

      sudo apt install ./<file>.deb

      Replace <file> with the actual filename of the downloaded package.

  3. Launch VS Code:

    • After installation, you can start VS Code by typing code in the terminal or by searching for it in your applications menu.

  4. Verify Installation:

    • In Terminal, type:

      code --version

      This should display the installed version of VS Code.

🔌 Enhancing VS Code for Python Development

Once VS Code is installed, enhance it for Python development:

  1. Install the Python Extension:

    • Open VS Code.

    • Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or pressing Ctrl+Shift+X.

    • Search for "Python" and install the extension provided by Microsoft.

  2. Configure the Python Interpreter:

    • Press Ctrl+Shift+P to open the Command Palette.

    • Type Python: Select Interpreter and choose the appropriate Python interpreter for your project.

🧰 Alternative Code Editors and IDEs

While VS Code is a powerful and versatile editor, there are other options available for Python development:

  • PyCharm: A feature-rich IDE specifically designed for Python. It offers both free (Community) and paid (Professional) editions.

  • Jupyter Notebook: Ideal for data science and machine learning projects, allowing you to create and share documents that contain live code, equations, visualizations, and narrative text.

  • Sublime Text: A lightweight and fast text editor with a Python API for plugin development.

  • Atom: An open-source text editor developed by GitHub, customizable with a wide range of packages.

  • Thonny: A beginner-friendly Python IDE that comes with Python built-in, making it easy to get started.

Choose the editor or IDE that best fits your workflow and project requirements.

With your code editor set up, you're now ready to start writing and executing Python code. In the next section, we'll guide you through creating and activating a virtual environment.

Setting Up a Virtual Environment

A virtual environment in Python is an isolated workspace that allows you to manage project-specific dependencies without interfering with other projects or the global Python installation. This isolation ensures that each project can have its own set of packages, facilitating better dependency management and avoiding version conflicts.

Why Use a Virtual Environment?

  • Dependency Management: Different projects might require different versions of the same package. Virtual environments allow each project to maintain its own dependencies.

  • Avoiding Conflicts: By isolating packages, you prevent conflicts between packages required by different projects.

  • System Integrity: Installing packages globally can affect system tools and other projects. Virtual environments keep your global Python installation clean.

Creating a Virtual Environment

Python's built-in venv module (available in Python 3.3 and above) allows you to create virtual environments easily.

Steps:

  1. Navigate to Your Project Directory: Open your terminal or command prompt and navigate to the directory where you want to create your project.

  2. Create the Virtual Environment: Run the following command:

    python -m venv venv

    This command creates a directory named venv (you can name it anything) containing the virtual environment.

Activating the Virtual Environment

  • For Windows:

    In Command Prompt:

    venv\Scripts\activate

    In PowerShell:

    .\venv\Scripts\Activate.ps1
  • For macOS and Linux:

    In the terminal:

    source venv/bin/activate

After activation, your command prompt will change to indicate that you're working inside the virtual environment, typically showing (venv) at the beginning.

Installing Packages with pip

Once the virtual environment is activated, you can install packages using pip, Python's package installer.

For example, to install the requests library:

pip install requests

These packages will be installed only within the virtual environment, keeping your global Python installation unaffected.

Deactivating the Virtual Environment

When you're done working in the virtual environment, you can deactivate it by simply running:

deactivate

This command will return you to your system's global Python environment.

Managing Virtual Environments in Visual Studio Code

If you're using Visual Studio Code (VS Code) as your code editor, it provides excellent support for virtual environments.

Steps:

  1. Open Your Project Folder: Launch VS Code and open the folder containing your project.

  2. Open the Integrated Terminal: Go to View > Terminal or press Ctrl+` to open the terminal.

  3. Create and Activate the Virtual Environment: Create and activate the virtual environment by following the previous section.

  4. Select the Python Interpreter:

    • Press Ctrl+Shift+P to open the Command Palette.

    • Type Python: Select Interpreter and select the interpreter located in your venv folder.

VS Code will now use the virtual environment's interpreter for running and debugging your Python code.

By setting up a virtual environment, you ensure that your Python projects are self-contained and free from external interference, leading to more reliable and maintainable codebases.

Testing the Setup

To verify that your Python development environment is correctly configured in Visual Studio Code (VS Code), let's create and run a simple Python script named hello.py.

  1. Create the hello.py File

    1. Open VS Code and navigate to your project folder.

    2. Create a New File:

      • Click on the Explorer icon in the Activity Bar on the side of the window.

      • Click on "New File" and name it hello.py.

    3. Add the Following Code:

      print("Hello, World!")
    4. Save the File:

      • Press Ctrl+S (Windows/Linux) or Cmd+S (macOS) to save the file.

  2. Run the Script

    1. Run the Script:

      1. With hello.py open, click the Run button (▶️) in the top-right corner of the editor.

      2. Alternatively, press Ctrl+F5 (Windows/Linux) or Cmd+F5 (macOS).

    2. View the Output:

      • The Terminal panel will open at the bottom, displaying:

        Hello, World!

If you see this output, your Python environment is set up correctly in VS Code.

Congratulations! You've successfully tested your Python setup by running a simple script in Visual Studio Code.

Conclusion

Setting up your Python environment properly is the first crucial step in your programming journey. In this post, you’ve learned how to:

  • Check if Python is already installed on your system.

  • Download and install the latest version of Python on Windows, macOS, and Linux.

  • Set up a powerful and beginner-friendly code editor—Visual Studio Code.

  • Create and activate a virtual environment to isolate your project dependencies.

  • Run a simple hello.py script to test your setup.

With this solid foundation, you're now ready to start writing real Python code in a well-organized and efficient development environment. Let’s continue building your Python skills one step at a time!