Python Development Environment

Overview

Teaching: 130 min
Exercises: 130 min
Questions
  • How to install Python?

  • How to use conda and pip package managers?

  • How do you create and activate a virtual environment?

  • How to install and use Interactive Development Environments and Integrated Development Environment (IDE)?

  • How to use Google Colab?

Objectives
  • Help participants set up a Python development environment on their machines.

  • Learn about conda and pip package managers.

  • Understand how to create and manage virtual environments.

  • Learn about Interactive Development Environments.

  • Get hands-on experience on IDEs.

  • Learn how to use Google Colab.

Setting up the Python Development Environment


Activities


What is Anaconda?

Importance of Anaconda for Python and Machine Learning Projects


Install Anaconda/Python on Windows OS

Follow these steps to install Anaconda on your Windows machine:

Step 1: Download the Installer

Step 2: Launch the Installer

Step 3: Start the Installation

Step 4: Accept the License Agreement

Tip: It is recommended to select “Just Me” during installation. This ensures Anaconda is installed only for the current user.

Step 5: Choose Installation Type

Step 6: Select Installation Folder

Important:

  • Avoid installing Anaconda in a directory path that contains spaces or Unicode characters.
  • Do not install as Administrator unless admin privileges are necessary.
  • Do not add Anaconda to the PATH environment variable, as this may cause conflicts with other software.

Step 7: Install Anaconda

Step 8: Finish Installation

Additional Resources

For more detailed information, refer to the Anaconda Installation Documentation.


Virtual Environment

When installing Python on your machine, you have the option to install it system-wide or in a virtual environment.

A virtual environment is an isolated Python environment that allows you to manage dependencies for different projects without conflicts. Below are step-by-step guides for creating virtual environments using Conda and pip.

Python Environment Management with Conda

Verify Conda Installation

conda --version

To list of all available environments

conda info --envs
conda env list

Verify Python Installation

python --version

Important:

  • Ensure Conda is installed. If not, install Anaconda or Miniconda.

Create a New Environment

conda create --name my_env python=3.9
conda create -n my_env python=3.9

Important:

  • Replace my_env with your desired environment name and 3.9 with your preferred Python version.

Activate the Environment

conda activate my_env

Install Required Packages

conda install packages_name 
conda install numpy pandas matplotlib notebook

Uninstall packages

conda uninstall packages_name
conda uninstall numpy

Search for packages

conda search packages_name
conda search numpy

Update packages

conda update packages_name
conda update numpy

List installed packages

conda list

Sharing python environment

Export the environment

conda env export > environment.yml
conda env export --from-history > environment.yml

Install the environment from the environment.yml file

conda env create -f environment.yml

Deactivate the Environment

conda deactivate

Remove an Environment

conda remove --name my_env
conda remove -n my_env
conda remove 

Python Environment management with pip

Python Default Package Manager: pip is the standard/default package manager for Python.

Virtual Environment using Windows Command Prompt

Create a Virtual Environment

python -m venv my_env

Activate the Virtual Environment

On Windows

my_env\Scripts\activate

On macOS/Linux

source my_env/bin/activate

To upgrade pip

`python.exe -m pip install --upgrade pip`

Install Required Packages

pip install numpy pandas matplotlib notebook

Deactivate the Virtual Environment

deactivate

Remove the Virtual Environment (Optional)

On Windows

rmdir /s my_env

On macOS/Linux

rm -r my_env

Virtual Environment using Windows Power-Shell

Verify Python Installation

python --version

Create a Virtual Environment

python -m venv my_env

Activate the Virtual Environment

.\my_env\Scripts\Activate

Install Required Packages

pip install numpy pandas matplotlib notebook

Deactivate the Virtual Environment

deactivate

Remove the Virtual Environment (Optional)

Remove-Item -Recurse -Force .\my_env

Interactive Development Environments

Jupyter Notebooks

1. Interactive Computing

2. Documentation

3. Reproducibility

4. Flexibility

5. Data Visualization

6. Education

Launching Jupyter Notebook (Hands-on)

Using the Anaconda Prompt

1) Open the Anaconda Prompt terminal and run the following command to start Jupyter Notebook

jupyter-notebook
jupyter-notebook

Important:

  • This will open the Jupyter interface in your default web browser with the file extension .ipynb.
  • The landing page displays your file directory.

Using the Anaconda Navigator

1) Open the Anaconda Navigator from the Windows Start and select the desired Environment

2) Click on Open with Jupyter Notebook.

3) Select the desired folder or create a new folder (Click New in top-right corner and create Newfolder)

4) Click New in the top-right corner and select Python 3 (ipykernel)

5) A new notebook will open with the file extension .ipynb.

Basic Features of Jupyter Notebook

There are two main cell types that we will cover:

1) Cells

Headings

# This is a level 1 heading
## This is a level 2 heading

Plain text formatting

 **bold** or __bold__
 *italic* or _italic_

Paragraphs must be separated by an empty line.

* Sometimes we want to include lists.
* Which can be bulleted using asterisks.

1. Lists can also be numbered.
2. If we want an ordered list.
[It is possible to include hyperlinks](yonsci.github.io/gdp-nowcasting-ml/)

Inline code

bar()

Images

 ![Alt text](../assets/img/uneca_logo.png)

2) kernel: It’s the computational engine that runs your code. When you write code in a notebook and ask it to run, the kernel is what takes that code, processes it, and gives you the results.

Importing Libraries and Writing Code

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Example: Create a simple plot
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Sine Wave")
plt.show()

Saving and Exporting

Integrated Development Environment (IDE) (Optional)

Integrated Development Environment (IDE) is a software application that provides comprehensive tools for coding, debugging, and managing Python projects. It streamlines development by integrating features like syntax highlighting, code completion, debugging, and version control.

Key Features to Look for in an IDE

Types of IDEs for Python

These IDEs support multiple programming languages, including Python.

How to Install Visual Studio Code (VS Code)

Visual Studio Code (VS Code) is a lightweight, versatile, and powerful code editor for Python and many other languages.

Step 1: Download VS Code

  1. Visit the official VS Code website.
  2. Click on the Download for Windows, macOS, or Linux button based on your operating system.

Step 2: Install VS Code

On Windows

  1. Locate the downloaded .exe file in your Downloads folder.
  2. Double-click the file to start the installation process.
  3. Follow the setup wizard:
    • Accept the license agreement.
    • Choose an installation location or use the default.
    • Select optional tasks, such as:
      • Create a desktop shortcut.
      • Add “Open with Code” to context menus.
      • Register VS Code as the default editor for supported file types.
  4. Click Install and wait for the installation to complete.

On macOS

  1. Open the downloaded .dmg file.
  2. Drag the Visual Studio Code icon into the Applications folder.
  3. Open Launchpad or Finder to locate and run VS Code.

On Linux

  1. Open a terminal
  2. Install the .deb or .rpm package using your package manager.

    sudo apt install ./<file>.deb    # For Debian-based systems
    sudo rpm -i <file>.rpm          # For Red Hat-based systems
    

Launch/Open VS Code

Important:

  • On the first launch, VS Code may prompt you to install recommended extensions.

Install Python Extension

Important:

  • This extension adds features like IntelliSense, debugging, linting, and Jupyter Notebook support.

Set Up Python Interpreter

Important:

  • Select the Python interpreter installed on your system (e.g., Python 3.x or your virtual environment).

Test Your VS code Setup

Google Colab (Optional)

Cloud-Based: No need for local installations; everything runs in the cloud.
Free GPUs and TPUs: Access hardware accelerators for machine learning and deep learning tasks.
Interactive Development: Combine code, text (Markdown), and outputs in a single document.
Integration with Google Drive: Automatically save and access notebooks from your Google Drive.
Collaboration: Share notebooks and collaborate with others in real-time.
Rich Visualization: Supports libraries like Matplotlib, Seaborn, and Plotly for creating visualizations.
Easy Library Installation: Install libraries directly with pip or apt.

How to Set Up and Use Google Colab

Google Colab (short for Colaboratory) is a free, cloud-based platform for coding and executing Python. It’s particularly popular in data science, machine learning, and research due to its simplicity and powerful features.

Access Google Colab

  1. Go to Google Drive.
  2. Sign in with your Google account. If you don’t have an account, create one first.

Create a New Folder

  1. In Google Drive, click the New button on the left sidebar.
  2. Select New Folder and give it a meaningful name (e.g., Colab-Projects).
  3. Click Create to make the folder.

Create a New Google Colab File

  1. Navigate to the newly created folder in Google Drive.
  2. Right-click inside the folder and select More > Google Colaboratory.
  3. A new .ipynb file will open in a new browser tab.
  4. Mount Google Drive in Colab

    To access files in your Google Drive from the Colab notebook, mount the drive:

    from google.colab import drive
    drive.mount('/content/drive')
    

    Important:

    • Follow the prompts to authorize access to your Google Drive.

Set the Working Directory

Set your Colab working directory to the folder you created earlier:

import os
os.chdir('/content/drive/MyDrive/Colab-Projects')  # Replace 'Colab-Projects' with your folder name

Unmount Drive (Optional)

drive.flush_and_unmount()

Understanding the Interface

Configure the Runtime

  1. Click on Runtime > Change Runtime Type.
  2. Choose:
    • Hardware Accelerator:
      • None for CPU-only tasks.
      • GPU for accelerated computations (e.g., TensorFlow, PyTorch).
      • TPU for advanced deep learning workloads.
    • Runtime Type: Choose Python 3.

Writing and Running Code


Exercise

1) Installing Anaconda
2) Creating a Conda Environment named gdp_nowcasting
3) Activate the environment
4) Install the following packages: NumPy, Pandas, Matplotlib
5) Verify the installed libraries and their versions

   print("NumPy version:", np.__version__)
   print("Pandas version:", pd.__version__)
   print("Matplotlib version:", matplotlib.__version__)

6) Deactivate the environment
7) Get familier with Jupyter Notebook, VSCODE, Google Colab
8) Bonus Challenge: Research and install a new Python library not mentioned in the tutorial, such as scikit-learn or statsmodels.


Key Points

  • Installing Python via Anaconda Distribution.

  • Using conda and pip package managers

  • Creating a Virtual Environment.

  • Setting up Interactive Development Environments.

  • Setting up Integrated Development Environment (IDE).”

  • Using Google Colab.

Copyright © 2024 UNECA-ACS

Contact