1.4 Installing Python and Setting Up Environment

Setting up Python on your system is the first step towards becoming proficient in the language. This tutorial will guide you through the process of installing Python on various operating systems, setting up a development environment, and ensuring everything is configured correctly.


1.4.1 Installing Python

Python can be installed on Windows, macOS, and Linux. Here's a step-by-step guide for each platform:


1.4.1.1 Installing Python on Windows

  1. Download Python:
    • Visit the official Python website.
    • Click the "Download Python" button, which will download the latest stable version (usually Python 3.x).
  2. Run the Installer:
    • Once the installer is downloaded, open it.
    • Make sure to check the box that says "Add Python to PATH" (important for using Python from the command line).
    • Click "Install Now".
  3. Verify Installation:
    • Open the command prompt (Press Win + R, type cmd, and press Enter).
    • You should see the Python version displayed (e.g., Python 3.x.x).

Type the following command to check if Python was installed correctly:

python --version

1.4.1.2 Installing Python on macOS

  1. Check for Pre-Installed Python:
    • To use Python 3, you will need to install it separately.
  2. Install Homebrew (Optional):
    • Homebrew is a popular package manager for macOS that makes installing software easier.
  3. Download and Install Python:
    • Alternatively, you can download the latest Python version from the official Python website.
    • Run the installer and follow the on-screen instructions.
  4. Verify Installation:
    • You should see the Python 3 version displayed.

Open the terminal and type:

python3 --version

After Homebrew is installed, you can install Python by running:

brew install python

To install Homebrew, open the terminal and run:

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

macOS often comes with Python 2 pre-installed. You can check the installed version by opening the terminal and typing:

python --version

1.4.1.3 Installing Python on Linux

Most Linux distributions come with Python pre-installed, but it’s often an older version like Python 2. To install or upgrade to Python 3:

  1. Check if Python 3 is Already Installed:
    • If Python 3 is already installed, the version number will be displayed.
  2. Installing Python 3 on Ubuntu/Debian:
  3. Installing Python 3 on Fedora/CentOS:
  4. Verify Installation:

Type the following command to verify:

python3 --version

For CentOS:

sudo yum install python3

For Fedora:

sudo dnf install python3

If Python 3 is not installed or needs upgrading, use the following commands:

sudo apt update
sudo apt install python3

Open the terminal and type:

python3 --version

1.4.2 Setting Up the Python Environment

After installing Python, it’s important to set up a proper environment for development. This includes configuring virtual environments and installing essential tools like pip.


1.4.2.1 Using pip for Package Management

  • What is pip?
    pip is Python's built-in package manager used to install and manage third-party libraries and dependencies.
  • Verifying pip Installation:
    • If you see the version number (e.g., pip 20.x.x), then pip is installed correctly.
  • Upgrading pip:
  • Installing Python Packages:

To install a third-party Python library (e.g., NumPy), you can use the pip install command:

pip install numpy

To ensure you have the latest version of pip, run the following command:

python -m pip install --upgrade pip

pip comes pre-installed with Python 3. To verify if it’s installed, open the terminal or command prompt and type:

pip --version

1.4.2.2 Setting Up Virtual Environments

Virtual environments allow you to create isolated Python environments for different projects. This prevents conflicts between different versions of libraries and ensures each project has its own dependencies.

  • Creating a Virtual Environment:
    • Open the terminal or command prompt and navigate to your project directory.
  • Activating the Virtual Environment:Once activated, you should see the name of your environment in your terminal/command prompt, indicating that you are now working inside the virtual environment.
    • After activating the environment, any packages installed using pip will only be installed within that environment.
  • Deactivating the Virtual Environment:

To exit the virtual environment and return to the global Python environment, simply type:

deactivate

Installing Packages in the Virtual Environment:

pip install requests

On macOS/Linux:

source myenv/bin/activate

On Windows:

myenv\Scripts\activate

Run the following command to create a virtual environment:

python -m venv myenv

Here, myenv is the name of your virtual environment.


1.4.3 Integrated Development Environments (IDEs) for Python

While Python can be written and executed in a text editor or terminal, using an Integrated Development Environment (IDE) can significantly enhance your productivity. Below are some popular IDEs and code editors used for Python development:


1.4.3.1 Visual Studio Code (VS Code)

  • Overview:
    VS Code is a free, open-source code editor developed by Microsoft. It’s lightweight, highly customizable, and supports Python through an extension.
  • Setting Up Python in VS Code:
    1. Download and install VS Code.
    2. Install the Python extension from the VS Code marketplace.
    3. Configure the Python interpreter by pressing Ctrl + Shift + P, typing "Python: Select Interpreter," and selecting the Python installation or virtual environment you want to use.
  • Key Features:
    • IntelliSense (code suggestions and completions)
    • Integrated terminal
    • Debugging support

1.4.3.2 PyCharm

  • Overview:
    PyCharm is an IDE specifically designed for Python development, offering a wide range of features like debugging, testing, and version control integration.
  • Installing PyCharm:
    1. Download and install PyCharm (Community Edition is free).
    2. Open PyCharm and configure your Python interpreter by going to File > Settings > Project Interpreter.
  • Key Features:
    • Robust code navigation and refactoring tools
    • Built-in support for virtual environments
    • Integrated testing and version control tools

1.4.3.3 Jupyter Notebook

  • Overview:
    Jupyter Notebook is an interactive web-based environment primarily used for data analysis and visualization in Python. It allows you to write code, view the output, and include explanatory text in the same document, making it great for data science and research projects.
  • Installing Jupyter:
  • Key Features:
    • Interactive coding environment
    • Inline data visualization (with libraries like Matplotlib)
    • Ideal for data science and machine learning workflows

Start Jupyter by running:

jupyter notebook

Install Jupyter using pip:

pip install notebook

1.4.4 Conclusion

By now, you should have Python installed on your system, understand how to manage packages using pip, and know how to set up and work with virtual environments. Additionally, you’ve been introduced to various tools and IDEs that can enhance your productivity as you write and run Python code. The next step is to start writing and executing Python programs, explore its syntax, and dive into real-world projects.